What are some potential security risks when processing data from external sources in PHP?
One potential security risk when processing data from external sources in PHP is the possibility of SQL injection attacks. To prevent this, it is important to sanitize and validate any user input before using it in SQL queries. This can be done by using prepared statements with parameterized queries.
// Example of using prepared statements to prevent SQL injection
// Assuming $conn is the database connection object
// Sanitize and validate user input
$user_input = $_POST['user_input'];
$user_input = filter_var($user_input, FILTER_SANITIZE_STRING);
// Prepare the SQL statement
$stmt = $conn->prepare("SELECT * FROM users WHERE username = ?");
$stmt->bind_param("s", $user_input);
// Execute the query
$stmt->execute();
// Process the results
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
// Process the data
}
// Close the statement and connection
$stmt->close();
$conn->close();