[英]Session variable content changes when form refreshes in PHP
suppose I have two users in the SQL database. 假设我在SQL数据库中有两个用户。 In the table column
authority
one is the Administrator
and the other is user
. 在表列
authority
一个是Administrator
,另一个是user
。
My issue is if I log in as the Administrator, before the form refreshes, the $_SESSION['Authentication']
echoes 'Administrator', but after the form refreshes $_SESSION['Authentication']` echoes as 'user'. 我的问题是,如果我以管理员身份登录,则在刷新表单之前,
$_SESSION['Authentication']
'Administrator',但是在刷新表单之后,$ _SESSION ['Authentication']`回显为'user'。
Where have I gone wrong in my code which result the $_SESSION['Authentication'] = 'user'
whereas it should be $_SESSION['Authentication'] = 'Administrator'
after the form refreshes ? 我的代码哪里出错了,导致
$_SESSION['Authentication'] = 'user'
而刷新表格后应该是$_SESSION['Authentication'] = 'Administrator'
?
Code : 代码:
Session started at the beginning....
then this follows.
<?php
include ("connect_db/index.php");
if(isset($_SESSION['loggedUser']))
{
echo "<form action='signoff/index.php'><div id='four'>Welcome ". $_SESSION['loggedUser']." !
<input type='submit' name='soff' id='soff' class='sout' value='Sign off'></div></form>";
echo "You are the : ".$_SESSION['Authentication']." of the site.";
}
else
{
?><div id='one'><?php
echo "
<div id='u2'>
<form name='form1' method='post' action='''>
<table border='1' style='width:520px; bordercolor:#FFFFFF;'>
<tr>
<td style='width:30px;'>User Name: </td>
<td style='width:80px;'><label for='textfield'></label>
<input type='text' maxlength='12' name='UnameZoom' id='UnameZoom' class='txss'></td>
<td style='width:30px;'> Password: </td>
<td style='width:80px;'><label for='txss'></label>
<input type='password' maxlength='12' name='PwordZoom' id='PwordZoom' class='txss'></td>
<td> <input type='submit' name='loggedUser' id='loggedUser' class='mylog' value='Login'></td>
</tr>
</table>
</form>
<p> </p>
<p> </p>
</div>";
?></div><?php
if(isset($_POST['loggedUser']))
{
?><div id='two'><?php
$un = $_POST['UnameZoom'];
$pw = $_POST['PwordZoom'];
if($un=='' || $pw == '')
{echo "<div id='three'>Empty Fields</div>";}
else {
$SQL = "SELECT pword FROM users WHERE username='$un'";
$resultA = mysqli_query($db,$SQL) or die ("SQL Error!!!");
$row = mysqli_fetch_array($resultA);
if($pw == $row['pword'])
{
$resultB = mysqli_query($db,"SELECT fname AS Lna FROM users WHERE username='$un'");
$rowB = mysqli_fetch_assoc($resultB);
//$sum = $rowB['Lna'];
$_SESSION['loggedUser'] = $rowB['Lna'];
$resultC = mysqli_query($db,"SELECT authority AS Auth FROM users WHERE username='$un'");
$rowC = mysqli_fetch_assoc($resultC);
unset($_SESSION['Authentication']);
$_SESSION['Authentication'] = $rowC['Auth'];
header("refresh:3;");
//echo "<div id='four'>Welcome ". $_SESSION['loggedUser']." !</div>";
}
else
{
echo "<div id='three'>No user found</div>";
}
}
?></div><?php
}
}
?>
// this is where I'm trying to echo //这是我要回显的地方
<div id="contentLog">
<?php
if(isset($_SESSION['Authentication']))
{echo $_SESSION['Authentication'];}
?>
please use this condition 请使用此条件
if($_POST['loggedUser'] != "")
{
}
Your header("refresh:3;");
您的
header("refresh:3;");
makes me suspicious. 使我感到怀疑。 This might be relevant.
这可能是相关的。 Add
session_write_close()
before you call header()
? 在调用
header()
之前添加session_write_close()
header()
吗? Echo session_id()
to see if the refresh creates a new blank session or if you have a logic error. 回显
session_id()
以查看刷新是否创建了一个新的空白会话,或者您是否遇到逻辑错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.