Sessions and cookies

The place for general PHP questions and hints for PHP on IBM i
Post Reply
jame6549
Posts: 5
Joined: Mon Oct 07, 2013 8:45 am
Contact:

Sessions and cookies

Post by jame6549 » Tue Oct 22, 2013 9:07 am

Can anyone give me a complete introduction to sessions and cookies? I learned them in college but I am getting difficulties to create a session. I have tried Google, but I think I am not a good Internet surfer; that's why I didn't get any solution.

mark397
Posts: 87
Joined: Thu May 30, 2013 6:12 pm

Re: Sessions and cookies

Post by mark397 » Tue Oct 22, 2013 5:36 pm

Search Google for "PHP session" and the first result will lead you to the most complete documentation from php.net. You'll want to use the $_SESSION variable. It's an array that can be accessed by calling the function session_start(). Really all you need to do is start the session on every page you want to read/write from. Tip: don't start the session twice in a script, for example if you start the session on a displayed page and an include file inside it you'll spam your error log. After the session is started you can r/w to it. The session variable is an automatic way for PHP to manage cookies. There's also a way for javascript to manage cookies, but this is usually less preferred. Another cool solution you should look into is HTML5 storage.

jame6549
Posts: 5
Joined: Mon Oct 07, 2013 8:45 am
Contact:

Re: Sessions and cookies

Post by jame6549 » Wed Oct 23, 2013 12:07 pm

mark397 wrote:The session variable is an automatic way for PHP to manage cookies. There's also a way for javascript to manage cookies, but this is usually less preferred. Another cool solution you should look into is HTML5 storage.
Thanks mark for reply. If session automatic manages cookies then what is the purpose of setcookie()?

mark397
Posts: 87
Joined: Thu May 30, 2013 6:12 pm

Re: Sessions and cookies

Post by mark397 » Thu Oct 24, 2013 8:39 pm

Turns out I was misinformed when I was introduced to sessions, a quick Google search for "php session vs cookie" shows that sessions are server-based and cookies are client-based. A session expires when a browser is closed, and a cookie can be persistent. For example, a login system typically uses a session, but a cookie could save the session so when the user returns they're still logged in (remember the checkboxes on website logins that say "keep me logged in").

loui7354
Posts: 10
Joined: Tue Jul 22, 2014 2:28 pm
Location: New York

Re: Sessions and cookies

Post by loui7354 » Wed Jul 23, 2014 6:29 am

Hi mark once again i say thanks to you for sharing your wonderful thoughts here and provide the solution here. I amazed to know that you have some excellent developing skills which helps my team also. I just want to send you PM soon and discuss about some more PHP developing related problems if you don't mind.

mark397
Posts: 87
Joined: Thu May 30, 2013 6:12 pm

Re: Sessions and cookies

Post by mark397 » Thu Jul 24, 2014 11:22 pm

No problem, message away! Although I do like to keep Q&A about help public on forums so it's accessible to others who may be looking for the same issue :) If you use IRC I can PM you my nick.

die5030
Posts: 1
Joined: Fri Jan 02, 2015 1:18 pm

Re: Sessions and cookies

Post by die5030 » Fri Jan 02, 2015 1:19 pm

Really all you need to do is start the session on every page you want to read/write from. Tip: don't start the session twice in a script, for example if you start the session on a displayed page and an include file inside it you'll spam your error log. After the session is started you can r/w to it. The session variable is an automatic way for PHP to manage cookies. There's also a way for javascript to manage cookies, but this is usually less preferred. Another cool solution you should look into is HTML5 storage.??







_________________
GuL

Post Reply