might be a silly question nonetheless:
I'm playing around with the following code:
$a='a';
if ($_GET['a'] == $a)
echo 'true';
else
echo 'false';
Now, is there any way to send data to break the verification? Obviously the way it could've been done in an SQL injection won't go.
Just wondering how secure this way of validation is.
Thanks in advance.
EDIT:
My question was, is there anything that can be passed thorugh $_GET that could 'break' the comparison and always output 'true'.
$_GET['a']
isn't set it'd just pop a warning into your logs - Crontab 2012-04-03 20:52
If you are looking to validate that $_GET['a']
really in face equals to "a"
and nothing else, than yes, that's the code.
However, if you're expecting "a"
and only "a"
it probably shouldn't be a user input.
Validation (or sanitation), means to take whatever string they might throw at you, and make sure it's valid for whatever purpose you want it to. If it's sent to the database, pass it through mysql_escape_string()
or use prepared statements. If it's to be displayed as HTML make sure there aren't any harmful tags by using html_entities()
or strip_tags()
.
Your verification isn't very good for anything else other than saying the user has inputted "a"
. But yes, nothing other than "a"
would be able to get through.
===
) to test for the matching - Madara Uchiha 2012-04-03 21:05
Well, if you knew exactly what was coming in, you could compare without type coercion and check for an empty parameter:
$a = 'a';
if( !empty( $_GET['a'] ) && $_GET['a'] === $a )
{
//do more validation using your data model
}
else
{
//output error msg
}
You could use Prepared-Statements from the mysqli extension this already prevents every possible injection.
If you don't want to use such mysql and mysqli also have "real_escape_string"-methods which you can use in your Query when putting in Userinput
Example
$sql = "SELECT `name` FROM `example` WHERE `id` = '".mysql_real_escape_string($YOURVAR)."'";
real_escape_string method from standart mysql extension