How can SQL-Injection and XSS attacks be prevented when using user input in PHP?
SQL-Injection attacks can be prevented by using prepared statements and parameterized queries in PHP. XSS attacks can be prevented by sanitizing and validating user input before displaying it on a webpage.
// SQL-Injection prevention using prepared statements
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->bindParam(':username', $username);
$stmt->execute();
// XSS prevention by sanitizing user input
$username = htmlspecialchars($_POST['username'], ENT_QUOTES, 'UTF-8');
echo $username;
Keywords
Related Questions
- How can PHP be used to filter out duplicate entries in a MySQL database query based on a specific column?
- What are some common mistakes or pitfalls to avoid when implementing a Newsscript in PHP?
- What are the best practices for validating the length of a string in PHP, especially when using preg_match?