强制用户在首次登录时更改密码

时间:2018-01-23 04:19:26

标签: php html login

我正在创建一个登录模块。管理员和用户使用相同的页面进行登录。根据凭据,页面将相应地指向管理员和用户页面。管理员登录后,管理员会添加用户并为其分配密码。使用此密码,用户登录。我的项目一直在这里工作。问题是,我必须编写一个脚本来强制用户在第一次登录时更改密码。

为此,我创建了一个登录表,其中包含用户名,密码,fname。

我是php的初学者。因此卡住了。

请帮帮我。

的login.php

 {
  $_SESSION['login'] = "OK";
  $_SESSION['username'] = $user;
  $_SESSION['password'] = $pass;
  header('Location: adminpage.php');

}

else

{
  $_SESSION['login'] = "";

  header('Location: wrong.html');    
}

adminpage.php

 if($_SESSION['username'] == 'admin')
{
  echo "<p align=right><a href='login.php'>Log out</a></p>";     
  echo "<p><a href='create_user.php'>Create a new user</a></p>";
  echo "<p><a href='reports.php'>Reports</a></p>";

}
  elseif
  {
      header('Location: userpage.php');
  }

2 个答案:

答案 0 :(得分:0)

# Error: AttributeError: file <maya console> line 7: 'unicode' object has no attribute 'attr' # 中添加一个新列,例如。

login table

设置为任何默认值(我建议为-1),当用户首次登录后更改密码时,将其更改为1.检查此值是否为您的用户的登录请求。稍后您还可以使用此列来阻止用户status TINYINT(1) NOT NULL DEFAULT -1

答案 1 :(得分:0)

我有同样的问题,并通过以下方式解决了该问题。

在脚本的一部分中,根据输入的密码验证哈希密码,我将这部分代码放在if语句中

这是完整的代码:

// Prepare a select statement
$sql = "SELECT id, username, password, admin, status FROM loginusers WHERE username = ?";

if($stmt = $mysqli->prepare($sql)){
    // Bind variables to the prepared statement as parameters
    $stmt->bind_param("s", $param_username);

    // Set parameters
    $param_username = $username;

    // Attempt to execute the prepared statement
    if($stmt->execute()){
        // Store result
        $stmt->store_result();

        // Check if username exists, if yes then verify password
        if($stmt->num_rows == 1){                    
            // Bind result variables
            $stmt->bind_result($id, $username, $hashed_password, $admin, $status);
            if($stmt->fetch()){
                if(password_verify($password, $hashed_password)){
                    // Password is correct, so start a new session
                    session_destroy();
                    session_start();

                    // Store data in session variables and cookies
                    $_SESSION["loggedin"] = true;
                    $_SESSION["id"] = $id;
                    $_SESSION["username"] = $username;
                    $_SESSION["admin"] = $admin;
                    setcookie("admin", $admin, time()+ 86400 * 30,"/");
                    setcookie("username", $username, time()+ 86400 * 30,"/");

                        // Redirect user to forced password change page
                        if($status == '0'){
                        // Redirect to first password change page
                        header("location: /login/firstpassword.php");
                        } else {
                        header("location: /index.php");
                    } else {
                        // Display an error message if password is not valid
                        $password_err = "Wrong password.";
                    }
                } else {
                    $password_err = "Error with database connection!";
                }
            } else{
                // Display an error message if username doesn't exist
                $username_err = "No account with this username.";
            }
        } else{
            echo "Oops, something is wrong. Contact the website admin.";
        }

        // Close statement
        $stmt->close();
    }

这是迫使用户进入密码更改页面的部分:

// Redirect user to forced password change page
if($status == '0'){
// Redirect to first password change page
header("location: /login/firstpassword.php");
} else {
header("location: /index.php");

除了我先前的回答外,您还必须将“状态”设置为0以外的任何值,以使用户在每次后续登录时都跳过强制密码更改页面。

我在“ firstpassword.php”页面上通过使用另一个SQL INSERT查询将“状态”设置为1来做到这一点。因此,用户只获得一次密码更改页面。

希望这会有所帮助!

相关问题