What potential security risks are involved in using $_GET to retrieve user information for displaying on a page?

Using $_GET to retrieve user information for displaying on a page can pose security risks such as SQL injection attacks or cross-site scripting (XSS) attacks. To mitigate these risks, it is important to properly sanitize and validate the input data before using it in your application.

$user_id = isset($_GET['user_id']) ? intval($_GET['user_id']) : 0;

// Validate user_id to ensure it is a positive integer
if ($user_id <= 0) {
    // Handle invalid input (e.g. redirect to an error page)
}

// Sanitize user_id before using it in a query
$user_id = mysqli_real_escape_string($connection, $user_id);

// Use $user_id in your query to retrieve user information