2015-01-15 73 views
1

我正在嘗試做單頁登錄不斷得到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(); 
} 
?> 
+4

你看過PHP錯誤日誌嗎?我也會從上面的代碼片段中刪除數據庫用戶名和密碼.. – ajtrichards 2015-01-15 16:43:23

+2

你知道如何使用瀏覽器開發工具上的'Network'選項卡嗎?如果您點擊其中一個請求,應該有一個「響應」選項卡,它會告訴您錯誤。在Firefox或Chrome中:'F12-> Network-> 500 Request-> Response'(注意可能需要在「Network」選項卡中顯示任何內容之前重新加載頁面) – 2015-01-15 16:44:38

+0

@ajtrichards我的託管服務提供商不提供對PHP的訪問錯誤日誌。當我打開PHP錯誤報告時沒有錯誤。 – Ben 2015-01-15 16:45:56

回答

1

你在做什麼,如果是用戶登錄是正確的,那麼你將會重定向到標題(「位置:./」);也許你沒有index.php或目錄索引在你的apache配置中關閉。

要做的一件事它更改爲

頭( '位置:的index.php');

它會奏效。

這與php或mysql無關,它是服務器配置錯誤。

+0

這是沿着正確的路線......從位置:./?xxx'改變成位置:index.php?xxx,它現在起作用。 – Ben 2015-01-15 16:55:45

0

我通過運行你的代碼:online php code validator語法似乎沒有任何問題。該問題可能來自文件的權限錯誤。該文件是否具有用戶的執行權限。在你的ftp中確保權限設置爲755.這應該允許每個人都讀取和執行php代碼。如果這不是問題,請聯繫您的主機並要求提供錯誤,他們應該能夠向您提供這些信息。另一種選擇是在PHP中創建自己的錯誤日誌記錄。您可以查看this old stack overflow post on this,以繼續使您的所有錯誤對單獨的文件可見。