0

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:((

Edited by pritaeas: Added code tag

4
Contributors
4
Replies
5
Views
4 Years
Discussion Span
Last Post by jkon
0

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

0

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?

0

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.