2013-06-28 93 views
-3

我是新來的PHP,我需要幫助這個代碼,因爲我得到「HTTP-fout 500(內部服務器錯誤):」當我嘗試加載它。有人可以發佈整個好的代碼。謝謝。php登錄腳本無法加載

<?php 
session_start(); 


if(empty($_POST['username']) || empty($_POST['password'])) { 
$notifications[] = 'Login failed! Please provide a username and password.'; 
    } 

    if(count($notifications) == 0) { 
try { 
    $dbh = new PDO('mysql:dbname=####;host=####', 
'####', '####'); 

    $sql = "SELECT username, verified FROM users WHERE username = :username AND  
password = :password"; 
    $sth = $dbh->prepare($sql); 
    $sth->execute(array(
     ':username' => $_POST['username'], 
     ':password' => md5($_POST['password']) 
    )); 

    $result = $sth->fetch(PDO::FETCH_ASSOC); 

    if($result) { 
     // Set session details and redirect user to members page 
     session_regenerate_id(); 
    $_SESSION['username']=':username'; 
    $_SESSION['password']=':password'; 

     header('Location: index.php'); 
    } else { 
     $notifications[] = "Username or Password incorrect."; 
    } 
    ?> 
+0

你可能想空出你的數據庫的登錄信息... – Novocaine

+0

乍一看,你有一個沒有catch塊的try塊。您是否嘗試過從命令行運行php -l <​​您的腳本名稱>?這會告訴你是否有任何分析錯誤。 – dethtron5000

+0

請與我們分享您迄今爲止所做的任何麻煩,以便我們減少猜測。第一步是嘗試找出發生錯誤的位置(哪條線)。 – RandomSeed

回答

0

你錯過了一些花括號。不確定這是否是你問題的原因,但試試看。第三和第四線增加了設置錯誤報告中顯示,而不是記錄到文件/ syslog的當前頁面上/等

<?php 
session_start(); 
ini_set('display_errors', true); 
error_reporting(E_ALL); 

if(empty($_POST['username']) || empty($_POST['password'])) { 
    $notifications[] = 'Login failed! Please provide a username and password.'; 
} 

if(count($notifications) == 0) { 
    try { 
     $dbh = new PDO('mysql:dbname=####;host=####','####', '####'); 
     $sql = "SELECT username, verified FROM users WHERE username = :username AND password = :password"; 
     $sth = $dbh->prepare($sql); 
     $sth->execute(array(
      ':username' => $_POST['username'], 
      ':password' => md5($_POST['password']) 
     )); 

     $result = $sth->fetch(PDO::FETCH_ASSOC); 

     if($result) { 
      session_regenerate_id(); 
      $_SESSION['username']=':username'; 
      $_SESSION['password']=':password'; 

      header('Location: index.php'); 
     } else { 
      $notifications[] = "Username or Password incorrect."; 
     } 
    } 
} 
?> 
+0

我仍然收到500錯誤 –

0

使用在你的腳本的頂部以下,你的頁面將顯示所有關於什麼地方出了錯信息=)

有了這些信息別人能更輕鬆地幫助你,它會幫助你學習/發現你的錯誤

ini_set('display_errors', true); 
error_reporting(-1); 
+0

我在哪裏可以看到錯誤? –

+0

在頁面self =)上,而不是500條消息,您將得到一個包含一些錯誤細節的頁面。 –