如果用户名和密码正确,它将重定向到 profile.php。我在 profile.php 中使用 cookie 来分隔用户。是否合适?如果没有,请告诉我如何编码。如果可能的话我不想使用 get 方法。
<?php
include('config.php');
$cookie = isset($_COOKIE['cookie']);
if($cookie) { header('Location: profile.php'); }
else {
echo '<link rel="stylesheet" href="../css/user.css">
<link rel="stylesheet" href="../css/all.css">
<div id="center"><div id="login-area">Login Here<br><br><form method="post">
<i class="fa fa-user"></i>
<input type="text" id="usr-pass" autocomplete="off" placeholder="Username" name="user"><hr><br>
<i class="fa fa-lock"></i>
<input type="password" id="usr-pass" autocomplete="off" placeholder="Password" name="pass"><hr><br>
<input type="submit" id="login" value="Login" name="login">
</form></div></div>
';
if(isset($_POST['login'])) {
if(!empty($_POST['user']) && !empty($_POST['pass'])) {
$user = $_POST['user'];
$pass = $_POST['pass'];
$query = 'SELECT password FROM user WHERE username = ?';
$stmt = $conn -> prepare($query);
$stmt -> bind_param('s', $user);
$stmt -> execute();
$stmt -> store_result();
if($stmt -> num_rows > 0) {
$stmt -> bind_result($password);
$stmt -> fetch();
if($password == $pass) {
setcookie('cookie', $user);
header('Location: profile.php');
} else { header('Location: login.php'); } //echo 'wrong password';
} else { header('Location: login.php'); } //echo 'account not exist';
} else { header('Location: login.php'); } //echo 'input both username & password';
}
}
?>
慕标5832272