Reputation: 123
i am trying to redirect to a url that is passed as a param from another page.
<?PHP $location = urlencode($_GET['url']); ?>
<script>
window.location = ("<?PHP echo $location; ?>");
</script
the problem is instead of redirecting to the external site, it thinks the encoded URL string is a file name relative to the current directory.
how can i make it redirect to the right place?
Upvotes: 0
Views: 143
Reputation: 1590
First of all, you should verify the passed URL is in fact a URL and not someone trying to inject JS into your site.
if(filter_var($_GET['url'], FILTER_VALIDATE_URL) !== false) {
header("Location: " . filter_var($_GET['url'], FILTER_VALIDATE_URL));
}
//Alternatively using JS
if(filter_var($_GET['url'], FILTER_VALIDATE_URL) !== false) {
echo('<script>
window.location = ("' . filter_var($_GET['url'], FILTER_VALIDATE_URL) . '");
</script>');
}
Upvotes: 0
Reputation: 218
you should add http://
before your url
Edit
I noticed you mentioned you get a string like this: http%3A%2F%2Fexample.com%3Faaa%3Dbbb
So you can decode that string:
$location = urldecode($_GET['url']); ?>
Upvotes: 1
Reputation: 905
I would suggest using a php header direction method:
<?php
header('Location: '.$_GET['url']);
?>
See: http://php.net/manual/en/function.header.php ...for more details.
Upvotes: 0
Reputation: 7851
Remove the urlencode()
and ensure the url is starting with http://
<?PHP $location = $_GET['url']; ?>
<script>
window.location = ("<?PHP echo $location; ?>");
</script>
Or:
<?php
$location = $_GET['url'];
header("Location: $location");
?>
Upvotes: 0