Session Question

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
Bigun
Forum Contributor
Posts: 237
Joined: Tue Jun 13, 2006 10:50 am

Session Question

Post by Bigun »

If you start a session on one page, save the info into the session, would it be necessary to call session_start again on the next page to recall that information?
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

yes
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Yes, always. Anytime you are using session data on a page you need to start the page with session_start() (unless you use auto_session_start [i think?!?!?] in php.ini).
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

In simplest terms -- calling session_start() loads the session data for that "user" into the $_SESSION superglobal array.

So the $_SESSION array will not exist until session_start() is called.
(#10850)
User avatar
technofreak
Forum Commoner
Posts: 74
Joined: Thu Jun 01, 2006 12:30 am
Location: Chennai, India
Contact:

Post by technofreak »

Yes, the session_start() should be included at the first line of all the pages where you wish to make use of the variable declared as session variables, $_SESSION[].

Actually, when you initialize a session variable by declaring it as,

Code: Select all

$_SESSION['user'] = $user;
The session variable gets written in a file which is stored in a particular location in your PHP directory ( like C:/PHP/temp) Everytime a session is created, a new file is created where the session variables are stored. When you use a session_start() at the beginning of the code, the session variables are made available from the global session array (contents of this file) to the code to follow. The files exists till the session exists ( can be killed with session_destroy() function) and the contents can be unset using the unset() function.

This is very helpfull to find whether your session variable declarations are working properly, by checking the contents of the corresponding session file. Files are usually named after a unique session id.
Post Reply