Jump to content

$_SESSION variables


CB150Special

Recommended Posts

This is a bit confusing.

 

To use session variables, I put the following on the start of each page

<?php
    session_start(); // Start the session
?> 

and I initialize the variables on the index.php page

<?php
    $_SESSION['memID'] = '';
    $_SESSION['bikeID'] = ''; 
?>

On the index.php page I use an include('header.php')  file that displays the logo etc and on the right had side, I display my session variables for debugging, all ok. index page works.

 

Now I navigate to the next page and header.php now can't find the session variables.  undefined index.  So it look like it doesn't recognize the session_start() on the second page.

 

If I add session_start() to the header.php file, I get session already started, so it does know it is there but can't use it !!!!

 

Any suggestions ?

 

Thanks

Link to comment
Share on other sites

if the session_start() on either page is not working (there would a php warning level error message) you would get the current symptom. do you have php's error reporting set to E_ALL, so that all errors will be reported? both the 'session already started' and the 'undefined index' errors are notice level error messages.

 

how are you navigating between pages and other than the name of the page, is the rest of the url consistent between the pages? is the host-name/sub-domain part of the url changing between having and not-having a www. in them? is the path after the domain part of the url changing? if either of these things are changing between the pages and the session id cookie settings are not set up to match variations in the host-name or path, the initial session id cookie won't be sent from the browser to the server with the request for the second page and you would start a new session.

 

does your code have any logic in it (anywhere) that is unsettling or setting the session variables to null values? if you have a header() redirect, that doesn't have an exit/die statement after it, to stop program execution, and it is followed by some code that's unsettling or setting the session variables to a null value, you would get the current symptom.

Link to comment
Share on other sites

Every PHP page you navigate to needs to have session_start(). You have it in index.php, but if you now navigate to item.php, that page also needs a session start. Notice I didn't say every PHP page, just the main php pages that'll be executed. Header.php would not need to start the session because it's included as a part of another PHP file.

 

Doing as Gandolf said and making this process part of a config or initialize process that's included at the top of all necessary pages is a good way to go.

Link to comment
Share on other sites

mm A bit confused, I created 3 new forms, index, header, login with the minimum bits in them and it works fine. The value was taken through.

 

I'll try the php's error reporting set to E_ALL. New to me.

 

UPDATE Something is screwy here, now my original code is working with no changes.

 

Possibly I had another page open and this caused issues with the current page's session. Is this possible and can I do something about it ?

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.