Forum Moderators: coopster

Message Too Old, No Replies

Sessions and changing pages

Why does the session stop?

         

greenowl

5:13 pm on Mar 1, 2011 (gmt 0)

10+ Year Member



Hi folks - a newbie here, trying to get my head round PHP sessions.

I put session_start()in a script on main page of my website expecting the session to persist when the user looks at other pages of my site but this does not seem to be the case. The session appears to 'stop'.

If session cookies are enabled in php.ini i can get to the info by calling session_start() on another page and the cookie supplies the session id from the previous start up.

Is session_start a bit of a misnomer - should it be more like session_open - and should i try to the think of the session as a pseudo file that you open with session_start().

Or should it persist and i have set something incorrectly, perhaps.

I have Apache as a server on my PC and have set the site up on localhost.

Many thanks for any help.

Orangutang

11:05 pm on Mar 1, 2011 (gmt 0)

10+ Year Member



Hi greenowl,

I'm a newbie myself but I use sessions and relate to your post. You need to have session_start(); on all pages you want to use the session contents, ie session id.

The placement of session_start(); is important as well, I believe it should always be at the very top of the page which is where I put it but I've seen on occasion scripts that call in half way down the page.

Hope it helps.

greenowl

11:36 am on Mar 2, 2011 (gmt 0)

10+ Year Member



Thanks very much - getting the hang of it - it seems you have to pass the session_id across via the url link or a cookie to get at the same session on different pages.

Matthew1980

12:24 pm on Mar 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi all,

Every single page that you need a reference handle to a $_SESSION needs the session_start() at the top, this is the way that php re instanciates that connection.

Hope that makes sense.

Cheers,
MRb

rocknbil

4:57 pm on Mar 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah it's a bit of a misnomer. :-) By definition [us2.php.net] it opens or resumes the current one based on a session identifier.

Note also is is the PHPSESSID cookie set in the browser that keeps the client connected to the session, otherwise you will have to append query strings to URL's.