2012-08-02 24 views
0

你好,我對PHP很陌生,所以不要打擾我,但我無法弄清楚這個代碼有什麼問題。所以基本上我有幾種形式的輸出,但只要我用mysql做任何事情(即使只是連接和斷開!),它也不會允許我做任何類型的輸出。它也不會讓我重定向。Mysql調用會停止所有形式的輸出php

我試圖把mysql連接和斷開連接代碼之間的所有代碼都取出來,但它並沒有幫助解決任何問題,但是,只要我註釋掉mysql連接代碼,我的所有輸出和重定向就可以工作!我試圖建立一個簡單的登錄腳本,從其他地方的表單獲取電子郵件和密碼。我很想解決這個問題,所以我可以弄清楚它的其餘部分是否有效。我知道'header'在echo之後不起作用;回聲和文件寫入將不會在那裏,只要我可以確保這是工作。任何幫助,將不勝感激!謝謝!

<?php 
/* 
    Login.php searches for the email address given by loginPage.php in the data base. 
    If the email is found and the password given by loginPage.php matches that stored 
    in the data base, a session will begin and the client will be redirected to the 
    main page. 
    *** INCOMPLETE *** 
*/ 
echo "HELLO!"; 
$email = $_POST["email"]; 
$password = $_POST["password"]; 

$errorLog = fopen("login.txt", "w");  
    fwrite($errorLog, "***Sesion started***"); 
$mysql_id = mysql_connect("localhost", "root", "12131"); 
if (!$mysql_id) 
    { 
     die('Could not connect: ' . mysql_error()); 
    } 
mysql_select_db('informationStation', $mysql_id); 

$results = mysql_query("SELECT password FROM Personals WHERE email = '" . $email . "';", $mysql_id); 

if($results != null && $password == mysql_fetch_array($result)) 
    { 
     $redirect = 'Location: http://127.0.1.1/main.php'; 
    } 
else 
    { 
     $redirect = 'Location: http://127.0.1.1/loginPage.php'; 
    { 
mysql_close($mysql_id); 
fwrite($errorLog, "result: " . $results); 
fwrite($errorLog, "redirect: " . $redirect); 
fclose($errorLog); 
header($redirect); 
?> 
+2

檢查你的服務器/ php錯誤日誌,看看是否有什麼崩潰,並打開所有的PHP調試選項:display_error,error_log等...不要在黑暗中工作,打開所有可用的燈。 – 2012-08-02 15:38:58

+0

告訴我們你寫的代碼,也許我們可以幫你 – 2012-08-02 15:39:53

回答

0

試試這個,讓你開始..

$results = mysql_query("SELECT password FROM Personals WHERE email = '" . $email . "'", $mysql_id) or die(mysql_error()); 

但你還需要了解SQL注入。

而且你不應該在你的數據庫中存儲密碼而不用醃製和散列它們。

而且你還需要使用數組語法從您的結果訪問數據...

+0

哇,謝謝你的建議!唯一的過濾是要放在這個登錄是Java腳本。我什至沒有考慮SQL注入!我將確保在我的更高版本中包含一種方法來阻止該問題! – user1571856 2012-08-02 16:11:08

+0

此外,您建議的代碼似乎無能爲力。問題在於,我們已經對die()進行了嘗試,即使在代碼中的任何位置打開mysql連接時,即使die()也不會顯示任何內容。 – user1571856 2012-08-02 16:22:29

0
$mysql = mysql_connect("localhost", "root", "12131") or die('Could not connect: ' . mysql_error()); 
mysql_select_db('informationStation', $mysql); 


function loged($email, $password) { 
    $result = mysql_query("SELECT id FROM Personals WHERE email = '" . $email . "' AND password='" . $password . "'"); 
    if(mysql_num_rows($result) != 1) 
     return false; 
    return true; 
} 


if(loged(mysql_real_escape_string($email), md5($password))) { 
    header('Location: http://127.0.1.1/mainPage.php'); 
    exit; 
} 

header('Location: http://127.0.1.1/loginPage.php'); 

在這個例子中,你需要使用MD5加密方法存儲用戶密碼(尋找其他更安全的加密方法)。

此外,我們已經逃脫了針對sql注入的電子郵件地址。

我已經創建了一個函數,可以在您每次查看用戶是否在其中時都可以調用該函數。

請注意,這不是一個完整的登錄腳本。您還需要創建一個登錄功能,您必須爲每個用戶啓動一個新會話。

+0

感謝您的建議,我打算改變我的看法,但是當我發現我無法使用任何mysql代碼的頭部時,它會阻止我的進度。也感謝加密方法的想法,我對加密一無所知,所以這給了我一個開始的好地方。 – user1571856 2012-08-02 16:16:30