登录页面错误

时间:2017-02-25 08:36:27

标签: php forms login

我在登录页面遇到问题。当我按下登录按钮时,只有选中了“记住我”复选框,才会将我带到用户面板。如果未选中,则它将保留在登录页面上。我无法弄清楚错误。

include("Database/database.php");
session_start();
if(isset($_COOKIE['username']) and isset($_COOKIE['passcode'])) {
    $username = $_COOKIE['username'];
    $passcode = $_COOKIE['passcode'];

    $_SESSION['name'] = $username;
    $_SESSION['pwd'] = $passcode;

    header("Location: useraccount.php");   /*Checks if cookies are set then takes me directly to useraccount.php */

    } else if (isset($_POST['login'])) {
        $username = $_POST['username'];
        $userpwd = $_POST['userpwd'];
        $rem = $_POST['remember'];

        $query = "SELECT * FROM login WHERE userName = '$username' AND Password = '$userpwd'";

        $result = mysqli_query($link, $query);
        $row = mysqli_num_rows($result);

        if ($row > 0) {
            $_SESSION['name'] = $username;
            $_SESSION['pwd'] = $userpwd;

            if (isset($_POST['remember']))     /*if remember me checkbox is checked than it set cookies and it takes me to useraccount.php page. But if I donot check the check box (remember me) than I am where I am on login page. */
            {
                setcookie("username", $username, time()+60*60*7);
                setcookie("passcode", $userpwd, time()+60*60*7);
            }

            header("Location: useraccount.php");     /* It should procees to              useracocunt.php page but it stays on login.php*/
        } else {
            header("Location: login.php");      /*Used this for testing purpose if login fails it takes me to homepage which is working fine*/
        }
        mysqli_close($link);

    }
}

3 个答案:

答案 0 :(得分:1)

我找到了解决这个问题的方法。这是我的错误,因为如果我没有设置cookie,我会将标题重定向回登录页面。

  • 因此,如果您正面临与标题相关的问题,请检查您是否将其标题为该文件的正确位置“url”或
  • 必须有一些拼写错误。

答案 1 :(得分:1)

这有助于我遇到同样的问题。登录后我将其重定向到同一个登录页面。没有设置我的cookie条件

答案 2 :(得分:0)

添加if to cheek的语句保存到session.Becuase你添加会话但是,无处检查它。

    <?php
        include("Database/database.php");
        session_start();

        if(isset($_COOKIE['username']) and isset($_COOKIE['passcode']))
        {
            $username = $_COOKIE['username'];
            $passcode = $_COOKIE['passcode'];

            $_SESSION['name'] = $username;
            $_SESSION['pwd'] = $passcode;

            header("Location: useraccount.php");   /*Checks if cookies are set then takes me directly to useraccount.php */

        }

         else if(isset($_POST['login']))
        {
            $username = $_POST['username'];
            $userpwd = $_POST['userpwd'];
            $rem = $_POST['remember'];

            $query = "SELECT * FROM login WHERE userName = '$username' AND Password = '$userpwd'";

            $result = mysqli_query($link, $query);
            $row = mysqli_num_rows($result);

            if($row > 0)
            {
                $_SESSION['name'] = $username;
                $_SESSION['pwd'] = $userpwd;

                if (isset($_POST['remember']))     /*if remember me checkbox is checked than it set cookies and it takes me to useraccount.php page. But if I donot check the check box (remember me) than I am where I am on login page. */
                {
                    setcookie("username", $username, time()+60*60*7);
                    setcookie("passcode", $userpwd, time()+60*60*7);
                }

                header("Location: useraccount.php");     /* It should procees to              useracocunt.php page but it stays on login.php*/
            }

            else
            {
                header("Location: login.php");      /*Used this for testing purpose if login fails it takes me to homepage which is working fine*/
            }
            mysqli_close($link);

        } if (isset($_SESSION['name']) and isset($_SESSION['pwd'])){

header("Location: useraccount.php");     /* It should procees to              useracocunt.php page but it stays on login.php*/

}

        ?>