Home  >  Article  >  Backend Development  >  php防止sql注入简单分析,phpsql注入_PHP教程

php防止sql注入简单分析,phpsql注入_PHP教程

WBOY
WBOYOriginal
2016-07-13 10:02:491077browse

php防止sql注入简单分析,phpsql注入

本文实例分析了php防止sql注入简单方法。分享给大家供大家参考。具体如下:

这里只说一个简单的方法

防止Sql注入的方法有很多,这里要说的其实就是漏洞演练平台Dvwa里的一种方式

直接看high级别的就可以了

$id = $_GET['id']; 
$id = stripslashes($id); 
$id = mysql_real_escape_string($id); 
if (is_numeric($id)){
$getid = "SELECT first_name,last_name FROM users WHERE user_id='$id'";
$result = mysql_query($getid) or die('<pre class="brush:php;toolbar:false">'.mysql_error().'
'); $num = mysql_numrows($result);

可见它的处理方式是首先通过 stripslashes 函数删除变量中的反斜杠 \,
然后再使用函数mysql_real_escape_string 转义特殊字符就行了。
所以当我们编写类似代码的时候

$getid="SELECT first_name,last_name FROM users WHERE user_id='$id'";

我们最简单的方法是

直接将变量$id 进行stripslashes 和 mysql_real_escape_string 处理。

注意: 这里并不是说这样就安全了, 这只是其中一种方式我可没说这就安全了。 更多的还要依据实际情况进行处理。

希望本文所述对大家的php程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/969494.htmlTechArticlephp防止sql注入简单分析,phpsql注入 本文实例分析了php防止sql注入简单方法。分享给大家供大家参考。具体如下: 这里只说一个简单的方法...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn