在我的項目中,登錄成功後,打開一個顯示用戶名的頁面。這是完美的。但是,當我刷新同一頁面而沒有註銷時,會話似乎被破壞並且用戶名消失。另外,如果我嘗試在新選項卡中打開頁面,則表明您需要先登錄,會話似乎已丟失或損壞。當我執行和測試它時,代碼幾天前運行良好。這是今天發生的這個問題。無法弄清楚錯誤或問題是什麼。請幫忙。 下面是代碼會話似乎死於頁面刷新或頁面在新選項卡中打開
<?php
session_start();
error_reporting(0);
?>
<html>
<head>
<title>Profile Test</title>
</head>
<body>
<?php
if(1==2)
{
$message = "Login to access the books page";
echo "<script type='text/javascript'>alert('$message');</script>";
echo "<script language='javascript'>";
echo "window.open('register.html','_self')";
echo "</script>";
}
else
{
echo "Welcome ".$_SESSION['username'];
}
?>
<br>
<button id="logout">Logout</button>
</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$("#logout").click(function(){
<?php session_destroy(); ?>
window.open("index.html","_self");
});
});
</script>
</html>
最近,我發現的$ _SESSION值[「用戶名」]丟失在頁面刷新。這可能是什麼原因? –
session_destroy()刪除所有$ _SESSION變量。正如Gaurav Rai所說,在其他地方運行該功能。 – jlemley
KK將嘗試:) –