doutai1509 2015-09-09 05:31
浏览 34

php重定向没有发生

I have a question..

function curPageURL() {
    $pageURL = 'http';
    if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
    $pageURL .= "://";
    if ($_SERVER["SERVER_PORT"] != "80") {
        $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
    } else {
        $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
    }
    return $pageURL.'?app_not_found';
}

Through the function I am fetching the current URL .... now I have two conditions

if($databaseAppVersion == $appVersionName)
    {
        //echo curPageURL();
        header('Location: '.curPageURL());
    }
    else 
    {
        $url ="www.google.com";
        //echo $url;
        header('Location: '.$url);
    }

when I am printing them the echo is happening but the problem is that the it does not redirection me to the intended page ... on the first condition I want to redirect to the current page having a message i.e. localhost/test/index.php?app_not_found but this is not happening

  • 写回答

1条回答 默认 最新

  • doubi2228 2015-09-09 08:54
    关注

    if your page is like this:

    <?php
    
    function curPageURL() {
        $pageURL = 'http';
        if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
        $pageURL .= "://";
        if ($_SERVER["SERVER_PORT"] != "80") {
            $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
        } else {
            $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
        }
        return $pageURL.'?app_not_found';
    }
    
    if($databaseAppVersion == $appVersionName)
        {
            //echo curPageURL();
            header('Location: '.curPageURL());
        }
        else 
        {
            $url ="www.google.com";
            //echo $url;
            header('Location: '.$url);
        }
    

    I wouldn't expect anything other than redirects to www.google.com, since you are not setting $databaseAppVersion and $appVersionName anywhere

    if you have any echo or print functions before your header call, redirecting won't work, since the headers are already sent. See How to fix "Headers already sent" error in PHP.

    on a different note; your curPageURL() will not necessarily return what you expect:

    if you give it http://www.example.com it will return http://www.example.com https://www.example.com will return https://www.example.com:443 since you only check for port 80.

    you can modify your code easily:

    function curPageURL() {
        $pageURL = 'http';
        if ($_SERVER["HTTPS"] == "on") {$pageURL .= "s";}
        $pageURL .= "://";
        if ($_SERVER["SERVER_PORT"] != "80" || ($_SERVER["SERVER_PORT"] != 443 && $_SERVER["HTTPS"] == "on")) {
            $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"];
        } else {
            $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];
        }
        return $pageURL.'?app_not_found';
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程