在登录时检查用户类型(php)

我制作了一个带有注册/登录系统的网站,我也想制作一个管理面板。我给了我的帐户管理员用户类型 (mysql),但我不知道如何在登录时检查用户类型。这是我检查登录的身份验证代码(echos 有匈牙利语文本不关心):


session_start();

$DATABASE_HOST = 'sql305.epizy.com';

$DATABASE_USER = 'epiz_25331636';

$DATABASE_PASS = 'q1SI3G8B0s';

$DATABASE_NAME = 'epiz_25331636_accounts';

$con = mysqli_connect($DATABASE_HOST, $DATABASE_USER, $DATABASE_PASS, $DATABASE_NAME);

if ( mysqli_connect_errno() ) {

    exit('Failed to connect to MySQL: ' . mysqli_connect_error());

}



if ( !isset($_POST['username'], $_POST['password']) ) {

    exit('Felhasználónév és jelszó is szükséges!');

}


if ($stmt = $con->prepare('SELECT id, password FROM accounts WHERE username = ?')) {

    $stmt->bind_param('s', $_POST['username']);

    $stmt->execute();

    $stmt->store_result();


        if ($stmt->num_rows > 0) {

    $stmt->bind_result($id, $password);

    $stmt->fetch();


    if (password_verify($_POST['password'], $password)) {

        session_regenerate_id();

        $_SESSION['loggedin'] = TRUE;

        $_SESSION['name'] = $_POST['username'];

        $_SESSION['id'] = $id;

        header('Location: home.php');

    } else {

        echo "<script type='text/javascript'>alert('Helytelen jelszó');

window.location='index.html';

</script>";


    }

} else {

    echo "<script type='text/javascript'>alert('Helytelen felhasználónév');

window.location='index.html';

</script>";

  }


    $stmt->close();

}


?>


哈士奇WWW
浏览 107回答 1
1回答

炎炎设计

假设每个用户都有一个usertype字段,其中包含管理员的“admin”,您可以执行以下操作:if ($stmt = $con->prepare('SELECT id, password, usertype FROM accounts WHERE username = ?')) {&nbsp; &nbsp; $stmt->bind_param('s', $_POST['username']);&nbsp; &nbsp; $stmt->execute();&nbsp; &nbsp; $stmt->store_result();&nbsp; &nbsp; if ($stmt->num_rows > 0) {&nbsp; &nbsp; &nbsp; &nbsp; $stmt->bind_result($id, $password, $usertype);&nbsp; &nbsp; &nbsp; &nbsp; $stmt->fetch();&nbsp; &nbsp; &nbsp; &nbsp; if (password_verify($_POST['password'], $password)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; session_regenerate_id();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['loggedin'] = TRUE;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['name'] = $_POST['username'];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $_SESSION['id'] = $id;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if($usertype == "admin"){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header('Location: adminpanel.html')&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; header('Location: home.php');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; echo "<script type='text/javascript'>alert('Helytelen jelszó');&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; window.location='index.html';&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </script>";&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }就像已经提到的那样,如果正在使用,请尽快更改凭据。
打开App,查看更多内容
随时随地看视频慕课网APP