可能重複:
Best way to stop SQL Injection in PHP如何讓我的PHP/mySQL註冊更安全?
我的問題是如何使註冊頁面更安全?在register.html上,用戶添加一些數據,如果在register.php中,用戶名存在於數據庫中,它告訴他使用另一個用戶名。
它對SQL注入很差嗎?另一方面,我如何確保用戶只使用字母數字字符?
register.php
$sql ="SELECT * FROM $table_name WHERE username= '$_POST[username]'";
$result = @mysql_query($sql,$connection) or die(mysql_error());
//get the number of rows in the result set
$num = mysql_num_rows($result);
//checks it see if that username already exists
if ($num != 0){
echo "<P>Sorry, that username already exists.</P>";
echo "<P><a href=\"#\" onClick=\"history.go(-1)\">Try Another Username.</a></p>";
exit;
}else{
$sql = "INSERT INTO $table_name VALUES
('$_POST[firstname]', '$_POST[lastname]', '$_POST[username]', password('$_POST[password]'), 'Users', '', '', '$pchange',
'$_POST[email]', '$default_url', '$verify', '')";
見http://stackoverflow.com/questions/60174/best-way-to-stop-sql-injection-in- php – JJJ
屬於[codereview](http://codereview.stackexchange.com/)(但不是,它非常容易被注入,我敢打賭它也容易受到XSS的攻擊)。 – Quentin