views:

58

answers:

3

Hi,

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

+1  A: 

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 call session_start() first! I could be wrong, in which case I apologise. But based on the above code snippet it could well be your problem!

chigley
sorry that's just me confusion people, I cut out some code there. I actually call session_start BEFORE anything else. Right after opening the php tag. thanks though
iamjonesy
In which case I'm not sure! Could be to do with your environment. I've never worked with local PHP on Windows before.
chigley
A: 

If you're calling session_close() where you should be, then are you accidentally destroying the session? Perhaps a few too many session_destroy() calls?

And by a few too many I mean one.

mattbasta
+1  A: 

Any ideas?

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.

  • First make sure you can see any error occurred. Just add an intentional one and see. If you see no errors you have to turn it on.
  • then make a testing script to see if your sessions do work

    <? 
    session_start(); 
    if (!isset($_SESSION['counter'])) $_SESSION['counter']=0;
    echo "Refreshed ".$_SESSION['counter']++." times.<br>
    <a href=".$_SERVER['PHP_SELF'].'?'.session_name().'='.session_id().">refresh</a>"; 
    ?>
    

if it works, make it to use cookies

<? 
session_start(); 
if (!isset($_SESSION['counter'])) $_SESSION['counter']=0;
echo "Refreshed ".$_SESSION['counter']++." times.<br>
<a href=".$_SERVER['PHP_SELF'].">refresh</a>"; 
?>

if it works too, you have to check your code.
Print out variables, reduce code, etc

Col. Shrapnel
+1 Teach a man to fish....
Steve Claridge
hi thanks for your suggestions. I added the counter test you posted. The interesting thing is that after trying that test, then navigating to the page where my sessions are set, then moving to the other page, instead of seeing the session array empty I now see counter. The issue must then be with the code setting the sessions. I set the sessions in a method used only by the first page. can it be they are being destroyed after being used?
iamjonesy
@Jonesy the code you posted here is all right. Though I'd make it with single statement `$_SESSION['savedata'] = $row;` and then use it as `$autotaskid = $_SESSION['savedata']['atid'];` sometimes it can help. if not - you have to add var_dump($_SESSION); in as many places as you can
Col. Shrapnel
What I forgot to mention is that the php app is accessed via an iframe. When I do the same in a new window everything is ok. Is there a different set of session variables for each iframe?
iamjonesy
@Jonesy iframes has nothing to do with PHP or sessions. Sessions are domain related.
Col. Shrapnel