需要清除会话值,会话值不携带到另一个页面


Need to clear the session value, session value not carried into another page

我在页面之间携带会话值时遇到问题。

我为这个问题挣扎了 3 天。

帮助我克服这个问题。

index.php(登录页面):

// initially declaring a variable with null value
!! include "conn.php";
   @session_start();
   if(isset($_SESSION['uname']))
   {
    $_SESSION['uname'] = " ";
   }
   else
   {
    $_SESSION['uname'] = " ";
   }
?>
//later assigning the value
$usrname = $_POST['uname'];
    $pass  = $_POST['pass'];
    $chk = mysqli_query($con,"select * from members WHERE username='$usrname'");
    while($value = mysqli_fetch_array($chk))
    {
        $realpassword = $value['password'];
        $_SESSION['uname'] = $_POST['uname'];
    }
    if(!isset($realpassword))
    {
        $realpassword  = "";
    }
    if($realpassword == $pass)
    {
         echo "<script>window.location.assign('dashboard.php');</script>";
    }

Dashboard.php(仪表板):

// In dashboard
@session_start();
include "conn.php";
if(isset($_SESSION['uname'])&&$_SESSION['uname']!="")
{
$uname =$_SESSION['uname'];
}
else{
 echo "<script>window.location.assign('http://www.website.com');</script>";
}
/// This page working fine

在第 3 页:

/// Session value not carried into this page .. when this page loads automatically logouts and redirect into home page
session_start();
include "conn.php";
if(!isset($_SESSION['uname'])&&$_SESSION['uname']=="")
{
  echo "<script>window.location.assign('http://www.website.com');</script>";
}
$uname =$_SESSION['uname'];

你写道:

if(!isset($_SESSION['uname']) && $_SESSION['uname'] == "")
 {
 echo "<script>window.location.assign('http://www.website.com');</script>";
 }

应该是(或不和):

if(!isset($_SESSION['uname']) OR $_SESSION['uname']==""){
    echo "<script>window.location.assign('http://www.website.com');</script>";
}

您的代码作为伪代码

索引.php:

1 start a session
2 if uname is set in session, set it to one space
3 otherwise, set it to one space
4 get data from db
5 if we have data, set uname in session to POST data uname

在"第 3 页"中:

1 if uname is NOT set in session OR uname in session is empty string
2     logoff
3 otherwise 
4     proceed ...

根据另一个答案和索引中的 2) - 3),"第 3 页"中 1) 中的条件永远不会为真。在仪表板中,您可能会看到类似的问题,即使用一个空格" ""删除"$_SESSION['uname']并检查空字符串""

更改索引.php:

include "conn.php";
@session_start();
unset($_SESSION['uname']); // delete previous values unconditionally (!)