Jump to content

Escape get values


Drummin

Recommended Posts

Hello, I was wondering if I need to escape all get values.  I often use a $_GET variable as in mypage.php?id=variable to selecting records to view etc.  I usually convert this to a variable to be used in a WHERE statement.

IF ($_GET['id']){
$id=$_GET['id'];
}

But what if someone tried to view all records

resulted in all content page data being displayed somehow. Or better yet, if visiting

resulted in all content being deleted. 

 

Is that even possible in the in the context of a MySQL WHERE statement?  Seems like the MySQL statement wouldn't be structured correctly and wouldn't work.

I use mysqli_real_escape_string" on posted content but should I also escape all GET input?

Link to comment
Share on other sites

All string data that you put into a query must be escaped. All numerical data that you put into a query must be validated as a number or cast as a number.

 

In the case of your id value, you probably have a query something like -

 

SELECT * FROM your_table WHERE id = $id

 

If you don't validate/cast $id as a number in a query like that, it is possible to inject sql into that query using a hexadecimal encoded string (usually a UNION statement that outputs all the data in the table) that has absolutely no quotes in it so that escaping the data would have no affect on the injected sql. However, casting the value as a number would truncate the hexadecimal encoded string and prevent the sql injection.

 

Php's mysql_query function specifically doesn't support multiple queries separated by ; (because too many people don't escape/validate data being put into a query statement.)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.