What potential security risks are present in the current PHP script, and how can they be mitigated?
The current PHP script is vulnerable to SQL injection attacks as it directly concatenates user input into the SQL query. To mitigate this risk, you should use prepared statements with parameterized queries to prevent malicious SQL injection.
// Original vulnerable code
$user_input = $_POST['username'];
$query = "SELECT * FROM users WHERE username='$user_input'";
$result = mysqli_query($connection, $query);
// Mitigated code using prepared statements
$user_input = $_POST['username'];
$query = "SELECT * FROM users WHERE username=?";
$stmt = $connection->prepare($query);
$stmt->bind_param("s", $user_input);
$stmt->execute();
$result = $stmt->get_result();
Keywords
Related Questions
- What are the potential pitfalls of not normalizing a database when dealing with multiple entries for the same attribute in PHP?
- What is the Unix format for the current server time and how can 1 hour be added or subtracted from it in PHP?
- In what scenarios is it recommended to use JavaScript and AJAX instead of PHP for real-time output on a web page?