Rumah > pembangunan bahagian belakang > tutorial php > PHP防止SQL注入的方法(1)

PHP防止SQL注入的方法(1)

WBOY
Lepaskan: 2016-07-29 09:15:00
asal
1008 orang telah melayarinya

(1)mysql_real_escape_string – 转义 SQL 语句中使用的字符串中的特殊字符,并考虑到连接的当前字符集
使用方法如下:

<code>$sql = "<span><span>select</span><span>count</span>(*) <span>as</span> ctr <span>from</span> users <span>where</span> username =<span>'".mysql_real_escape_string($username)."'</span><span>and</span> password=<span>'". mysql_real_escape_string($pw)."'</span> limit <span>1</span><span>";</span></span></code>
Salin selepas log masuk

使用

<code><span><span>mysql_real_escape_string</span><span>()</span></span></code>
Salin selepas log masuk

作为用户输入的包装器,就可以避免用户输入中的任何恶意 SQL 注入。
(2) 打开magic_quotes_gpc来防止SQL注入
php.ini中有一个设置:magic_quotes_gpc = Off
  这个默认是关闭的,如果它打开后将自动把用户提交对sql的查询进行转换,
  比如把 ’ 转为 \’等,对于防止sql注射有重大作用。
如果magic_quotes_gpc=Off,则使用addslashes()函数
(3)自定义函数

<code><span><span>function</span><span>inject_check</span><span>(<span>$sql_str</span>)</span> {</span><span>return</span> eregi(<span>'select|insert|and|or|update|delete|\'|\/\*|\*|\.\.\/|\.\/|union|into|load_file|outfile'</span>, <span>$sql_str</span>);
} 

<span><span>function</span><span>verify_id</span><span>(<span>$id</span>=null)</span> {</span><span>if</span>(!<span>$id</span>) {
        <span>exit</span>(<span>'没有提交参数!'</span>); 
    } <span>elseif</span>(inject_check(<span>$id</span>)) { 
        <span>exit</span>(<span>'提交的参数非法!'</span>);
    } <span>elseif</span>(!is_numeric(<span>$id</span>)) { 
        <span>exit</span>(<span>'提交的参数非法!'</span>); 
    } 
    <span>$id</span> = intval(<span>$id</span>); 

    <span>return</span><span>$id</span>; 
} 


<span><span>function</span><span>str_check</span><span>( <span>$str</span> )</span> {</span><span>if</span>(!get_magic_quotes_gpc()) { 
        <span>$str</span> = addslashes(<span>$str</span>); <span>// 进行过滤 </span>
    } 
    <span>$str</span> = str_replace(<span>"_"</span>, <span>"\_"</span>, <span>$str</span>); 
    <span>$str</span> = str_replace(<span>"%"</span>, <span>"\%"</span>, <span>$str</span>); 

   <span>return</span><span>$str</span>; 
} 


<span><span>function</span><span>post_check</span><span>(<span>$post</span>)</span> {</span><span>if</span>(!get_magic_quotes_gpc()) { 
        <span>$post</span> = addslashes(<span>$post</span>);
    } 
    <span>$post</span> = str_replace(<span>"_"</span>, <span>"\_"</span>, <span>$post</span>); 
    <span>$post</span> = str_replace(<span>"%"</span>, <span>"\%"</span>, <span>$post</span>); 
    <span>$post</span> = nl2br(<span>$post</span>); 
    <span>$post</span> = htmlspecialchars(<span>$post</span>); 

    <span>return</span><span>$post</span>; 
}</code>
Salin selepas log masuk

http://www.phpddt.com/php/228.html

以上就介绍了PHP防止SQL注入的方法(1),包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Label berkaitan:
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan