【发布时间】: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