烏姆有一些事情我錯在這裏看到...
首先查詢應消毒的...
$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);
?>
因爲我是一個很好的人,我不會毀掉你的數據庫,我真的希望它不包含任何重要的東西。你*必須*學會清理你的查詢。閱讀您可以在SQL注入中找到的所有內容。 – meagar