我正在开发一个基本的登录系统,允许注册/登录将用户存储在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>';
}
}
}
答案 0 :(得分:3)
这样想
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) {
$_SESSION['username']= $username;
//redirect to home page
} 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
答案 1 :(得分:0)
这在很大程度上取决于您构建其余代码的方式。如果您只是在构建HTML页面并在其中添加PHP函数,那么您可以在代码的各个位置使用这么简单的东西:
$loggedIn = login($username, $password)
然后你需要让登录返回true或false。然后,您的路由结构可以根据$ loggedIn做出不同的决策,您的页面可以显示不同的布局和内容。
这在编程风格方面不是一种很好的风格,但它可能会给你一个想法。如果您添加有关如何构建代码的更多详细信息,则应获得更好的答案
答案 2 :(得分:0)
session_start();
if($_SERVER["REQUEST_METHOD"] == "POST") {
$myusername=addslashes($_POST['username']);
$mypassword=addslashes($_POST['password']);
$sql="SELECT id FROM admin WHERE username='$myusername' and passcode='$mypassword'";
$result=mysql_query($sql);
$row=mysql_fetch_array($result);
$active=$row['active'];
$count=mysql_num_rows($result);
if($count==1) {
session_register("myusername");
$_SESSION['login_user']=$myusername;
header("location: welcome.php");
}
else {
$error="Your Login Name or
Password is invalid";
}
}
答案 3 :(得分:0)
为了给您Usman Allam答案一个变体,我认为简单地检查一下mysqli_num_rows($ result)是否> = 1会容易得多
function login($username, $password) {
$userpass = sha1($password);
$result = mysqli_query($con, "SELECT * FROM members WHERE username='$username' AND password='$userpass'");
if(mysqli_num_rows($result) >= 1) {
$_SESSION['username'] = $username;
//redirect to home page
} 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