Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.2k views
in Technique[技术] by (71.8m points)

php - Why is my SESSION array OK on one page but empty on another?

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...