1

I'm creating a website that relies on $_GET to function correctly. It works like this: http://www.example.com/show?q=14&i=48 q and i are IDs on MySQL server that must be fetched using mysql_query() and the like. Parameters are always integers.

If someones type the url without any parameters both PHP and MySQL yells errors. PHP for undefined variable(s) and MySQL for incorrect syntax.

What i've tried so far doesn't works i always get "Error";

if (is_int($_GET['q']) AND is_int($_GET['i']))
{
 echo "All good.";
}
else
{
 echo "Error.";
}

What am i doing wrong here.

Thanks, atno

3 Answers 3

2

That's because you're checking the type of the variable and not its content. Anything coming from a query string via $_GET is considered a string, so you should use ctype_digit() instead if is_int(), and also checking if the values are present with empty().

Sign up to request clarification or add additional context in comments.

2 Comments

+1, my thoughts exactly. ctype_digit() or even doing $_GET['i'] = (int)$_GET['i'];
Casting also works, but I'd rather reject bad input than fix it
2

You should use is_numeric instead of is_int.
is_int returns false for numeric strings.

Downside is that the is_numeric function also returns true for numbers like "1.5". If you don't want that, you can check the function here

Comments

0

You should try the isset($_GET[q])

if(isset($_GET[q]) && isset($_GET[i])){

}

2 Comments

isset() isn't the right function to solve it because as kemp said everything in $_GET considered a string. So example.com/show?q=&i= returns "All good."
According to the question, the case on your example does not happen. He says, "If someones type the url without any parameters both PHP and MySQL yells errors." In your example there exist two parameters. My partial solution only helps on the PHP side, but not on the MySQL side.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.