php sessions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
netcode
Forum Newbie
Posts: 4
Joined: Mon Jul 20, 2009 7:41 am

php sessions

Post by netcode »

Hello there,

I am have two questions about php sessions.

1. Is it required to start a session every new page if you want to continue the session?
2. When does session data get dropped from the server?

Thanks.

I am sure the answer will lead into more discussion ;)

B.
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: php sessions

Post by jazz090 »

1. yes but that is if you need session data (you need to access $_SESSION), if you are not going to use session data dont call session_start()
2. never (session data is always on your server, the session key is trapped in a cookie for the user and that is whats deleted from the users pc when the session ends)
netcode
Forum Newbie
Posts: 4
Joined: Mon Jul 20, 2009 7:41 am

Re: php sessions

Post by netcode »

jazz090 wrote:1. yes but that is if you need session data (you need to access $_SESSION), if you are not going to use session data dont call session_start()
2. never (session data is always on your server, the session key is trapped in a cookie for the user and that is whats deleted from the users pc when the session ends)

1. If I start a session on one page, skip the session on the next page, and start a session again on the 3rd page, will it be in the same session as the first page? I guess that might even be a question about the way a browser handles cookies huh?

2. Is there any way to clear this data? Eventually would session data begin to take up significant space (if there are 1000's of sessions)?

Thanks for your quick reply!

B.
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: php sessions

Post by jazz090 »

1. well session data is passed in 2 ways via the URL as a query string or as cookies. most php apps use cookies to store session data,
so if session_start() is skipped in one page, the cookie still gets passed around and still exists in the browser so when session_start() is called it will use the same key it did the last time so it will be the same session. for URL based, it will open the session which the key was passed in the URL so theoretically you can jump from 2 different sessions.

2. session_destroy()
Last edited by jazz090 on Mon Jul 20, 2009 8:55 am, edited 1 time in total.
netcode
Forum Newbie
Posts: 4
Joined: Mon Jul 20, 2009 7:41 am

Re: php sessions

Post by netcode »

jazz090 wrote:1. well session data is passed in 2 ways via the URL as a query string or as cookies. most php apps use cookies to store session data,
so if session_start() is skipped in one page, the cookie still gets passed around and still exists in the browser so when session_start() is called it will use the same key it did the last time so it will be the same session. for URL based, it will open the session which the key was passed in the URL so theoretically you can jump from 2 different sessions.
I assume cookies are dropped on the users end when the browser window is closed?
User avatar
jazz090
Forum Contributor
Posts: 176
Joined: Sun Apr 12, 2009 3:29 pm
Location: England

Re: php sessions

Post by jazz090 »

dropped on the users end? what does that mean? if you mean the cookie is deleted then yes given the expire cookie time is set to 0 in php.ini which is 0 by default.
netcode
Forum Newbie
Posts: 4
Joined: Mon Jul 20, 2009 7:41 am

Re: php sessions

Post by netcode »

jazz090 wrote:dropped on the users end? what does that mean? if you mean the cookie is deleted then yes given the expire cookie time is set to 0 in php.ini which is 0 by default.
Sorry I am not one for terminology, I have a bad habit of using slang.

Your assumption is right.

All this information has been very useful, thanks!

B.
Post Reply