The mysql_real_escape_string () function escapes special characters in the string used in the SQL statement. The following characters are affected:
| The code is as follows |
Copy Code |
X00
N
R
'
"
X1a |
If successful, the function returns the escaped string. If it fails, it returns false.
Easy to use the following function, you can effectively filter.
| The code is as follows |
Copy Code |
|
function Safe ($s) {//Safe filter functions
if (GET_MAGIC_QUOTES_GPC ()) {$s =stripslashes ($s);}
$s =mysql_real_escape_string ($s);
return $s;
} |
or join the conn public connection file so that you don't have to modify the code:
| The code is as follows |
Copy Code |
|
if (GET_MAGIC_QUOTES_GPC ()) {
$_request = Array_map (' stripslashes ', $_request); }
$_request = Array_map (' mysql_real_escape_string ', $_request); |
mysql_real_escape_string anti-SQL injection is just some of the basics, and if you need more powerful anti-SQL injection I can refer to the following methods
The first is the security settings for the server, mainly the Php+mysql security settings and the Linux Host Security settings. To prevent Php+mysql injection, first set the MAGIC_QUOTES_GPC to On,display_errors set to off, and if the ID type, we use Intval () to convert it to an integer type, such as code:
| The code is as follows |
Copy Code |
$id =intval ($id);
mysql_query= "Select *from example where articieid= ' $id '", or write: mysql_query ("select * from article where articleid=.") Intval ($id). "") |
If it is a character type, use Addslashes () to filter, and then filter "%" and "_" such as:
| The code is as follows |
Copy Code |
$search =addslashes ($search);
$search =str_replace ("_", "_", $search);
$search =str_replace ("%", "%", $search);
|
Of course, you can add PHP generic anti-injection code:
/*************************
PHP Universal Anti-injection security code
Description
Determines whether the passed variable contains illegal characters
such as $_post, $_get
Function:
Anti-injection
| The code is as follows |
Copy Code |
**************************/
Illegal characters to filter
$ArrFiltrate =array ("'", ";", "union");
The URL to jump after an error is not filled in the default previous page
$STRGOURL = "";
Whether the value in the array exists
function Funstringexist ($StrFiltrate, $ArrFiltrate) {
foreach ($ArrFiltrate as $key => $value) {
if (eregi ($value, $StrFiltrate)) {
return true;
}
}
return false;
}
Merging $_post and $_get
if (function_exists (Array_merge)) {
$ArrPostAndGet =array_merge ($HTTP _post_vars, $HTTP _get_vars);
}else{
foreach ($HTTP _post_vars as $key => $value) {
$ArrPostAndGet []= $value;
}
foreach ($HTTP _get_vars as $key => $value) {
$ArrPostAndGet []= $value;
}
}
Verify Start
foreach ($ArrPostAndGet as $key => $value) {
if (Funstringexist ($value, $ArrFiltrate)) {
echo "Alert (/" NEEAO hint, illegal character/");
if (empty ($STRGOURL)) {
echo "History.go (-1);";
}else{
echo "window.location=/" ". $StrGoUrl." /”;”;
}
Exit
}
}
?>
|
/*************************
Save As Checkpostandget.php
Then add include ("checkpostandget.php") in front of each PHP file;
**************************/
The
also takes MD5 encryption of the administrator username and password, which effectively prevents the injection of PHP.
There are also servers and MySQL to tighten up on some security precautions.
for Linux server security settings:
Encrypt passwords, use the "/usr/sbin/authconfig" tool to open the shadow feature of passwords, and encrypt password.
Prohibit access to important files, enter the Linux command interface, and enter at the prompt:
#chmod 600/etc/inetd.conf//change file attributes to
#chattr +i /etc/ inetd.conf //Guarantee file owner is root
#chattr –i /etc/inetd.conf //restrictions on changes to the file
prohibit any user from changing to root by using the SU command
Add the following two lines to the beginning of the SU configuration file, the/etc/pam.d/directory:
auth sufficient& nbsp; /lib/security/pam_rootok.so Debug
auth required /lib/security/pam_whell.so Group=wheel
Delete all special accounts
#userdel LP, and so on delete the user
#groupdel LP, etc. delete group
prohibit Suid/sgid programs that are not used
#find/-ty PE f (-perm-04000 -o–perm-02000)-execls–lg {};