Hi, I need help please. i spent the last 6 hours looking EVERYWHERE for help about using PHP session across web pages. The $_SESSION variable is supposed to be global, right? but I can only access it on the page it was declared. like:

//PAGE 1 this works
<?php
session_start();
$_SESSION ['login'] = 1;
echo $_SESSION ['login'];

BUT WHEN I TRY TO ECHO THE SAME THING IN MY NEXT WEB PAGE IT DOESN'T WORK :((

//PAGE 2 this doesn't work :(
<?php
session_start();
echo $_SESSION ['login'];

HELP PLEASE!!! :(( I'm tired i'm giving up:((

Recommended Answers

If session cookies are disabled in your browser it doesn't work. Technically your PHP code is correct.

Jump to Post

All 4 Replies

If session cookies are disabled in your browser it doesn't work. Technically your PHP code is correct.

thanks pritaeas but if my session cookies are disabled how come it works when i try to echo it from the same page? :s

Make sure that you don't have any code before the call to session_start(); because if you have, it will be an error because there are headers already sent.
Can you tell us what's the error message? Or there is no error?

but if my session cookies are disabled how come it works when i try to echo it from the same page?

Because you define it and assign value to it in the same page .

In the same page if there is no session id in the request, it creates a Session Id (and file in file system for it). If it were a session id in the headers of the request it would use that one to read that file from the file system and give values to the $_SESSION array.

There for in the same execution flow from where you call the session_start if there is no session id in the request, the $_SESSION array is empty . Of course if you put something inside it you can retrieve it from the same execution flow (page) but if session cookies are disabled from your browser you can’t retrieve it from the next one.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.20 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.