简体   繁体   English

使用标头,重定向在PHP中不起作用

[英]Redirect is not working in php, using header

I am wondering why this is not relocating me to the contact page? 我想知道为什么这不能将我重新定位到联系页面?

    <?php session_start(); ?>


    <?php

    function setSessions($page){
        switch($page)
        {
            case "contact":
                $_SESSION['contact_name'] = $_GET['name'];
                $_SESSION['contact_address'] = $_GET['address'];
                $_SESSION['contact_phone'] = $_GET['phone'];
                header('Location:contact.php');
                exit;
                break;
            case "employment":
                break;
            case "position":
                break;
            default:
                break;
        }

    }




    $current_page = $_REQUEST['page'];
    setSessions($current_page);


    ?>

I am positive it is going in the contact case. 我很肯定这将在联系案例中进行。 thanks for the help 谢谢您的帮助

-edit: NVM, it works. -edit:NVM,它可以工作。 Thanks! 谢谢!

You can't send any output before making a call to header() . 调用header()之前不能发送任何输出。

You would have to remove: 您将必须删除:

<html>

and

echo "i got in here";

References: 参考文献:

This is why: 这就是为什么:

 <html>

You can't call header() if you have output anything to the browser. 如果已向浏览器输出任何内容,则无法调用header()。 Move the redirect code above this html tag and remove that echo and you should be ok. 将重定向代码移到该html标记上方,然后删除该回显,您应该就可以了。

For future reference white html space before header being called will also stop this from working. 为了将来参考,在调用标头之前,白色html空格也将阻止其工作。 (lines 2 & 3) (第2和3行)

You can't have ANY output what so ever before using header(). 使用header()之前,您无法获得任何输出。 The echo statement in your switch statement is considered output and is the thing messing up your code. switch语句中的echo语句被认为是输出,是使代码混乱的事情。

Any emission sends headers in PHP, and once you've sent headers you can't send any more. 任何发射都会在PHP中发送标头,一旦发送标头,您将无法再发送任何标头。 Even whitespace will do this, but you have the string <html> being output. 即使是空格也可以做到这一点,但是您将输出字符串<html> You can't do that. 你不能那样做。

The better solution is to not do it and save all emission until after you have built everything that you want to output. 更好的解决方案是在构建完要输出的所有内容之后,不执行此操作并保存所有排放。

The worse solution would be to use output buffering. 较差的解决方案是使用输出缓冲。 At the start of your script but, ob_start() . 在脚本的开头,但是ob_start() Then, headers will work as normal. 然后,标题将正常工作。

The problem is that you are not getting the correct value for $current_page ; 问题是您没有获得$current_page的正确值; you can echo that value and you will see it's empty. 您可以回显该值,您将看到它为空。 So what you need to do is you should take the current page value here using this function: 因此,您需要做的是使用此函数在此处获取当前页面值:

function getMyurl(){

  $url = (!empty($_SERVER['HTTPS'])) ? "https://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'] : "http://".$_SERVER['SERVER_NAME'].$_SERVER['REQUEST_URI'];

  echo $url;

}

now $current_page=getMyurl();

You should know that getMyurl will return a full url of your website or application so you should modify it in your cases where it's written contact you should replace that with case "yourwebsite/contact". 您应该知道getMyurl将返回您的网站或应用程序的完整url,因此,如果您是书面联系人,则应对其进行修改,应将其替换为“ yourwebsite / contact”。

It should now work for you 现在应该为您工作

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

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