Forum Moderators: coopster
function Clean($string){
if (get_magic_quotes_gpc())
{
return $string;
}
else
{
return mysql_real_escape_string($string);
}
$string = trim($string);
$string = safeEscapeString($string);
$string = htmlentities($string);
return $string;
}
foreach($_POST as $name1 => $value){
$_POST[$name1] = Clean($value);
}
foreach($_GET as $name1 => $value){
$_GET[$name1] = Clean($value);
}
foreach($_COOKIE as $name1 => $value){
$_COOKIE[$name1] = Clean($value);
}
foreach($_REQUEST as $name1 => $value){
$_REQUEST[$name1] = Clean($value);
}
I think I would do this:
function clean($s) {
if (get_magic_quotes_gpc())
$s = stripslashes($s);
return mysql_real_escape_string($s)
} And you can use this like you said,
clean($_GET['input']);
So magic_quotes is a last resort not your first.
Mysql_real_escape_string is the better method; also doesnt lead to having to strip slashes on all of your output.
$_POST = array_map [uk2.php.net]('Clean',$_POST);
If its recursive you`ll need a custom function.
dc
function clean($s, &$link) {
if (get_magic_quotes_gpc())
$s = stripslashes($s);
return mysql_real_escape_string($s, $link)
}
This should work properly :)
I got message from my host that my variable was expoited by a spammer.
If you want to save it on db try something like this:
# clean GET!
# ----------
$_SERVER['REQUEST_URI'] = str_replace("'", '', $_SERVER['REQUEST_URI']);
spammers are the worst >:O
function clean_sql($varia) {
// REMOVE BLANK SPACE
$varia=rtrim($varia);
$varia=ltrim($varia);
// REMOVE HTML TAGS
$varia=strip_tags($varia);
// CHECK IF GET_MAGIC_QUOTE IS ON AND STRIP SLASH ACCORDINGLY
if (get_magic_quotes_gpc()) {$varia = stripslashes($varia);}
// CLEAN SPECIFICALLY FOR MYSQL QUERY
$varia = mysql_escape_string($varia);
return $varia;}
If you variable is not used for Mysql query but it is shown in your HTML webpage, then a simple function with htmlentities would be better
function clean($varia) {
$varia=rtrim($varia);
$varia=ltrim($varia);
// CHANGE SPECIAL CHARACTERS INTO HTML ENTITIES
$varia=htmlentities($varia, ENT_QUOTES);
$varia=str_replace("\n","<br>",$varia);
if (get_magic_quotes_gpc()) {$varia = stripslashes($varia);}
return $varia;}
Note that I work in UTF-8 environment. If you have many issues with special characters, make sure that you are in a free-hassle utf-8 environment (configure default charset in ini, httpd.conf, apache, mysql, etc.).
@NeilsPHP
First, you must be sure that text entered into your form have the correct encoding
<form accept-charset="utf-8" ...
Then, if you know what you variable are (integer, string, email address, etc.), you must always check them before any else. Below, rowstart is obligatory a number and and if it failed, then assign a default value.
if(isset($_GET["rowstart"]) AND $_GET["rowstart"]!="") {$rowstart=$_GET["rowstart"];
if(!numeric($rowstart)) {$rowstart="0";}} else {$rowstart="0";}
Regarding the file upload issues, just read [webmasterworld.com...] . You'll see that it is slightly different because your file variables are store in an array - $_FILES["uploaded_file"]
FromBelgium said
"I want to clean a variable of a query (no MySQL involved). For example link.php?input=value"