我有一个包含变量的URL,我希望该变量确定要重定向到哪个页面。我尝试在 Switch 语句中使用元重定向,但它不起作用。我做错了什么?
<?php
setcookie("affiliate", $_GET['a'], time()+31536000);
?>
<!DOCTYPE html>
<html>
<body>
<?php
switch($_GET['p'])
{
case"h":
echo "<meta http-equiv="refresh" content="1; url=website/">";
echo "redirecting in 1 second";
echo "if this page does not redirect <a href="website/">click here.</a>";
break;
case"w":
echo "<meta http-equiv="refresh" content="1; url=website/watches.html">";
echo "redirecting in 1 second";
echo "if this page does not redirect <a href="website/watches.html">click here.</a>";
break;
case"sf":
echo "<meta http-equiv="refresh" content="1; url=website/watches/solar-flare.html">";
echo "redirecting in 1 second";
echo "if this page does not redirect <a href="website/watches/solar-flare.html">click here.</a>";
break;
default:
echo "incorrect page option";
}
?>
</body>
</html>
谢谢:)
克里斯
使用 header
函数:
header('Location: index.php');
或
header('Location: http://google.com');
而不是打印元标记。
注意:在打印页面上的任何内容之前,必须使用header
功能。所以我建议你将开关移到代码的顶部。
您可以使用refresh=X;url=Y
标头在 X 秒后进行重定向:
<?php
setcookie("affiliate", $_GET['a'], time()+31536000);
switch($_GET['p']) {
case"h":
header("refresh:5;url=website/");
$body = "redirecting in 1 second<br />" . "if this page does not redirect <a href='"website/'">click here.</a>";
break;
case"w":
header("refresh:5;url=website/watches.html");
$body = "redirecting in 1 second<br />" . "if this page does not redirect <a href='"website/watches.html'">click here.</a>";
break;
case"sf":
header("refresh:5;url=website/watches/solar-flare.html");
$body = "redirecting in 1 second<br />" . "if this page does not redirect <a href='"website/watches/solar-flare.html'">click here.</a>";
break;
default:
$body = "incorrect page option";
}
?>
<!DOCTYPE html>
<html>
<body>
<?= $body ?>
</body>
</html>