【问题标题】:Need to clear the session value, session value not carried into another page需要清除会话值,会话值不携带到另一个页面
【发布时间】:2015-03-05 11:02:54
【问题描述】:

我在页面之间传递会话值时遇到问题。

我为这个问题苦苦挣扎了 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'];

【问题讨论】:

  • 您的会话变量在重定向之前是否仍然设置:echo "window.location.assign('dashboard.php');" ?
  • 在发送任何内容之前使用 session_start() ! @session_start(); 不是好的做法
  • @ 不是好的做法,期间。这相当于把你的手指塞进耳朵里然后说“lalalalala 听不到你的声音”
  • 您应该使用header 调用进行重定向。没有 JS,重定向将不起作用。
  • 这段代码非常容易受到多重攻击:/你为什么要使用:if(isset($_SESSION['uname'])) { $_SESSION['uname'] = " "; } else { $_SESSION['uname'] = " "; }。这是多余的。

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


【解决方案1】:

你的代码是伪代码

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 (!)

【讨论】:

    【解决方案2】:

    你写的:

    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>";
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-12-13
      • 1970-01-01
      • 1970-01-01
      • 2015-08-01
      • 2014-03-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多