dtcyv3985 2016-11-22 21:12
浏览 36

PHP SESSION变量,只在第二次尝试时保存

So I'm currently working on a plattform for Dance Trainer to register for our Dance Contest... The Trainers Create an Account, Log in with that Account -> if the mail and passwort is correct some session variables will be initiated, inside those variables are the users id, mail, etc. and the users "role" (admin or user / 1 or 2) after those variables have been initiated the user will be forwarded to the actual user area website. That side will now check if the session variables are set and if the user role is high enough to visit that page, if not forward the user to the admin or login page (admin if role = 2 and login if role != 1 or 2) But the variables do not save on the first login attempt, you have to log in twice....

here is my code

Login Page:

    session_start();
    include('connect.php');  //mysql connection function
    if (isset($_POST["login"])) // check if login attemp exists
    {
        //get mail from form
        $mail = mysqli_real_escape_string($verbindung, $_POST["mail"]);

        //get password from form
        $pw = mysqli_real_escape_string($verbindung, $_POST["pw"]);

        //check if user exists in db
        $sql = "SELECT * FROM user_db WHERE mail = '$mail'";
        $query = mysqli_query($verbindung, $sql);

        while ($user = mysqli_fetch_object($query))
        {
            if (password_verify($pw, $user->password))
            {
                // set session variables
                $_SESSION["login"] = $user->acc_type;  //role
                $_SESSION["id"] = $user->id;           //id
                $_SESSION["vorname"] = $user->vorname;
                $_SESSION["mail"] = $user->mail;
                $_SESSION["nachname"] = $user->nachname;
            }
        }
    }

    // check if login attempt succeded and get the user role
    if ($_SESSION["login"] == 1)
    {
        // if role is user goto user page
        echo '<meta http-equiv="refresh" content="100; URL=user" />';
    }else if ($_SESSION["login"] == 2)
    {
        if role is admin goto admin page
        echo '<meta http-equiv="refresh" content="100; URL=admin" />';
    }

User/Admin Page (both with identical code/error):

session_start();
if ($_SESSION["login"] == '2')
{
    //goto admin if user is admin
    echo '<meta http-equiv="refresh" content="0; URL=../admin">';
}else if ($_SESSION["login"] != '1')
{
    //goto login if user is neither role 1 or 2
    //this one triggers allways if it is your first attempt to login
    //it behaves like the session didnt start on the login page but i dont know why
    echo '<meta http-equiv="refresh" content="0; URL=../">';
}
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何实验stm32主通道和互补通道独立输出
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率
    • ¥15 TI的insta-spin例程
    • ¥15 完成下列问题完成下列问题
    • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
    • ¥15 YoloV5 第三方库的版本对照问题