What potential issue is highlighted in the PHP login script provided in the forum thread?
The potential issue highlighted in the PHP login script is the vulnerability to SQL injection attacks due to the use of unsanitized user input in the SQL query. To solve this issue, user input should be properly sanitized or prepared before being used in the SQL query to prevent SQL injection attacks.
// Sanitize user input before using it in the SQL query
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = mysqli_real_escape_string($conn, $_POST['password']);
// Prepare the SQL query using prepared statements
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ? AND password = ?");
$stmt->bind_param("ss", $username, $password);
$stmt->execute();
$result = $stmt->get_result();
// Check if there is a matching user in the database
if ($result->num_rows == 1) {
// User authenticated successfully
// Redirect to the home page or perform other actions
} else {
// User authentication failed
// Handle the error or display an error message
}
$stmt->close();
$conn->close();