1) 当需要把用户输入的内容,有可能包含单引号、双引号 、反斜线、空字元 NUL,到mysql的语句中执行时,应该把APACHE中的magic_quotes_gpc项设成On.
如果APACHE中的此项设成Off时,也可用php的函数addslashes()达到相同的目的,但这两种手段不能同时使用,否则会出现重复替换,出现错误.
样例PHP代码如下:
<?php if (get_magic_quotes_gpc()) { $content=$_POST["content"]; } else { $content=addslashes($_POST["content"]); }
当然,如果APACHE中的magic_quotes_gpc项为On,但有时又不想转义某项的特殊字符,可以使用stripslashes()去掉其中的 .
2) 过滤影响MSSQL正常运行的字符。
当需要把用户输入的内容,有可能包含单引号,到mssql的语句中执行时,应该把APACHE中的magic_quotes_sybase项设成On,此时magic_quotes_gpc项不再生效.
如果APACHE中的此项设成Off时,php中并没有合适的函数达到相同的目的,只能使用字符串替换函数来达到此目的.
样例,PHP代码:
<?php $content=str_replace("'","''"$_POST["content"]);
现在10.218.17.53上的PHP既要访问mysql又要访问mssql,APACHE中的设置不能兼顾两种数据库,所以只对mysql做了相应设置.
2. 应对用户输入包含SQL语句的一个措施。
以下两种SQL写法都比较普遍,但安全程度是不同的,当用户提交的$id='1 and 1=2 union select ...'时第一种就会显示出不该显示的数据,而第二种就相对安全些.
SQL代码:
Select * FROM article Where articleid=$id
Select * FROM article Where articleid='$id'
3. 防止用户输入的内容因包含html标签或javascript而影响页面的正常显示,可以用htmlspecialchars()过滤其中的 & " ,PHP代码:
$content = htmlspecialchars($content);
4. 当页面要显示的内容包含回车换行时,可以使用nl2br()来达到页面上换行的效果.
方法一.
<?php function chkstr($paravalue,$paratype) //过滤非法字符 { if($paratype==1) { $inputstr=str_replace("'","''",$paravalue); } elseif($paratype==2) { $inputstr=str_replace("'","",$paravalue); }//开源代码phprm.com return $inputstr; } $user1=chkstr($_GET["user"],1); $user2=chkstr($_GET["user"],2); //$user=$_GET["user"]; print "方式1-----------------<br>"; print "$user1 <br>"; print "方式2-----------------<br>"; print "$user2 <br>";
方法二.
<?php //用法:qstr($str, get_magic_quotes_gpc()) function qstr($string, $magic_quotes=false, $tag=false) { $tag_str = ''; if ($tag) $tag_str = "'"; if (!$magic_quotes) { if (strnatcmp(PHP_VERSION, '4.3.0') >= 0) { return $tag_str.mysql_real_escape_string($string).$tag_str; } $string = str_replace("'", "[url=file://\]\'[/url]" , str_replace('\', '\\', str_replace("", "[url=]\[/url]", $string))); return $tag_str.$string.$tag_str; } return $tag_str.str_replace('\"', '"', $string).$tag_str; }
教程地址:
欢迎转载!但请带上文章地址^^

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

WebStorm Mac version
Useful JavaScript development tools

Atom editor mac version download
The most popular open source editor

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software