Question

I'm using the below function to redirect a person after specific task (eg.: after login, after logout, after searching etc.) code is below:

<?php
class common {
    /* Redirect to another page
     * $url= Url to go
    */
    function redirection($url){
        header("location: $url");
        exit();
    }
    // Some other function below
?>

But now I'm dealing this class with many project of different host (MLM project). I have a problem now. With some server it works as i expected, but in some other server, it's not redirecting. If i enable error_reporting(E_ALL); i found a notice that headers are already send. So I'm in confusion that what can I do now instead of header() function. Also i tried the below code

<?php
    function redirection($url){
        echo "<div align='center'><a href='$url' target='_top'><img src='../img/proceed.jpg' alt='Proceed>>' align='absmiddle' border='0'></a></div>";
        exit();
    }
?>

But it is not desirable as everybody wants automatic redirection. My servers are windows and linux both. Please help me anyone

Was it helpful?

Solution

well, this situation is very common, then you can simple turn on output buffering (the output will be stored in an internal buffer).

Use ob_start(); in the very first line of your application

<?php
    class common {

        /* Redirect to another page
         * $url= Url to go
         */

        function redirection($url)
        {
          header("location: $url");
          exit();
        }

        // Some other function below

    }

?>


<?php
    ob_start("redirection");

    // Your Common Class Page
    include("Common.php");

     // some code 

    ob_end_flush(); // turn off output buffering
?>

OTHER TIPS

One way to deal with this is to test if the header has already been sent before calling header(location). You could use mix both solutions:

<?php
class common {
    /* Redirect to another page
     * $url= Url to go
    */
    function redirection($url){
        if (!headers_sent()) {
            header("location: $url");
        } else {
            echo "<div align='center'><a href='$url' target='_top'><img src='../img/proceed.jpg' alt='Proceed>>' align='absmiddle' border='0'></a></div>";
        }
        exit();
    }
// Some other function below
?>

This way if the headers haven't been sent, you redirect automatically. If they have, you ask the client to click.

This is the reason why when you see a redirection notice in most websites, it also includes a sentence stating - if you are not redirected automatically, please click here...

Hope this helps.

Good luck!

If headers have already been sent, it is likely because content has already been written out to the screen (via an echo, print, or similar). Since your class has no control over what came before it was instantiated and the function was called, it seems unlikely that you can do much to avoid your client PHP (what calls your class) from writing anything out before. Either use Javascript or use Apache redirects.

I would try using:

header("Location: ".$url, TRUE, 302);

If you want to use a different method, or called "refresh" method,

header("Refresh:0;url=".$url);

Both would work in every case. The problem with your header is, you need to let them know it's a 302 redirect, as well as set TRUE to replace the existing headers. If header is already set, you need to replace it using TRUE boolean.

302 is also the common HTTP response code for redirection, which needs to be specified when you are trying to redirect using header.

The Refresh method works fine as well, though it has compatibility issues with older browsers.

http://en.wikipedia.org/wiki/HTTP_302

http://php.net/manual/en/function.header.php

the easiest way is to do it through client side. javascript...

window.location= url
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top