What are some potential security risks associated with using $_GET variables in PHP?

Using $_GET variables in PHP can pose security risks such as SQL injection and cross-site scripting attacks if the input is not properly sanitized. To mitigate these risks, always validate and sanitize user input before using it in your code. One way to do this is by using functions like filter_input() or htmlentities() to clean the input data.

// Sanitize the input data from $_GET
$user_id = filter_input(INPUT_GET, 'user_id', FILTER_SANITIZE_NUMBER_INT);

// Use the sanitized input in your code
$query = "SELECT * FROM users WHERE id = $user_id";
$result = mysqli_query($conn, $query);