views:

74

answers:

2

I am not a professional web developer but I love web development as a hobby.. I am a student and I was asked to make a departmental event web site. I need to create HTML forms for user registration. I am aware of XSS Scripting attack and SQL Injection. But don't know how they exactly work. I am using PHP and MySQL on server side.

I am taking in consideration:

  • Not to take empty values.
  • Verify that a number field is composed of integer values only.
  • Email ID verification. etc..

All this I am doing using RegEx check - both with javascript and PHP

Now my questions are:

  • What are all characters which I should not allow into my database?
  • Will it be okay if I convert < and > with their &lt; and &gt; alternatives?
  • What else should I consider while taking the input?

I don't want to restrict users from inputting symbols which are harmless. So is there any particular set of character which I need to filter before storing the values into the database, so I can properly write regex checks for my form fields?

I have searched in google but was not able to find a proper answer. :(

+1  A: 

To prevent SQL injection, you should use the language's escape function. For PHP, that's mysql_real_escape_string. Or, better yet, use PDO to restrict what users can put into the DB.

The HTML injection/XSS attack is different; you can store raw HTML in the database without issue, but before displaying any HTML originating with the user, call htmlspecialcharacters on it to prevent it from being interpreted by the client's web browser.

Do not code your own custom checks. You will miss something.

Borealid
and there would be gain in performance if htmlspecialcharacters will be used once before inputing data into the database, rather than using it while output :)
Idlecool
@idlecool but its not good design. You should not temper with data before you actually use actually it in a view.
Iznogood
@Idlecool: Yes, but you don't know the context in which the data will be used. What if you wanted to parse the HTML users sent and use it somewhere without displaying it? Then you'd have to un-specialchars it.
Borealid
oh! yes! i get it... so i have to use mysql_real_escape_string before inputing data while using htmlspecialcharacters after it. while i am reading about PDO too. :)
Idlecool
A: 

As for usename, this should do the job:

if (preg_match('/^[a-z\d_\-<>]{5,20}$/i', $username)) {
    echo "Your username is ok."; 
    // Note that you still have to do something with <>
    // Though, personally I'd advise sticking to /^[a-z\d_]{5,20}$/i
} else {
    echo "Wrong username format.";
}

Source

As for SQL injection, use mysql_real_escape_string or mysqli_real_escape_string on everything you enter into the DB

Robus