Are there any potential security risks associated with using user input in the URL to fetch images?

Using user input directly in the URL to fetch images can pose security risks such as directory traversal attacks or injection of malicious code. To mitigate these risks, it is important to sanitize and validate user input before using it in the URL to fetch images. This can be done by ensuring that the input is properly validated and sanitized to prevent any malicious code or unauthorized access.

$user_input = $_GET['image'];

// Sanitize and validate user input
$allowed_images = ['image1.jpg', 'image2.jpg', 'image3.jpg'];
if (in_array($user_input, $allowed_images)) {
    $image_url = 'images/' . $user_input;
    echo '<img src="' . $image_url . '" alt="User Image">';
} else {
    echo 'Invalid image selected';
}