What are the potential security risks of passing a variable URL to an iframe in PHP?
Passing a variable URL to an iframe in PHP can pose security risks if the URL is not properly sanitized or validated. This can lead to potential XSS attacks or the loading of malicious content. To mitigate these risks, it is important to validate the URL input and only allow trusted sources to be passed to the iframe.
<?php
// Sanitize and validate the URL before passing it to the iframe
$trusted_url = "https://example.com";
$user_input = $_GET['url'];
if ($user_input === $trusted_url) {
$url = $user_input;
} else {
$url = $trusted_url;
}
echo '<iframe src="' . $url . '"></iframe>';
?>