PHP anti-injection FunctionCode
In PHPProgramIn this case, we all pay more attention to website security. How does PHP defend against injection?
The addslaches () function that comes with PHP itself is a little weak and cannot be too reassuring.
Now I want to share with you a simple method:
Create a new file and save it as checkpostandget. php
Add include ("checkpostandget. php") before each PHP file.
<? PHP
/*************************
Note:
Determines whether the passed variable contains invalid characters.
Such as $ _ post and $ _ Get
Function:
Anti-Injection
**************************/
// Invalid characters to be filtered. The characters to be filtered can also be added.
$ Arrfiltrate = array ("'", ";", "union ");
// The URL to be redirected after an error occurs. If this parameter is left blank, the previous page is displayed by default.
$ 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;
}
// Merge $ _ 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;
}
}
// Verification starts
Foreach ($ arrpostandget as $ key => $ value ){
If (funstringexist ($ value, $ arrfiltrate )){
Echo "<script language =" JavaScript "> alert (" invalid character "); </SCRIPT> ";
If (empty ($ strgourl )){
Echo "<script language =" JavaScript "> history. Go (-1); </SCRIPT> ";
} Else {
Echo "<script language =" JavaScript "> window. Location =" ". $ strgourl." "; </SCRIPT> ";
}
Exit;
}
}
?>