我在页面之间传递 session 值时遇到问题。
我为这个问题苦苦挣扎了 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>";
}
应该是(或不是 AND):
if(!isset($_SESSION['uname']) OR $_SESSION['uname']==""){
echo "<script>window.location.assign('http://www.website.com');</script>";
}
关于php - 需要清除session值,session值不能带入另一个页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27821366/