What are some common security vulnerabilities in PHP forums and how can they be mitigated?

One common security vulnerability in PHP forums is SQL injection, where attackers can manipulate database queries to access or modify data. This can be mitigated by using prepared statements with parameterized queries to prevent user input from being executed as SQL code.

// Mitigating SQL injection vulnerability using prepared statements
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->bindParam(':username', $_POST['username']);
$stmt->execute();