我是PHP新手,不確定我的代碼是否會阻止它受到任何SQL攻擊。而且,我對我爲什麼不能登錄感到困惑。它不會給我任何錯誤。此代碼是否可以防止SQL注入攻擊併爲何不起作用?
<?php
$host="localhost"; // Host name
$username="*****"; // Mysql username
$password="****"; // Mysql password
$db_name="***"; // Database name
$tbl_name="electoral"; // Table name
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
// username and password sent from form
$username=$_POST['username'];
$password=$_POST['password'];
// To protect MySQL injection (more detail about MySQL injection)
$username = stripslashes($username);
$password = stripslashes($password);
$username = mysql_real_escape_string($username);
$password = mysql_real_escape_string($password);
$sql="SELECT * FROM $tbl_name WHERE username='$username' and password= md5('$password')";
$result=mysql_query($sql);
// Mysql_num_row is counting table row
$count=mysql_num_rows($result);
// If result matched $myusername and $mypassword, table row must be 1 row
if($count==1){
// Register $myusername, $mypassword and redirect to file "_____"
session_register("username");
session_register("password");
header("location:electoralcommission.php");
}
?>
有什麼想法嗎?謝謝
你是寫這個還是你從某處複製過? – 2012-02-19 23:46:03
複製零碎 – 2012-02-19 23:46:36
你的代碼在'<?php'之前是否真的有空格?如果是這樣,會話呼叫將失敗。 – 2012-02-19 23:48:00