[英]Change php header using switch case
我想根据组合框中选择的字符串重定向到其他页面。
我添加了以下代码:
switch ($downloadType){
case "text1":
header("Location:pdf/text1.pdf");
break;
case "text2":
header("Location:pdf/text2.pdf");
case "text3":
header("Location:pdf/text3.pdf");
default:
header("Location:index.html");
}
但是这个简单的脚本不起作用。 我是php新手。 您知道为什么我无法更改标题吗? 难道我们无法使用switch-case语句更改标头。
如果不是这样,那么重定向到表单提交上的另一页的可能方法是什么。
您需要添加休息时间; 到每种情况的结尾。
case "text3":
header("Location:pdf/text3.pdf");
break;
您需要添加中断:
switch ($downloadType){
case "text1":
header("Location:pdf/text1.pdf");
break;
case "text2":
header("Location:pdf/text2.pdf");
break;
case "text3":
header("Location:pdf/text3.pdf");
break;
default:
header("Location:index.html");
}
您必须将switch语句视为一种构造跳转 。 根据执行的表达式的值,代码执行将跳到第一个适当的case
或default
语句,然后从那里继续。 这就是为什么您通常会想插入break
语句的原因。 当前,仅针对"text1"
情况进行此操作。
除此之外,我建议您在编写Location
HTTP标头时使用完整URL,即使相对URL通常可以按预期工作。 HTTP规范实际上并不允许相对URL。
header('Location: http://example.com/foo/pdf/text2.pdf');
并且不要忘记发送HTTP标头不会停止脚本执行。 添加exit;
完成后的声明。
除了使用break来完成case语句外,还请记住,必须在发送任何实际输出之前调用header()。
例如:
<html>
<?php
header("Location:pdf/text2.pdf");?>
</html>
将不起作用,因为“ <html>
”行已经被发送到客户端,并且不能使用header()。 基本上,在尝试调用header()之前,请确保文件中没有任何输出。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.