我正在遵循指南(https://www.youtube.com/watch?v=a3Wxi-ZvUng),现在我已经完成了登录系统的这一部分,但现在不起作用了,我做了所有工作,就像教程中的家伙一样,并且我已经看了然后三遍,以确保我的代码与他的代码相同。我有它在本地服务器上运行,并在数据库中有两个用户。 (我遵循所有这些教程)
我已经在评论部分中要求解决方案,但是视频来自三年前,还没有答复。
function getLogin($conn) {
if (isset($_POST['loginSubmit'])) {
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
$sql = "SELECT * FROM user WHERE uid='$uid' AND pwd='$pwd'";
$result = $conn->query($sql);
if(mysqli_num_rows($result) > 0) {
if($row = $result->fetch_assoc()) {
$_SESSION['id'] = $row['id'];
header("Location: index.php?loginsuccess");
exit();
}
}
else {
header("Location: include.php?loginfailed");
exit();
}
}
}
function userLogout() {
if (isset($_POST['logoutSubmit'])) {
session_start();
session_destroy();
header("Location: include.php");
exit();
}
}
查看
<?php
echo "<form method='POST' action='".getLogin($conn)."'>
<input type='text' name='iud'>
<input type='password' name='pwd'>
<button type='submit' name='loginSubmit'>Login</button>
</form>";
echo "<form method='POST' action='".userLogout()."'>
<button type='submit' name='logoutSubmit'>Logout</button>
</form>";
if (isset($_SESSION['id'])) {
echo "You are logged in!";
} else {
echo "You are not logged in!";
}
?>
答案 0 :(得分:0)
尝试一下:
<?php
if (isset($_POST['logoutSubmit'])) {
session_start();
session_destroy();
header("Location: include.php");
exit();
}
if (isset($_POST['loginSubmit'])) {
$uid = $_POST['uid'];
$pwd = $_POST['pwd'];
$sql = "SELECT * FROM user WHERE uid='$uid' AND pwd='$pwd'";
$result = $conn->query($sql);
if(mysqli_num_rows($result) > 0) {
if($row = $result->fetch_assoc()) {
$_SESSION['id'] = $row['id'];
header("Location: index.php?loginsuccess");
exit();
}
}
else {
header("Location: include.php?loginfailed");
exit();
}
} ?>
<form method='POST' action="#">
<input type='text' name='iud'>
<input type='password' name='pwd'>
<button type='submit' name='loginSubmit'>Login</button>
<button type='submit' name='logoutSubmit'>Logout</button>
</form>
请记住,这具有SQL注入和纯文本密码漏洞,因此您不应将其实时发布到互联网上。