问题
I have a website with about 50 pages, each one have a text link and a banner link,
I want them to lead to the same redirect page, that would redirect them to an external URL,
according to two variables that would be in these banner/text link.
a banner link would be something like:
http://mydomain.com/redirect.php?dest=2&source=banner
Here's a code that seems to be working, I'm not a programmer so I've just compiled patches from here and there.
<!doctype html>
<?php
$id = $_GET['source']; // user would get to this page with an "id" that would be according to the link he clicked, either text or banner; this "id" info needs to be passed on to the external page as well, with the following "?referal" tag.
if(isset($_GET['dest'])); // user would get to this page from one of about 50 different pages; his redirect destination would depened on the "dest" number of the link he clicked
switch ($_GET['dest']) {
case "1":
$url = "http://url1.com/?referal=$id";
break;
case "2":
$url = "http://url2.com/?referal=$id";
break;
case "3":
$url = "http://url3.com/?referal=$id";
break;
default:
$url = "http://unknown.com/?referal=$id";
}
?>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="refresh" content="5;url=<?php print $url; ?>">
<title>Untitled Document</title>
</head>
<body>
<?php
$id = $_GET['source'];
if ($id == 'text'){
echo 'The clicked link was a text link';
}
elseif ($id == 'banner'){
echo 'The clicked link was a banner link';
}
else {
echo 'The clicked link is unknown';
}
?>
<p></p>
<?php
if(isset($_GET['dest']));
switch ($_GET['dest']) {
case "1":
$url = "http://url1.com/?referal=$id";
echo "You would be redirected to domain no. 1";
break;
case "2":
$url = "http://url2.com/?referal=$id";
echo "You would be redirected to domain no. 2";
break;
case "3":
$url = "http://url3.com/?referal=$id";
echo "You would be redirected to domain no. 3";
break;
default:
echo "default";
}
?>
<P></P>
<?php
echo "The url you would be redirected to is: $url";
?>
</body>
</html>
I want to know - Is this the optimal code for the purpose? also, Is it possible to make the redirect with PHP instead of META, even though I want the page to display some data?
回答1:
Test here: https://eval.in/83122
It should work. I have tested it:
<!doctype html>
<?php
$id = $_GET['source']; // user would get to this page with an "id" that would be according to the link he clicked, either text or banner; this "id" info needs to be passed on to the external page as well, with the following "?referal" tag.
if(isset($_GET['dest'])){ // user would get to this page from one of about 50 different pages; his redirect destination would depened on the "dest" number of the link he clicked
switch ($_GET['dest']) {
case "1":
$url = "http://url1.com/?referal=$id";
break;
case "2":
$url = "http://url2.com/?referal=$id";
break;
case "3":
$url = "http://url3.com/?referal=$id";
break;
default:
$url = "http://unknown.com/?referal=$id";
}
}
?>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="refresh" content="5;url=<?php print $url; ?>">
<title>Untitled Document</title>
</head>
<body>
<?php
$id = $_GET['source'];
if ($id == 'text'){
echo 'The clicked link was a text link';
}
elseif ($id == 'banner'){
echo 'The clicked link was a banner link';
}
else {
echo 'The clicked link is unknown';
}
?>
<p></p>
<?php
if(isset($_GET['dest'])){
switch ($_GET['dest']) {
case "1":
$url = "http://url1.com/?referal=$id";
echo "You would be redirected to domain no. 1";
break;
case "2":
$url = "http://url2.com/?referal=$id";
echo "You would be redirected to domain no. 2";
break;
case "3":
$url = "http://url3.com/?referal=$id";
echo "You would be redirected to domain no. 3";
break;
default:
echo "default";
}
}
?>
<P></P>
<?php
echo "The url you would be redirected to is: $url";
//header("location".$url); //enable it to redirect to the $url
?>
</body>
</html>
回答2:
using meta is no doubt a reliable solution... But if you want a redirect using php, try following:
header('Location: index.php');
or whatever file you want to redirect to.. The benefit of using header() over META is that no one could manually edit it from their browser. ;) Hope that helps..
来源:https://stackoverflow.com/questions/20776636/multiple-redirect-page-that-would-display-a-message-before-is-this-the-best-met