sois wrote:If i pass a variable through the URL like this:
www.sois.com/home.php?playerID=5
What is the best way to use the playerID variable? Should I use the variable direclty in my code? For example "select * from DB where PLAYER = '$playerID' " or should I use GET or POST methods?
Also, can someone add something to the URL to hack/destroy my site? Something like this?
www.sois.com/home.php?playerID=5;drop DB;... etc.
Is that possible or am I safe? Thanks all.
Hey, I created this query string sanitizer for my apps....it is a work in progress and you are limited to a-zA-Z0-9, underscore and dashes.
I use it like
$GVars = qStrSanatize( $_GET );
Now in your example above you would do something like
$playerID = (int)$_GET["playerID"];
What I do with this function is
$GVars = qStrSanatize( $_GET );
$playerID = $GVars["playerID"];
we verify if it's numeric or text and we also convert all entities, it seems pretty safe and I use it all the time.
Also keep in mind that you need to handle it when you do get an error where it returns all your Get...post requests back false
I don't use this function for Post vars only Query strings in the url and I limit my self to using only the chars listed.
FUNCTION qStrSanatize( $GetRequestArr )
{
#Error Code series 200, E200 is reserved for Query String Errors
GLOBAL $site; # for main site url must match http_host exactly
//$ValidArr = ARRAY();
IF ( !empty( $_SERVER['QUERY_STRING'] ) ) {
//ECHO "Hello ";
IF ( is_array( $GetRequestArr ) ) {
FOREACH ( $GetRequestArr as $Key => $Value ) {
# If request not my server then return false for all $_Get
IF ( 'http://'.$_SERVER['HTTP_HOST'].'/' === $site['url_main'] ) {
IF ( is_numeric( $Value ) AND eregi( "[^0-9]", $Value ) ) {
$ValidArr[$Key] = (int)$Value;
$ValidArr['E200'] .= "Numeric key = ". $Key.' Value = ' . $Value;
}ELSE{
#We are expecting a string here so return false if it's not valid
$TempV = trim( strip_tags( html_entity_decode( $Value ) ) );
$TempV = stripslashes( $TempV );
$TempV = str_replace(chr(13), "", $TempV); #chr(13) is a carriage RETURN
$TempV = str_replace(chr(10),"",$TempV); #chr(10) line feed
#Match a-zA-Z0-9 - and _ only if string anything else return 0 for key value
IF ( !preg_match('/[^a-zA-Z\d_-]/i', $TempV) ) {
$ValidArr[$Key] = $TempV;
}ELSE{
$ValidArr[$Key] = 0;
}
$ValidArr['E200'] .= "Valid String key = ". $Key.' Value = ' . $ValidArr[$Key];
}
}ELSE{
$ValidArr[$Key] = 0;
$ValidArr['E200'] .= "Severs did not match, reseting all keys to 0";
}
}
}ELSE{
IF ( 'http://'.$_SERVER['HTTP_HOST'].'/' === $site['url_main'] ) {
IF ( is_numeric( $GetRequestArr ) AND eregi( "[^0-9]", $GetRequestArr ) ) {
$ValidArr = (int)$GetRequestArr;
}ELSE{
#We are expecting a string here so return false if it's not valid
$TempV = trim( strip_tags( html_entity_decode( $GetRequestArr ) ) );
$TempV = stripslashes( $TempV );
$TempV = str_replace(chr(13), "", $TempV); #chr(13) is a carriage RETURN
$TempV = str_replace(chr(10),"",$TempV); #chr(10) line feed
#Match a-zA-Z0-9 - and _ only if string anything else return 0 for key value
IF ( !preg_match('/[^a-z\d_-]/i', $TempV) ) {
$ValidArr = $TempV;
}ELSE{
$ValidArr = 0;
}
}
}ELSE{
$ValidArr = 0;
}
}
}ELSE{
$ValidArr['E200'] = "No Query String Detected.";
$ValidArr[0] = 0;
}
RETURN $ValidArr;
}