Possible Duplicate:
What’s the best method for sanitizing user input with PHP?
I know similar questions have been asked multiple times here, and I've spent some time reading all of them, but I am still not confident if my application is going to be secure...
The website I am building allows users to post things into a database, edit their own postings, search for other postings, and display postings from other users. Clearly, there is a whole bunch of user input and form data and what not, which I have to make sure is secure.
So here's how my process currently works:
- users input their data into HTML forms, which are converted into $_POST data.
- a posthandler.php file converts all $_POST data into $_SESSION data
- the $_SESSION data is converted into $this-> class variables
- the $this-> class variables are used in the mySQL query and entered into the DB
- from the DB the data is displayed back in HTML.
What measures do I have to take in which of those steps in order to insure the security of my website? (preventing code injection and XSS)
For now this is what I am thinking, please correct me if I am wrong:
- For 1. I have no control, the data is completely vulnerable. 
- Therefore in 2. I have to check if the data matches certain patterns, which I expect (check if it's numeric, check if it's a date, check if it's letters only, check if it's a value from an array which I provide, use regular expressions, ... ) This way I can prevent for example if I expect a value between 1 and 10, and someone says "'--DROP table;" instead that I have already made sure these fields are secure. Of course fields, which are not as restrictive like text boxes where users can type messages are still vulnerable. 
- For step 3. there shouldn't be anything I need to do, if I did everything correct in the step before, right? An attacker shouldn't be able to change the SESSION data or the class variables, unless there is a major security breach on the server side, for which my hosting provider should be responsible, correct? 
- In step 4. where I do my query, I need to escape the data with mysqli_real_escape_string() to make sure the data is not handled as a command. 
- And in step 5. I plan on converting every special character into their respective HTML code snippet. All - "are going to be converted to- "for example.
Are there any additional security measures /filters I should apply within each step?
What if I make a mistake in step 2.) or I am unable to find a regular expression for a field. I just convert from $_POST to $_SESSION without further validation. Will it save my butt to use mysqli_real_escape_string() during the query and to filter all characters before they are displayed in the browser?
I look forward to your insight!
 
    