2011-09-07 163 views
0

我正在爲我的業務設置一個博客類型頁面。全新的MySQL和PHP。設置這個登錄系統。出於某種原因,不知道爲什麼登錄正在下降。假設檢查錯誤,然後通過php返回'好',如果電子郵件和密碼是正確的。如果PHP返回好,那麼它就會重定向到博客頁面。Jquery/PHP ajax登錄系統

處理這個幾個月需要絕望的幫助。謝謝。 這裏是跟隨jQuery的php代碼。

鏈接到測試網站在這裏。 test.toddprod.com/login 真的會很感激幫助。 感謝

<?php 
#fake mysql connection first 
DEFINE ('DB_USER','usernamegoeshere'); 
DEFINE ('DB_PASSWORD','passwordhere'); 
DEFINE ('DB_HOST','hostnamehere'); 
DEFINE ('DB_NAME','andtheotherthinghere'); 

$dbc = mysql_connect (DB_HOST, DB_USER, DB_PASSWORD) or die ('Could not connect to MySQL'); 

$db = mysql_select_db(DB_NAME, $dbc) or die('Could not select database.'.mysql_error()); 


$e = $_POST['email']; 
$pass = $_POST['pass']; 
$q = 'SELECT user_id from toddprod where email="'.$e.'" and pass= SHA1("'.$pass.'")'; 
$r = mysql_query($db, $q); 
if(mysql_num_rows($r)==1){ 
    setcookie ('user_id', $r); 
    setcookie ('email', '$e'); 
    setcookie ('logged-in', 'true'); 
    echo 'good'; 
    } 
else if (mysql_num_rows($r)==0) { 
    echo 'Your '.$e.' with password '.$pass; 
}; 
mysql_close ($db); 
?> 
+0

因爲我是一個很好的人,我不會毀掉你的數據庫,我真的希望它不包含任何重要的東西。你*必須*學會清理你的查詢。閱讀您可以在SQL注入中找到的所有內容。 – meagar

回答

0

首先第一件事情,你MUST清理用戶與mysql_real_escape_string()輸入:

$e = mysql_real_escape_string ($_POST['email']); 
$pass = mysql_real_escape_string ($_POST['pass']); 

閱讀上SQL injection一點,你會很高興你沒有。

至於主要問題,您能否提供更多的上下文?你如何檢查用戶是否登錄?

1

烏姆有一些事情我錯在這裏看到...

首先查詢應消毒的...

$email = mysql_real_escape_string ($_POST['email']); // escape the email 
$pass = SHA1(mysql_real_escape_string ($_POST['pass'])); // escape and encrypt the pass 

// now you can put it into the query safely 
$query = "SELECT user_id from toddprod where email = '$email' and pass = '$pass' "; 

接下來你執行查詢錯了,mysql_query函數接受兩個參數,即查詢和數據庫連接。你傳遞錯誤的參數,你傳遞的查詢和mysql_select_db函數的結果只是一個布爾值。因此,您必須將$dbc而不是$db納入該查詢中,並且即使這樣您仍然以錯誤的順序傳遞參數。查詢首先進行,而不是連接。因此,它應該是...

$result = mysql_query($query, $dbc); 

接下來你想設置爲從mysql_query功能作爲Cookie返回值,但該值是一種資源,而不是你需要的用戶ID。你必須從這個資源中實際讀取值。

$row = mysql_fetch_array($result); 
$userid = $row["user_id"]; 
setcookie('user_id', $userid); 

繼續前進......當你設置電子郵件的cookie,你必須在單引號中的變量,所以cookie將實際上包含$e,而不是實際的電子郵件,因爲單引號存儲字符串litterly(不解析變量)。所以你應該使用雙引號,或者根本不使用引號。所以下面的任何一個是好的...

setcookie('email', "$e"); 
setcookie('email', $e); 

最後但並非最不重要的,你不應該在你的if語句的結束分號,再次,你需要通過不連接的數據庫-selection導致進入mysql_close功能,所以它應該是

mysql_close($dbc); 

在那裏,希望這可以讓你的地方,嘗試這些變化,如果問題仍然存在,我很樂意進一步提供幫助。

這裏有鏈接,這將幫助你:

http://www.php.net/manual/en/function.mysql-query.php

http://www.php.net/manual/en/function.mysql-fetch-array.php

http://www.php.net/manual/en/function.mysql-real-escape-string.php

編輯:

在這裏,我根據已定的代碼我發現的問題。試一試,我無法測試它,所以它可能會有一些小的語法錯誤在這裏和那裏,但它應該給你一些比較。同樣對於將來,我建議你在語義上/正確地命名變量,以便其他人更容易拾取,並且還可以避免讓你感到困惑,例如將$ db而不是$ dbc傳遞給幾個函數。

<?php 
    // keep the function names in lowercase, no reason, just looks better to me 
define('DB_USER', 'usernamegoeshere'); 
define('DB_PASSWORD', 'passwordhere'); 
define('DB_HOST', 'hostnamehere'); 
define('DB_NAME', 'andtheotherthinghere'); 

    // connect to the mysql server 
$conn = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD) or die ('Could not connect to MySQL'); 
    // select the database, you don't need to store the result, it just returns true or false 
mysql_select_db(DB_NAME, $conn) or die('Could not select database.' .mysql_error()); 

    // escape the input 
$email = mysql_real_escape_string($_POST['email']); 
$pass = sha1(mysql_real_escape_string($_POST['pass'])); 

    // create the query 
$query = "SELECT user_id FROM toddprod WHERE email = '$email' AND pass = '$pass'"; 

    // execute the query 
$result = mysql_query($query, $conn); 
$usercount = mysql_num_rows($result); 

if($usercount == 1){ 
    // read the results and get the user_id 
    $row = mysql_fetch_array($result); 
    $userid = $row['user_id']; 

    // set the cookies 
    setcookie('user_id', $userid); 
    setcookie('email', $email); 
    setcookie('logged-in', 'true'); 

    // echo success message 
    echo 'good'; 
}elseif($usercount == 0) { 
    echo "You're $email with password $pass"; 
} 
mysql_close($conn); 
?>