I want a link on my site to redirect like such...
$offer = $_GET['url'];
if ($offer == '1'){
header("Location: http://site1.com");
}
if ($offer == '2'){
header("Location: http://site2.com");
}
This is functional, but is there a better way to do this rather than having 50 if statements?
Assuming that your sites aren't really site1.com
and site2.com
(I'd be impressed if they were), this is probably a good way to approach the problem:
$sites = array(
'1' => 'http://www.google.com/',
'2' => 'http://www.bing.com/'
);
if (isset($sites[$_GET['url']])) {
header('Location: ' . $sites[$_GET['url']]);
die();
} else {
die('Sorry, this does not exist.');
}
This would of course also allow you to do things such as accepting non-numeric values in ?url=
.
header("Location: http://site{$url}.com"); //success!!
or
header("Location: http://site" . $url . ".com"); //success!!
But please do not rely on user (client) input like that.
Validate it a little before making the header or you will have issues...
As long as they're in this format you can use:
$offer = $_GET['url'];
header("Location: http://site".$offer.".com");
Might want to check the $offer as an integer $offer = intval($offer)
Use a table on db, search for id and use a single header() or an array $redirect[option] header("Location: "$redirect[option]);
Instead of passing a number, try passing the actual url. When you initially place the url on the link, make sure you encode it, and when you try to read it back, make sure you decode it.
$offer = urldecode($_GET['url']);
header("Location: ". $url);
You can also store the mapping on number and url on a database, and then just query for the url where number matches what you send.
Another alternative would be to have an array where you have that mapping.
<?php
$array = array(
1 => "http://www.google.com",
2 => "http://www.yahoo.com",
3 => "http://www.msn.com",
4 => "http://www.reddit.com"
);
$offer = $_GET['url'];
header("Location: " . $array[$offer]);
?>
Store your identifiers and URLs in an associative array:
$list = array(
'1' => 'http://site1.com',
'2' => 'http://site2.com'
);
Then look for the value that matches your key:
if (array_key_exists($_GET['url'], $list)) {
header('Location: ' . $list[$_GET['url']]);
} else {
// bad key
}
You can have an array of possible URL's and you can check the value of $_GET['url']
against these values, then redirect the user accodingly. This can be a manually written array inside this PHP file or an array the values of which have been gathered from a database.
You can also use the if/else
consitions or a switch
condition but that will make your code longer. It's not a bad option although it's less practical.
Also, if you have any other code in this specific page, make sure you have exit;
after the header()
function to prevent it from executing before redirection.
$urls = array(
1 => 'www.someurl.com',
2 => 'www.someotherurl.com'
);
$key = @trim($_GET['url']);
if (!empty($key) && isset($urls[$key]))
header('Location: http://' . $urls[$key]);
else
// fallback
header('Location: http://www.defaulturl.com');
exit;