I have a class that sets various session variables. After I set the session variable I do a var dump of SESSION and get an output of them all. all good so far. Then I navigate to another page.
session_start(); // i call this right after opening the php tag
var_dump($_SESSION); // i call this after setting the variables
and it's empty this time?
Setting my sessions
while( $row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{
$_SESSION['atid'] = $row['autotaskid'];
$_SESSION['bmsid'] = $row['bmsid'];
$_SESSION['shavlikid'] = $row['shavlikid'];
$_SESSION['cpid'] = $row['cpid'];
}
Trying to use the variables on another page
$autotaskid = $_SESSION['atid'];
$tsql = "SELECT COUNT(tblBackupArchive.StatusID) AS total, tblBackupArchive.StatusID ".
"FROM tblBackupArchive INNER JOIN ".
"tblBackup ON tblBackupArchive.BackupID = tblBackup.BackupID ".
"GROUP BY tblBackupArchive.StatusID, tblBackup.ClientID ".
"HAVING (tblBackup.ClientID = " . $autotaskid . ")";
Results in
Undefined index: atid in C:\Program File...
I've made sure I'm issuing the start_session(); function before any other code is run on every page that uses them.
Another important point: the php page that calls the method setting the variables within an iframe. when i open the page in a new tab/window it sets the sessions correctly. It's almost like the main window has session variables and then each iframe keeps it own seperate.
Any ideas?
Billy
Make sure that:
session_start()
before you set or get a session variable. (If the server is configured to autostart sessions, this is not necessary.)Yes. You have to learn to debug your code.
As you can see, your question cannot be answered out of guesswork. So, it's time for handwork.
then make a testing script to see if your sessions do work
if it works, make it to use cookies
if it works too, you have to check your code.
Print out variables, reduce code, etc
Try comparing the session ids. If you get a different session id across the calls, the session management is not correctly set up.
I.e. your code may rely on session ids stored in cookies, but that option is disabled (see your
php.ini
).As a debugging measure you may use "transparent session ids" (they're concatenated to any link), but not for production code.
If you're calling
session_close()
where you should be, then are you accidentally destroying the session? Perhaps a few too manysession_destroy()
calls?And by a few too many I mean one.
Based on your first code block, it sounds like you might be calling
session_start()
after you've tried to set the$_SESSION
variables. You must callsession_start()
first! I could be wrong, in which case I apologise. But based on the above code snippet it could well be your problem!