tags:

views:

59

answers:

3

Ok I set up a session... but now how do I make it work on my other pages?

I tried doing

@session_start();

if(isset($_SESSION['$userName'])) {

 echo "Your session is running " . $_SESSION['$userName'];

}
A: 

Make sure session_start() is at the top of every page you wish to use sessions on. Then you can refer to session variables just like in your example.

John Conde
thats what ive done =[ its not working =[ must be a problem with my login
MrEnder
A: 

Check that the session cookie is being sent to the browser on the first hit and getting returned by the browser in subsequent requests. There are lots of reasons why this may be failing - typically PHP has flushed the headers before the call to session_start() (which may be due to UTF-8 BOM chars or just messy programming).

Make sure you've got error reporting enabled.

C.

symcbean
+2  A: 

If your PHP setup is clear (session writing ok) and cookie normally sent to browser (and preserved), you should be able to do something like this

On first page :

session_start();
$_SESSION['userName'] = 'Root';

On a second page :

session_start();
if(isset($_SESSION['userName'])) {
  echo "Your session is running " . $_SESSION['userName'];
}

Be careful session_start() must be called before any output is sent, so if you had to use the @ for session_start it can hide warnings.

As these are warnings, if given example doesn't work try to add this before calling session_start :

error_reporting(E_ALL);
Benoit