在PHP中进行表单刷新时,会话变量内容会发生变化

时间:2013-09-07 17:00:33

标签: php

假设我在SQL数据库中有两个用户。在表格列authority中,一列是Administrator,另一张是user

我的问题是,如果我在表单刷新之前以管理员身份登录$_SESSION['Authentication']回声'管理员',但在表单刷新后$ _SESSION ['Authentication']`回显为'user'。< / p>

我的代码在哪里出错导致$_SESSION['Authentication'] = 'user',而表单刷新后应该$_SESSION['Authentication'] = 'Administrator'

代码:

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&nbsp". $_SESSION['loggedUser']."&nbsp;! &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <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&nbsp;Name:&nbsp;&nbsp;</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;'>&nbsp;Password:&nbsp;&nbsp;</td>
                      <td style='width:80px;'><label for='txss'></label>
                        <input type='password' maxlength='12' name='PwordZoom' id='PwordZoom' class='txss'></td>
                      <td>&nbsp;<input type='submit' name='loggedUser' id='loggedUser' class='mylog' value='Login'></td>
                    </tr>
                  </table>
                </form>
                <p>&nbsp;</p>
                <p>&nbsp;</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&nbsp". $_SESSION['loggedUser']."&nbsp!</div>";

                    }
                    else
                    {
                    echo "<div id='three'>No user found</div>";
                    }
                }   
                ?></div><?php
        }   
    }
?>

//这是我试图回应的地方

 <div id="contentLog">
   <?php
if(isset($_SESSION['Authentication']))
   {echo $_SESSION['Authentication'];}
    ?>

2 个答案:

答案 0 :(得分:0)

请使用此条件

if($_POST['loggedUser'] != "")
{

}

答案 1 :(得分:0)

你的header("refresh:3;");让我怀疑。 This可能是相关的。在致电header()之前添加session_write_close()?回显session_id()以查看刷新是否创建新的空白会话或是否存在逻辑错误。