Forum Moderators: coopster

Message Too Old, No Replies

Defining session variables

php sessions

         

don_dr

4:44 pm on Jul 27, 2008 (gmt 0)

10+ Year Member



I am having problems with session variables on multiple pages. when i define a new set of session variables on the 2nd page, they old set of variables seem to vanish completely. here is what i mean

page 1
========
//i define variables based on values posted from a form on the same page using PHP_SELF, run a couple of checks and redirect to page 2 using header()
<?php
session_start();
$name = htmlspecialchars($_POST['name']);
$lname = htmlspecialchars($_POST['lname']);

$_SESSION['name'] = $name;
$_SESSION['lname'] = $lname;

....
?>

page 2
========

echo $_SESSION ['name'] works just fine....
//then i define new variables on page 2 using the same method and redirect to page 3

<?php
session_start();
//new variables
$co = htmlspecialchars($_POST['co']);
$_SESSION['co'] = $co;
....
?>

page 3
========

echo $_SESSION['co'] shows the value of $co, but $_SESSION['name'] seems to have completely vanished.

Anyone know why this is happening? I can be a little more specific if neccessary.

Thanks.

nick279

9:53 pm on Jul 27, 2008 (gmt 0)

10+ Year Member



Just double check that your sessions aren't expiring between pages.

$expireTime = 60*60*24*100; // 100 days
session_set_cookie_params($expireTime);
session_start();

omoutop

2:20 pm on Jul 28, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



sessions do not expire so soon...i think the default timeout is around 25 mins (not sure on this thought).

echo "<pre>";
print_r($_SESSION);
echo "</pre>";

use the above to every page you initialize sessions to check what is being passed/defined/set/unset

Try the code in both header and footer of your pages.

Is it pssible that between page 2 and page 3 you do some other redirection, through a page that do not have session_start()?

On page 3 you said that $_SESSION['name'] do not work. What about $_SESSION['lname']? Does it has any value?

ag_47

3:53 am on Jul 29, 2008 (gmt 0)

10+ Year Member



Also make sure the cookies are being set on the browser.
open page 1, check cookies, check session files on server.
Go to page 2, see what changed? Was a new session file created on the server? If yes, then problem is in cookies.

djbuddhi

8:25 am on Jul 29, 2008 (gmt 0)

10+ Year Member



or else u can used session_register();

don_dr

10:41 am on Jul 29, 2008 (gmt 0)

10+ Year Member



i think the probelm had to do with something concerning sessions expiring because i added the following bit of code to pages 1 and 2 and everything seems to be ok now

$expireTime = 60*60*24*1; // 1 day
$sessdir = "/path/";
//session_save_path($sessdir);
//session_set_cookie_params($expireTime);
session_start();