需要清除会话值,会话值不能进入另一个页面

时间:2015-01-07 14:13:25

标签: php mysql session session-variables session-cookies

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

我为这个问题奋斗了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'];

2 个答案:

答案 0 :(得分:3)

您写道:

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

应该是(或不是AND):

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

答案 1 :(得分:0)

您的代码为伪代码

<强>的index.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']“删除”" "并检查空字符串""而导致的类似问题

更改index.php:

include "conn.php";
@session_start();

unset($_SESSION['uname']); // delete previous values unconditionally (!)