我有一个大约 50 个页面的网站,每个页面都有一个文本链接和一个横幅链接,
我希望它们引导至相同的重定向页面,这会将它们重定向到外部 URL,
根据这些横幅/文本链接中的两个变量。
横幅链接类似于:
http://mydomain.com/redirect.php?dest=2&source=banner
这是一个似乎有效的代码,我不是程序员,所以我只是从这里和那里编译了补丁。
<!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>
我想知道 - 这是达到目的的最佳代码吗?另外,即使我希望页面显示一些数据,是否可以使用 PHP 而不是 META 进行重定向?
在这里测试:https://eval.in/83122
应该可以。我测试过:
<!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>
使用meta无疑是一个可靠的解决方案...但是如果你想使用php进行重定向,请尝试以下操作:
header('Location: index.php');
或者您想要重定向到的任何文件。使用 header() 相对于 META 的好处是没有人可以从浏览器手动编辑它。 ;)希望有帮助..