This example describes a quick way to find malicious code in a database in PHP. Share to everyone for your reference. Specifically as follows:
The database is entered with malicious code and you have to be careful to clean up your database to keep it safe. With one of the following super handy features, you can quickly clear the database for malicious code.
function CleanInput ($input) {
$search = array (
' @]*?>.* @si ',//Strip out JavaScript
' @<[\/\!] *? [^<>]*?> @si ',//Strip out HTML tags
' @
]*?>.*?
@siU ',//strip style tags properly
' @@ '//Strip multi-line comments
);
$output = Preg_replace ($search, ', $input);
return $output;
}
function sanitize ($input) {
if (Is_array ($input)) {
foreach ($input as $var => $val) {
$output [$var] = Sanitize ($val);
}
else {
if (GET_MAGIC_QUOTES_GPC ()) {
$input = stripslashes ($input);
}
$input = CleanInput ($input);
$output = mysql_real_escape_string ($input);
}
return $output;
}
Usage:
$bad _string = "hi! It ' s a good day! ";
$good _string = sanitize ($bad _string);
$good _string Returns "hi! It\ ' s a good day! "
Also use for getting post/get variables
$_post = sanitize ($_post);
$_get = sanitize ($_get);
I hope this article will help you with your PHP program design.