What are some common security risks associated with URL input in PHP?
One common security risk associated with URL input in PHP is the possibility of SQL injection attacks if the input is not properly sanitized. To mitigate this risk, it is important to use prepared statements when interacting with a database to prevent malicious SQL queries from being executed.
// Using prepared statements to prevent SQL injection
$mysqli = new mysqli("localhost", "username", "password", "database");
// Check if input is set and not empty
if(isset($_GET['user_id']) && !empty($_GET['user_id'])) {
$user_id = $_GET['user_id'];
// Prepare a SQL statement using a prepared statement
$stmt = $mysqli->prepare("SELECT * FROM users WHERE user_id = ?");
$stmt->bind_param("i", $user_id);
$stmt->execute();
// Process the results
$result = $stmt->get_result();
while ($row = $result->fetch_assoc()) {
// Do something with the data
}
// Close the statement
$stmt->close();
}
// Close the connection
$mysqli->close();