Php redirect after headers sent

I understand that so long as data has been sent to the browser then the headers cannot be modified.

Is there any way (using PHP) that I can perform a redirect to take a user to another page (obviously not using headers)

If so could you please point me to some documentation?

asked Aug 15, 2011 at 15:00

Php redirect after headers sent

Decided to write my own php function which implements a javascript redirect. See the code below.

function redirect($url)
{
    $string = '<script type="text/javascript">';
    $string .= 'window.location = "' . $url . '"';
    $string .= '</script>';

    echo $string;
}

answered Aug 15, 2011 at 15:26

Php redirect after headers sent

GaryDevenayGaryDevenay

2,3752 gold badges18 silver badges41 bronze badges

1

I assume output buffering is not an option, since you mention that data has been sent. Use meta redirects, or output JavaScript that takes care of this.

The former could be implemented by adding the following to the <head> section

<meta http-equiv="refresh" content="0; url=http://example.com/">

For the latter, writing something like the following could help:

<script type="text/javascript">
    window.location = "http://example.com/";
</script>

Replace http://example.com/ with your target URL. Note that the latter might be more wieldly to implement if some data has indeed been sent.

A caveat: Both these methods can be blocked at the client end, but technically, so can 301 and 302 redirects.

answered Aug 15, 2011 at 15:10

susmitssusmits

2,1702 gold badges22 silver badges27 bronze badges

2

You can use Javascript or a Meta tag. But if you do not want to fix your code to display output after processing then just turn on output_buffering in the .htaccess file for the site.

php_flag output_buffering on

Is what you would put in your .htaccess file to turn that on. It will allow redirects even if you output stuff while processing. This is because the output buffer saves the buffer to the end, so no data is sent prior to.

Remco K.

6484 silver badges18 bronze badges

answered Aug 15, 2011 at 15:10

Php redirect after headers sent

JimJim

18.4k5 gold badges48 silver badges65 bronze badges

1

Maybe you can use the output buffer before you make a decision to redirect the page.

IMHO otherwise there is only the way to use HTML meta redirect or JavaScript redirect.

answered Aug 15, 2011 at 15:13

Eddy FreddyEddy Freddy

1,8261 gold badge13 silver badges18 bronze badges

This is the php function i use to redirect. If headers haven´t been sent, uses "header(location)", else, uses javascript function.

 function redirect($url)
{
    $baseUri=_URL_;

    if(headers_sent())
    {
        $string = '<script type="text/javascript">';
        $string .= 'window.location = "' . $baseUri.$url . '"';
        $string .= '</script>';

        echo $string;
    }
    else
    {
    if (isset($_SERVER['HTTP_REFERER']) AND ($url == $_SERVER['HTTP_REFERER']))
        header('Location: '.$_SERVER['HTTP_REFERER']);
    else
        header('Location: '.$baseUri.$url);

    }
    exit;
}

answered Jan 21, 2014 at 11:34

PrescolPrescol

5864 silver badges12 bronze badges

PHP doe not offer any other way as far as I remember (limitation of the http protocol), but you can do this with javascript, but it is a completely different process : with js, it is the content of the page that tells the browser to perform a redirection, not the server.

answered Aug 15, 2011 at 15:10

In my case worked ob_start

Javascript solution looks so.. strange

answered Feb 7, 2014 at 11:39

How redirect to another page after submitting a form in PHP?

Now in PHP, redirection is done by using header() function as it is considered to be the fastest method to redirect traffic from one web page to another. The main advantage of this method is that it can navigate from one location to another without the user having to click on a link or button.

How do I redirect a header?

Answer: Use the PHP header() Function You can simply use the PHP header() function to redirect a user to a different page. The PHP code in the following example will redirect the user from the page in which it is placed to the URL http://www.example.com/another-page.php . You can also specify relative URLs.

How can I redirect a URL to another URL in PHP?

Redirection from one page to another in PHP is commonly achieved using the following two ways: Using Header Function in PHP: The header() function is an inbuilt function in PHP which is used to send the raw HTTP (Hyper Text Transfer Protocol) header to the client.

How do I redirect to another page after login?

To redirect users to a specific page after login, you can simply add the redirect URL parameter in login form Shortcode. The redirect_url parameter allows you to redirect to a certain page after the user is logged in.