簡體   English   中英

如果在框架內單擊,框架集/ PHP將丟失會話變量

[英]Frameset / PHP loses session variable if clicking inside frame

我正在為我的網站使用框架(我知道這不是最佳選擇,計划將來進行更改)

使用左側的導航,可以正確傳遞會話變量。

但是,如果我單擊以從“內容”框架中打開一個新頁面並替換它,則會創建一個新會話。

我在每個頁面的開頭都設置了session_start(),並嘗試使用頁眉解決方法,但是沒有任何解決方法。 如果我查看sessiondata文件夾,我會看到在更改“內容”框架時會創建一個新的會話。

if(!isset($_SESSION))  {
session_start();·
}
header('P3P: CP="CAO PSA OUR"');

-框架:

<?php session_start();
if($_SESSION['email']==''){echo 'please login with your credentials';}else if($_SESSION['email']!='') {
    session_save_path('/var/www/home/path/sessiondata/');
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">

<html><head>
  <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  <title>Website Name.</title>
  <meta http-equiv="Pragma" content="no-cache">
  <meta http-equiv="Cache-Control" content="no-cache">
  <link rel="SHORTCUT ICON" href="http://domain.com/favico.ico" type="image/x-icon">
</head>


<frameset rows="11%,89%" frameborder="1" framespacing="1" topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" border="0" bordercolor="#ffffff">
  <frame name="bar" src="report/index1.php" frameborder="0" noresize="noresize" scrolling="no">

  <frameset id="mainset" cols="137,5,*" frameborder="0" framespacing="1" topmargin="0" leftmargin="0" marginheight="0" marginwidth="0" border="0" bordercolor="#ffffff">
    <frame name="menu" src="report/index_002.php" frameborder="0" noresize="noresize">
    <frame name="function" src="report/index_003.php" frameborder="0" scrolling="no" marginwidth="0" marginheight="0" border="0" framespacing="0" noresize="noresize">
        <frame name="content" src="report/index_004.php" frameborder="0" noresize="noresize">
      </frameset>
  <noframes><body><p>This page uses frames, but your browser doesn't support them.</p></body></noframes>
</frameset>
</html>
<?php 
 session_start();
 if($_SESSION['email']==''){
      echo 'please login with your credentials';
 } else if($_SESSION['email']!='') {
      session_save_path('/var/www/home/path/sessiondata/');
?>

花一些時間考慮一下這段代碼在做什么。 session_start()意味着

  • 使用提供的會話令牌從默認位置檢索會話數據
  • 創建一個會話ID,然后在該會話ID所標識的默認位置創建一個新會話

session_save_path()表示將來對會話數據進行的任何讀或寫操作都將轉到該位置,而不是默認位置。

因此,您的代碼將永遠不會檢索經過身份驗證的會話(因為session_save_path()在session_start()之后)。 而且,碰巧的是,您在此處描述的行為不能由您顯示給我們的代碼創建(暗示其他地方或其他時間與會話數據進行交互的不同代碼)。

解決此問題的唯一方法是當前將會話ID作為參數傳遞。 奇怪的是,它適用於從左側導航中調用的所有其他頁面。

echo "<TD align='center'> <a href='http://domain.com/path/report/showt.php?option=$data&id=$iid&sess=$sessid' target='content'> Detail of $data</a></TD>\n";

並在相應的文件showt.php中被調用:

<?php
$sessid= $_REQUEST['sess'];
session_id($sessid);
session_start();·
print_r($_SESSION);
header('P3P: CP="CAO PSA OUR"');
?>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM