我在做这个教程How to create a login system in PHP准备的语句执行,但我不能获取结果
我试图使用IF语句来检查是否设置会话ID。当前允许用户登录并回显当前会话ID。当我故意输入一个错误的密码时,代码应该回显一条消息,但它什么也不做。另外,我无法使用注销按钮来终止会话。
这是我的登录页面
<?php
session_start();
//include 'dbh.php';
//probably do not need this can write connection in this file too
$conn = new PDO('mysql:host=localhost;dbname=testtable', 'root', '');
$conn->setAttribute(PDO::ATTR_DEFAULT_FETCH_MODE, PDO::FETCH_OBJ);
if(!$conn){
die("Connection is fubar yo!!! ");
}
$uid = $_REQUEST['uid'];
$pwd = $_REQUEST['pwd'];
$sql = "SELECT * from user WHERE uid='$uid' AND pwd='$pwd'";
$result = $conn->prepare($sql);
$result->execute();
if(!$row = $result->fetch(PDO::FETCH_ASSOC)){
echo("Your username or Password is wrong");
} else {
echo("hey man..it works");
$_SESSION['id'] = $row['id'];
}
header("Location: fakelogin.php");
?>
这是我的注销页面。
<?php
session_start();
session_destroy();
header("Location: fakelogin.php");
?>
这里是包含登录,注册和注销按钮的主页面。
<?php
session_start();
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<title>Look ma, no hands!</title>
</head>
<body>
<form action="login.php" method="POST">
<input type="text" name="uid" placeholder="Enter username"><br>
<input type="password" name="pwd" placeholder="Enter password"><br>
<button type="submit" id="login">LOGIN</button>
</form>
<?php
echo("testing");
if(isset($_SESSION['id'])){
echo($_SESSION['id']);
} else {
echo("You are not logged in");
}
?>
<br><br><br>
<form action="signup.php" method="POST">
<input type="text" name="first" placeholder="Enter first name"><br>
<input type="text" name="last" placeholder="Enter last name"><br>
<input type="text" name="uid" placeholder="Enter username"><br>
<input type="password" name="pwd" placeholder="Enter password"><br>
<button type="submit" id="sbt">SIGN UP HERE YO!</button>
</form>
<br><br><br>
<form>
<button action="logout.php">LOG OUT</button>
</form>
</body>
</html>
您应该在测试时注释掉头部重定向。 – josephting
我把它拿出来了。我没有看到变化。为了记录,为什么在测试时将其删除是一个好主意? – VK1
您将无法看到结果,因为您只会看到'fakelogin.php'或任何页面,如果您离开重定向进程完成后重定向到。加载您的登录页面后,您是否重定向到fakelogin.php?或者你留在登录页面? – josephting