Jump to content

Input Validation


Omzy

Recommended Posts

I've got a form with input fields and I've included the most common validation, for example strlen(), trim() and strip_tags().

 

Are there any other validation methods I should include? I am particularly trying to prevent SQL Injection attacks.

Link to comment
Share on other sites

Cheers guys. Currently I've got:

 

$query="INSERT INTO table (name) VALUES (' " .strip_tags($name)). " ')";

 

So what would be best to add to this - htmlspecialchars() or mysql_real_escape_string()?

Link to comment
Share on other sites

Well, with strip_tags() you completely remove html tags, so there's not much point in using htmlspecialchars() as well.

 

mysql_real_escape_string is almost mandatory. If you don't use it, your query will break if you have a quote in the input. (unless magic quotes is on, but we all know that's stupid and should be turned off).

 

It also prevents sql injection, and goes one step further than addslashes(). For example, If I were to put 0x27 into your input, it would convert it to a single quote ('). addslashes wouldn't pick that up, but mysql_real_escape_string would, and escape it.

Link to comment
Share on other sites

Basically the 'ID' is got from the URL construct and there is a form on the page, clicking Submit will POST that form and it gets processed in the script as:

 

if(isset($_POST['delete']))
{
$query="DELETE FROM table WHERE id='$_GET['id']'";
//run the mysql_query
}

 

So is this safe enough? All the IDs in the database are INTs, do I need to do any intval() check or anything else?

Link to comment
Share on other sites

Well, if I put this is the url: &id=';DELETE FROM `sometable` WHERE'1'='1

Your query would then look like this:

$query="DELETE FROM table WHERE id='';DELETE FROM `sometable` WHERE'1'='1'";

So someone could potentially truncate, dump or delete one of your tables. You need to run mysql_real_escape_string() on EVERYTHING being used with a database. That would escape all single quotes in that statement, so it would work fine.

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.