I lost the first version to a web search (new tab, dammit!) so here’s take two.
1. Getting the values
You can use one of the two following sets of code to retrieve a value:
// safely retrieve a value without filter_input
$email = '';
if (isset($_GET['email'])) {
$email = $_GET['email'];
}
// filter_input can be used from PHP 5.2 onwards
$email = filter_input(INPUT_GET, 'email');
The benefit of using filter_input is that you can also apply filters to the values. For example, with an email address there is the FILTER_SANITIZE_EMAIL filter.
$email = filter_input(INPUT_GET, 'email', FILTER_SANITIZE_EMAIL);
2. Magic quotes
None of the above yet protects your code from potentially malicious input. Up to PHP 5.3 it’s magic quotes that attempted to provide the protection. That was found to be less than adequate, with mysql_real_escape_string or prepared statements providing better protection, so as of PHP 5.3 the magic quotes are officially deprecated. In PHP 6.0 they won’t exist at all. This means changing your mindset so that the code you write now, will have a better chance to be issue-free later on.
You need to approach things with the assumption that magic quotes are no longer there. If they just-so-happen to be enabled, you need to remove the added slashes from the values, so that you don’t run the risk of double-escaping the values.
if (get_magic_quotes_gpc()) {
$email = stripslashes($email);
}
There are also many other ways to disable magic quotes, my favourite being to disable them completely at the server, but if your code is going to be run in an unknown environment, you can apply added protection so that it still works as-per-normal, or even dies with an appropriate error message.
3. Handling values
The values that you now have must still be considered to be untrusted, and potentially dangerous. When passed to the database they may contain attempted SQL Injection code, and when passed to the web page they may contain XSS code. Your code needs to now treat them as untrusted values, as they came from an untrusted source, the user. This is not to say that they will contain bad values. It’s only to say that the potential for bad values still exists.
There are some ways to protect against bad values when getting values in, but the only way to provide proper protection is to make sure that the values are safe when they are going out, whether that be to the database, the web page, or other places like email, XML, files, url, etc.
4. Output to database
Sending values to the database is fraught with issues, but if you ensure that your database values are escaped only once, you should be safe. The appropriate ways to do that are to use mysql_real_escape_string at the database query itself, as in the example on the mysql_real_escape_string documentation page, or by using prepared statements with for example, [url=“http://www.php.net/manual/en/mysqli.prepare.php”]mysqli_prepare.
5. Output to the page
As you were saying before, htmlentities is useful for outputting values to the page.
echo 'An email has been sent to you at ' .
'<strong>' . htmlentities($email) . '</strong>';
There is a lot more to be said about thess topics, but it’s kinda late for me now (1am), and the topic has been well covered here before, such as:
[list][]magic_quotes_gpc - how to handle in PHP 5.3 and 6.0
[]How do I eliminate the \ from MySQL returned data
[*]html entities?[/list]
We might even be encouraged some day to put together a proper reference about all of this.