問(wèn)題描述
我是 PHP 新手,我意識(shí)到我的數(shù)據(jù)庫(kù)連接,使用 php 表單(帶有用戶和傳遞文本輸入)是完全不安全的:
I′m new in PHP and I′ve realised that my database connection, using a php form (with user and pass text inputs) was totally unsafe:
這是有效的,但不安全:
This was working, but was unsafe:
<?php
$link=mysqli_connect('localhost','xx','xx','xx');
$sql=' SELECT * FROM usuarios
WHERE username="'.$_POST['usuario'].'"
AND pass="'.$_POST['usuario'].'"
';
$rs=mysqli_query($link,$sql);
mysqli_close($link);
?>
所以,我已經(jīng)閱讀了 mysqli_real_escape_string,并決定嘗試一下:
So, I′ve read about mysqli_real_escape_string, and decided to try it out:
<?php
$link=mysqli_connect('localhost','xx','xx','xx');
$usuario=mysqli_real_escape_string($link, $_POST["usuario"]);
$clave=mysqli_real_escape_string($link, $_POST["clave"]);
$sql=' SELECT * FROM usuarios
WHERE username="'.$usuario.'"
AND pass="'.$clave.'"
';
$rs=mysqli_query($link,$sql);
mysqli_close($link);
?>
這是正確的嗎?這是如何使用 mysqli_real_escape_string 的好例子嗎?
Is this correct? Is this a good example of how to use mysqli_real_escape_string?
推薦答案
這是正確的嗎?
是的.
這是如何使用 mysqli_real_escape_string 的好例子嗎?
Is this a good example of how to use mysqli_real_escape_string?
否
如果曾經(jīng)使用過(guò),這個(gè)函數(shù)必須被封裝到一些內(nèi)部處理中,并且永遠(yuǎn)不必從應(yīng)用程序代碼中直接調(diào)用.必須使用占位符來(lái)表示查詢中的數(shù)據(jù):
$sql='SELECT * FROM usuarios WHERE username=? AND pass=?';
然后,在處理占位符標(biāo)記時(shí),此功能可以被應(yīng)用(如果適用)但不是單獨(dú)應(yīng)用,而是沿著所有格式規(guī)則應(yīng)用.
And then, upon processing placeholder marks, this function may be applied (if applicable) but not by itself but along ALL the formatting rules.
這篇關(guān)于mysqli_real_escape_string 安全嗎?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!