PHP在刷新时删除会话变量

时间:2019-03-06 14:07:20

标签: php html html5 web-deployment

我目前正在学习如何使用PHP制作登录系统。起初一切似乎都很容易-我将输入数据设置为git config merge.ff false 变量,然后在打开的下一页开始会话。但是,当我刷新页面时,会话仍在工作,但可变数据已删除并且无法识别。


这是我的php,用于设置变量:

$_SESSION

这是我用来检查下一页会发生什么的地方:

<?php 
            include ("connection.php"); //connects to the database.

            if(isset($_POST['login'])) 
            {
                $user_name = $_POST['EMAIL'];
                $password = $_POST['PASSWORD'];

                // echo $user_name, " AND ", $password, "<br>";

                $q = "SELECT * FROM ".$table." WHERE EMAIL='".$user_name."' AND PASSWORD= '".$password."' AND IS_ADMIN= 'YES';";
                // echo "<br>",$q,"<br>";
                $r = mysqli_query($conn, $q); 

                if(mysqli_num_rows($r) > 0)
                {
                    if(session_id() == '' || !isset($_SESSION)) 
                    {
                        // session isn't started
                        session_start();

                        echo "<br>GREAT SUCCESS!!!<br>";


                        $_SESSION["SESSION_EMAIL"]= $user_name;
                        $_SESSION["SESSION_PASSWORD"]= $password;  

                        echo $_SESSION["SESSION_EMAIL"], "  ", $_SESSION["SESSION_PASSWORD"];

                        header("Refresh:0; url=\website2.php");
                    }

                } 
                else echo "<br>FAIL!!!<br>";
            }
        ?>  

2 个答案:

答案 0 :(得分:0)

<?php 
     session_start();
        include ("connection.php"); //connects to the database.

        if(isset($_POST['login'])) 
        {
            $user_name = $_POST['EMAIL'];
            $password = $_POST['PASSWORD'];

            // echo $user_name, " AND ", $password, "<br>";

            $q = "SELECT * FROM ".$table." WHERE EMAIL='".$user_name."' AND PASSWORD= '".$password."' AND IS_ADMIN= 'YES';";
            // echo "<br>",$q,"<br>";
            $r = mysqli_query($conn, $q); 

            if(mysqli_num_rows($r) > 0)
            {
                if(!isset($_SESSION["SESSION_EMAIL"])) 
                {
                    // session isn't started


                    echo "<br>GREAT SUCCESS!!!<br>";


                    $_SESSION["SESSION_EMAIL"]= $user_name;
                    $_SESSION["SESSION_PASSWORD"]= $password;  

                    echo $_SESSION["SESSION_EMAIL"], "  ", $_SESSION["SESSION_PASSWORD"];

                    header("Refresh:0; url=\website2.php");
                }

            } 
            else echo "<br>FAIL!!!<br>";
        }
    ?>  

然后在第二页上检查类似的内容

<?php   
session_start(); 
if (isset($_SESSION["SESSION_EMAIL"])) 
{
    echo 'Session is active <br>';
    echo $_SESSION["SESSION_EMAIL"];
} else echo"session is ded";
 // it prints out an error if it screws up here
?>

希望获得帮助

您可以检查其他会话变量

答案 1 :(得分:0)

我真的很讨厌这样说,老实说我应该早点做,但是在重新启动XAMPP和PHP服务器之后,一切似乎都已修复,并且该会话现在可以正常工作。现在什么都放不下,页面之间的所有转换都毫不费力。

对于所有对此我这个愚蠢的问题进行思考的人,我深表歉意,同时我还要感谢他们在回答问题上所付出的所有时间和精力。

再次感谢大家!