Goal of the Tutorial
This tutorial aims to equip you with the knowledge and skills to prevent common security vulnerabilities such as Cross-Site Scripting (XSS) and SQL Injection in your web applications. We will focus on using PHP to sanitize user input and consequently prevent these attacks.
Learning Outcomes
By the end of this tutorial, you will be able to:
Prerequisites
Basic knowledge of PHP and SQL is recommended but not strictly necessary.
XSS is a type of injection attack where malicious scripts are injected into trusted websites. To prevent XSS attacks, always escape user input. PHP provides the htmlspecialchars()
function to escape special characters.
SQL Injection is a code injection technique that attackers can use to exploit vulnerabilities in a web application's database. To prevent this, use prepared statements and parameterized queries.
Preventing XSS
<?php
// User input
$user_input = "<script>malicious code</script>";
// Sanitize user input
$sanitized_input = htmlspecialchars($user_input, ENT_QUOTES, 'UTF-8');
// Output: <script>malicious code</script>
echo $sanitized_input;
?>
This code snippet uses htmlspecialchars()
to convert special characters to their HTML entities, thus preventing any injected script from executing.
Preventing SQL Injection
<?php
// Database connection
$db = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
// User input
$id = $_GET['id'];
// Prepared statement
$stmt = $db->prepare("SELECT * FROM users WHERE id = :id");
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->execute();
// Fetch and display the result
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo $row['username']."<br/>";
}
?>
In this example, the prepare()
method is used to create a prepared statement. bindParam()
binds the user input to the SQL statement, ensuring it is treated as a string value, not part of the SQL command.
In this tutorial, we have covered:
htmlspecialchars()
For further learning, explore other types of web application vulnerabilities and how to prevent them. OWASP (Open Web Application Security Project) is a great resource for this.
Exercise 1
Write a PHP script to sanitize the following user input to prevent XSS:
$user_input = "<img src='http://fake-evil-url.com/evil-script.js'>";
Solution
<?php
$user_input = "<img src='http://fake-evil-url.com/evil-script.js'>";
$sanitized_input = htmlspecialchars($user_input, ENT_QUOTES, 'UTF-8');
echo $sanitized_input;
?>
Exercise 2
Assume you have a form where users can update their email address. Write a PHP script using prepared statements to safely update a user's email in the database.
Solution
<?php
$db = new PDO('mysql:host=localhost;dbname=test', 'username', 'password');
// User input
$id = $_GET['id'];
$email = $_GET['email'];
$stmt = $db->prepare("UPDATE users SET email = :email WHERE id = :id");
$stmt->bindParam(':id', $id, PDO::PARAM_INT);
$stmt->bindParam(':email', $email, PDO::PARAM_STR);
$stmt->execute();
?>
Keep practicing and always be mindful of security when developing your web applications. Happy coding!