我在測試項目上工作,試圖瞭解更多關於PHP和MySQL什麼沒有得到NULL結果。所以我決定爲我的測試網站構建一個認證引擎,但是我遇到了這個問題。PHP/MySQL的驗證腳本查詢
基本上,如果我跑......
SELECT * FROM users WHERE name="tester" and passwd=md5('pass');
從mysql控制檯,我得到...
+--------+---------------+----------------------------------+
| name | email | passwd |
+--------+---------------+----------------------------------+
| tester | [email protected] | 1a1dc91c907325c69271ddf0c944bc72 |
+--------+---------------+----------------------------------+
1 row in set (0.00 sec)
然而,當我發出相同的查詢(至少我認爲它從我的PHP代碼相同)(見下文),我似乎得到一個空的結果,從而驗證失敗。
<?php
include 'db_connect.php';
$username = $_POST['username'];
$passwd = $_POST['passwd'];
// To protect MySQL injection (more detail about MySQL injection)
//$username = stripslashes($username);
//$passwd = stripslashes($passwd);
//$username = mysql_real_escape_string($username);
//$passwd = mysql_real_escape_string($passwd);
//Encrypted password
$secpasswd = md5($passwd);
$sql="SELECT * FROM users WHERE name='$username' and passwd = $secpasswd";
$result=mysql_query($dbconnect,$sql);
var_dump($result);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
//var_dump($count);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "login_success.php"
session_register("username");
session_register("passwd");
header("location:login_success.php");
}
else {
echo "Wrong Username or Password";
}
ob_end_flush();
?>
從什麼古代手稿你收到'session_register'的事情? –
加密單引號passwd - '和passwd ='$ secpasswd'「'注意這是使用過時的代碼,並且不安全/開放給SQL注入 – Sean
**不要使用md5 **來散列密碼!太不安全 **不使用mysql擴展**。它已被棄用,使用'mysqli'或'PDO'。 –