Best redirection methods?

In a PHP file, when do I need to redirect the user, and the headers are already sent, so I can not use the php header function, in this case is the best way to redirect the user?

The fastest and most reliable method, regardless of brand of browser users?

echo '<script type="text/javascript">window.top.location="http://localhost/";</script>';

// OR

echo '<meta http-equiv="refresh" content="0;url=' .$location. '">';

UPDATE

Here is my end result code, which I am using now, if the headers have already been sent to where I cannot redirect to the main page, I just bring me the home page, so instead of including the body page, it will include my home page and footer

function validlogin($url) {
    if (!isset($_SESSION['auto_id']) || ($_SESSION['auto_id']=='')) {
        $_SESSION['sess_login_msg'] = 'Please login';
        $_SESSION['backurl'] = $url;
        $temp = '';
        if (headers_sent() === false){
            header("Location: /");
            exit();
        }else{
            //header is already sent so we will just bring the homepage to us instead of going to it!!!
            include 'mainbody.inc.php';
            include 'footer.inc.php';
            exit();
        }
    }
}
+3
source share
4 answers
function Redirect($url, $permanent = false)
{
    if (headers_sent() === false)
    {
        header('Location: ' . $url, true, ($permanent === true) ? 301 : 302);
    }

    exit();
}

Redirect('http://www.google.com/', false);

Again, use ob_start ();

+4

! HTTP.

ob_start() .

, MVC, , - ( ob_start(), ).

+7

Piggy porneL:

: "". "" - .

. , , .

+2

, .

If the user has javascript disabled or a browser without javascript is used, the first redirect will never work.

-1
source

Source: https://habr.com/ru/post/1714782/


All Articles