Session's getting lost?

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
yoji
Forum Commoner
Posts: 25
Joined: Sun Oct 19, 2008 3:09 am

Session's getting lost?

Post 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"??
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Session's getting lost?

Post 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 :)
cristiano
Forum Newbie
Posts: 18
Joined: Tue Jul 01, 2008 1:44 am

Re: Session's getting lost?

Post by cristiano »

You need to add session_start(); on all pages you wish to extend your session to.
cristiano
Forum Newbie
Posts: 18
Joined: Tue Jul 01, 2008 1:44 am

Re: Session's getting lost?

Post by cristiano »

Oops, sorry looks like someone already posted the answer.
yoji
Forum Commoner
Posts: 25
Joined: Sun Oct 19, 2008 3:09 am

Re: Session's getting lost?

Post 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".
Post Reply