我正在开发一个基本的登录系统,该系统允许注册/登录将用户存储在 MySQL 数据库中。我有它,所以人们可以注册,它将用户名和散列密码存储在数据库中。当有人登录时,它会根据用户名和密码是否匹配显示成功消息或错误消息。
我的问题在于登录功能。我需要创建用户会话并将它们重定向到站点的仅登录部分,而不是显示当前存在的成功消息。我不确定该怎么做...
这是我的代码:
登录/注册功能
function login($username, $password) {
$userpass = sha1($password);
$result = mysqli_query($con, "SELECT * FROM members WHERE username='$username' AND password='$userpass'");
while($row = mysqli_fetch_array($result)) {
$success = true;
}
if($success == true) {
echo 'Success!';
} else {
echo '<div class="alert alert-danger">Oops! It looks like your username and/or password are incorrect. Please try again.</div>';
}
} // END LOGIN FUNCTION
function register($username, $password) {
$userpass = sha1($password);
// Check if Username Exists
$result = mysqli_query($con,"SELECT * FROM members WHERE username='$username'");
while($row = mysqli_fetch_array($result)) {
$userexist = 1;
}
if($userexist > 0) {
echo '<div class="alert alert-danger">Sorry, it looks like that username is already taken.</div>';
} else {
$newmember = "INSERT INTO members SET username='$username', password='$userpass'";
if(mysqli_query($con,$newmember)) {
echo '<div class="alert alert-success">Congrats! You can now log in using your username and password</div>';
}
}
}