简体   繁体   English

使用切换大小写更改php标头

[英]Change php header using switch case

I want to redirect to other page based on the selected string in combo box. 我想根据组合框中选择的字符串重定向到其他页面。

I have added following code: 我添加了以下代码:

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");
}

But this simple script is not working. 但是这个简单的脚本不起作用。 I am new to php. 我是php新手。 Do you have any idea why I am not able to change header. 您知道为什么我无法更改标题吗? Is it like we cannot change header using switch-case statements. 难道我们无法使用switch-case语句更改标头。

If this is not the way then what could be the possible way to redirect to another page on form submission. 如果不是这样,那么重定向到表单提交上的另一页的可能方法是什么。

You need to add a break; 您需要添加休息时间; to the end of each case. 到每种情况的结尾。

case "text3": 
    header("Location:pdf/text3.pdf"); 
    break;

You need to add 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");
}

You must see switch statements as a sort of jump to construct. 您必须将switch语句视为一种构造跳转 Depending on the value of the evaluated expression, code execution jumps to the first appropriate case or default statement and continues from there. 根据执行的表达式的值,代码执行将跳到第一个适当的casedefault语句,然后从那里继续。 That's why you'll want to insert a break statement most of the times. 这就是为什么您通常会想插入break语句的原因。 Currently, you only do it for the "text1" case. 当前,仅针对"text1"情况进行此操作。

Apart from that, I suggest you use full URLs when composing Location HTTP headers, even though relative URLs normally work as expected. 除此之外,我建议您在编写Location HTTP标头时使用完整URL,即使相对URL通常可以按预期工作。 The HTTP specification doesn't really allow relative URLs. HTTP规范实际上并不允许相对URL。

header('Location: http://example.com/foo/pdf/text2.pdf');

And don't forget that sending an HTTP header does not stop the script execution. 并且不要忘记发送HTTP标头不会停止脚本执行。 Add an exit; 添加exit; statement when you're done. 完成后的声明。

Apart from completing your case statements with break, also remember that you must call header() before any actual output is sent. 除了使用break来完成case语句外,还请记住,必须在发送任何实际输出之前调用header()。

Eg: 例如:

<html>
<?php
header("Location:pdf/text2.pdf");?>
</html>

Will not work, since the " <html> " line will already have been sent to the client, precluding the use of header(). 将不起作用,因为“ <html> ”行已经被发送到客户端,并且不能使用header()。 Basically, make sure no output is going on in the file before you try to call header(). 基本上,在尝试调用header()之前,请确保文件中没有任何输出。

Edit: Ref: http://php.net/manual/en/function.header.php 编辑:参考: http : //php.net/manual/en/function.header.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM