Alternative to “header” for re-directs in PHP

前端 未结 8 1600
旧时难觅i
旧时难觅i 2020-12-05 06:16

I am working on a project and I am required to run my program on someone else\'s webserver. It is a pretty simple login page that I am having the problem with. The program

相关标签:
8条回答
  • 2020-12-05 06:23

    By using the below code we redirect the page

    $page = $_SERVER['REQUEST_URI'];
    echo '<script type="text/javascript">';
    echo 'window.location.href="'.$page.'";';
    echo '</script>';
    
    0 讨论(0)
  • 2020-12-05 06:23
    echo "<script>window.location.href='yourPage.php'</script>";
    
    0 讨论(0)
  • You cannot set headers after outputting some text, so either you move the header call before the echo statements (fine in your case) or you use output buffering.

    0 讨论(0)
  • 2020-12-05 06:27

    I use this function for redirect...

    Which works in all situations..even if headers are already sent..or even javascript is disabled..

    function redirect($url)
    {
        if (!headers_sent())
        {    
            header('Location: '.$url);
            exit;
            }
        else
            {  
            echo '<script type="text/javascript">';
            echo 'window.location.href="'.$url.'";';
            echo '</script>';
            echo '<noscript>';
            echo '<meta http-equiv="refresh" content="0;url='.$url.'" />';
            echo '</noscript>'; exit;
        }
    }
    
    0 讨论(0)
  • 2020-12-05 06:35

    You probably have already sent content to the browser before the php header is sent. This can be just a \n after a closing php tag ( ?> )

    To find the place where unwanted output is generated canbe hard on bigger projects or long lines of classes extending each other. To find the problem you can do this:

     error_reporting(E_ALL); 
     ini_set("display_errors", 1);
     header("Location: https://mydomain.com/myLoginAdress/");
     die();
    

    Strict error reporting will throw line and file of the output.

    0 讨论(0)
  • 2020-12-05 06:41

    Redirecting via headers is illegal if any content has come through. Take out all the echos and it should work. If you do want there to be a message shown to the user before the redirect, you'll likely have to do it with JavaScript via location.href.

    0 讨论(0)
提交回复
热议问题