我在做這個教程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