标头重定向不起作用,用户检查不更新sql表

时间:2017-09-04 10:31:17

标签: php mysql

我的PHP文件中存在重定向问题,该文件在用户登录时检查用户数据。

我相信,问题可能出在login_check if语句部分。我检查了其他部分,并且:

  • 包含db.php数据是正确的。
  • 当我没有填写其中一个字段时,用户名/密码if语句会返回echo。
  • "用户信息验证db"似乎是正确的,该表被称为"用户"
  • 如果故意输入错误的密码,最后的else语句也会返回。
  • 我用Google搜索并三重检查了标题重定向语句,并确定它的语法正确(我已经玩过了)

我注意到该列" last_login"没有使用now()值更新。这就是为什么我认为问题出在这个领域,但我无法弄清楚。

PHP版本: 5.6.30

这是php文件:

<?
    /* Check User Script */
    session_start();  // Start Session

    include 'db.php';
    // Conver to simple variables
    $username = $_POST['username'];
    $password = $_POST['password'];

    if((!$username) || (!$password)){
        echo "Please enter ALL of the information! <br />";
        include 'login_form.html';
        exit();
    }

    // Convert password to md5 hash
    $password = md5($password);

    // check if the user info validates the db
    $sql = mysql_query("SELECT * FROM users WHERE username='$username' AND password='$password' AND activated='1'");
    $login_check = mysql_num_rows($sql);

    if($login_check > 0){
        while($row = mysql_fetch_array($sql)){
        foreach( $row AS $key => $val ){
            $$key = stripslashes( $val );
        }
            // Register some session variables!
            session_register('first_name');
            $_SESSION['first_name'] = $first_name;
            session_register('last_name');
            $_SESSION['last_name'] = $last_name;
            session_register('email_address');
            $_SESSION['email_address'] = $email_address;
            session_register('special_user');
            $_SESSION['user_level'] = $user_level;

            mysql_query("UPDATE users SET last_login=now() WHERE userid='$userid'");

            header("Location: login_success.php");
        }
    } 

    else {
        echo "You could not be logged in! Either the username and password do not match or you have not validated your membership!<br />
        Please try again!<br />";
        include 'login_form.html';
    }
    ?>

任何想法都会受到赞赏。

2 个答案:

答案 0 :(得分:0)

我冒昧地更改了您的代码,向您展示了PDO中的代码,并且您UPDATE语句也发生了变化。

注意: Link到支持的时区列表

<?
    /* Check User Script */
    session_start();  // Start Session

    include 'db.php';
    // Conver to simple variables
    $username = $_POST['username'];
    $password = $_POST['password'];

    //Set time
    date_default_timezone_set("America/Adak");//Set the correct time zone
    $now = date("Y-m-d H:i:s");

    if((!$username) || (!$password)){
        echo "Please enter ALL of the information! <br />";
        include 'login_form.html';
        exit();
    }

    // Convert password to md5 hash
    $password = md5($password);

    // check if the user info validates the db
    $sql = ("SELECT * FROM users WHERE username=:username AND password= :password AND activated='1'");
    $login_check = $dbConnect -> prepare($sql);
    $login_check -> bindParam(':username',$username);
    $login_check -> bindParam(':password',$password);
    $login_check -> execute();

    $rowCount = $login_check -> rowCount();

    if($rowCount > 0){
        while($row = $login_check -> fetch(PDO::FETCH_ASSOC)){
        foreach( $row AS $key => $val ){
            $$key = stripslashes( $val );
        }
            // Register some session variables!
            $_SESSION['first_name'] = $first_name;            
            $_SESSION['last_name'] = $last_name;
            $_SESSION['email_address'] = $email_address;
            $_SESSION['user_level'] = $user_level;

            $upstmt=("UPDATE users SET last_login=:now WHERE userid= :userid");
            $query = $dbConnect -> prepare($upstmet);
            $query -> bindPara(':userid', $userid);
            $query -> bindPara(':now', $now);
            $query -> execute();

            header("Location: login_success.php");
        }
    } 

    else {
        echo "You could not be logged in! Either the username and password do not match or you have not validated your membership!<br />
        Please try again!<br />";
        include 'login_form.html';
    }
    ?>

答案 1 :(得分:0)

快速更新,当我删除4 session_register();行:

CustomJS

运行checkuser.php(此文件的名称)脚本,然后将我重定向到login_success.php页面。这是session_register('');由于折旧导致它。运行后,它还更新了表'users'中需要更新的内容(last_login数据)。

不过,我应该检查一下我的文件并更新到mySQLi