我正在嘗試做單頁登錄不斷得到500服務器錯誤
我創建一個登錄表單的單頁成員的區域。
登錄後,用戶應該看到的唯一一件事情(在這個階段)是一個註銷按鈕。
什麼錯誤
每當有數據庫匹配(例如用戶名是正確的,或兩者的用戶名和密碼是否正確)有一個500 Server Error
。
刷新頁面,不管如果密碼匹配,用戶獲取登錄。
當用戶單擊註銷鏈接,也就是一個500 Server Error
。
守則
<?php
session_start();
if(isset($_GET['logout'])&&$_GET['logout']==='true'){
logout();
header('location: ./');
}
if(isset($_POST['submit'])) {
$email = $_POST['email'];
$password = $_POST['password'];
$hostname = 'REDACTED';
$username = 'REDACTED';
$password = 'REDACTED';
$dbname = 'REDACTED';
try {
$dbh = new PDO("mysql:host=$hostname;dbname=$dbname", $username, $password);
}
catch(PDOException $e){
echo($e->getMessage());
}
$stmt=$dbh->prepare('SELECT `password`, `id` FROM `tap_users` WHERE `email`=?');
$stmt->execute(array($email));
if($stmt->rowCount()==0){
//incorrect username
header('location: ./?error=user');
exit();
}
$userData = $stmt->fetch(PDO::FETCH_ASSOC);
if($password != $userData['password']) {
//incorrect password
header('location: ./?error=pass');
exit();
} else {
validateUser($userData['id']);
header('location: ./');
}
}
?>
<head></head>
<body>
<?php
if(!isLoggedIn()) {
show_login();
exit();
}
?>
<a href="?logout=true">Logout</a>
</body>
</html>
<?php
//Show login form
function show_login(){
echo '<form method="post">
<input type="text" name="email" placeholder="Email Address" />
<input type="password" name="password" placeholder="Password" />
<input type="submit" value="Submit" name="submit" />
</form>
</body>
</html>';
}
//Check if a user is logged in
function isLoggedIn(){
if(isset($_SESSION['valid']) && $_SESSION['valid'])
return true;
return false;
}
//validate the user
function validateUser($userid){
//this is a security measure
session_regenerate_id();
$_SESSION['valid'] = 1;
$_SESSION['userid'] = $userid;
}
//logout the user
function logout(){
//destroy all of the session variables
$_SESSION = array();
session_destroy();
}
?>
你看過PHP錯誤日誌嗎?我也會從上面的代碼片段中刪除數據庫用戶名和密碼.. – ajtrichards 2015-01-15 16:43:23
你知道如何使用瀏覽器開發工具上的'Network'選項卡嗎?如果您點擊其中一個請求,應該有一個「響應」選項卡,它會告訴您錯誤。在Firefox或Chrome中:'F12-> Network-> 500 Request-> Response'(注意可能需要在「Network」選項卡中顯示任何內容之前重新加載頁面) – 2015-01-15 16:44:38
@ajtrichards我的託管服務提供商不提供對PHP的訪問錯誤日誌。當我打開PHP錯誤報告時沒有錯誤。 – Ben 2015-01-15 16:45:56