Page 1 of 1

Session's getting lost?

Posted: Mon Dec 29, 2008 11:18 am
by yoji
Hi, I have tried to solve this prob myself but couldn't, please help me out here..
This is the code:

Code: Select all

 
<?php
session_start();
$_SESSION['name']='dexter';
echo $_SESSION['name'];
?>
 
The above code works just fine... The problem comes when I try to access this associative array in another file. Let's say this is first file:

Code: Select all

 
<?php
session_start();
$_SESSION['name']='dexter';
?>
<a href="page2.php">Go to this page</a>
 
And now let's say that this is page 2.php

Code: Select all

 
<?php
echo $_SESSION['name'];
?>
 
Why does it give error "Undefined variable: _SESSION"??

Re: Session's getting lost?

Posted: Mon Dec 29, 2008 11:22 am
by jaoudestudios
You need session_start(); on every page!

If you had googled 'php session' then you would have found this at the top of the search results
http://uk3.php.net/function.session-start
Which does state this in the first few lines :)

Re: Session's getting lost?

Posted: Mon Dec 29, 2008 11:25 am
by cristiano
You need to add session_start(); on all pages you wish to extend your session to.

Re: Session's getting lost?

Posted: Mon Dec 29, 2008 11:25 am
by cristiano
Oops, sorry looks like someone already posted the answer.

Re: Session's getting lost?

Posted: Mon Dec 29, 2008 11:26 am
by yoji
I have referred to quite a few resources (including documentation. Which btw is like a physics textbook :p )
Now there's is a new prob: "Undefined index: name" Even though I have double check the "index name".