Cannot modify header information PHP error

If you are getting following warning and redirecting is not working then use the following function or remove all echo commands.

The following function works even if you have ECHO commands in your script page

Warning: Cannot modify header information – headers already sent by

redirect( “http://www.example.com” );

function redirect( $filename )

    {

        if ( !headers_sent() )

        header( ‘Location: ‘ . $filename );

    else

    {

        echo ( ‘JavaScript’ );

        echo ‘<script type=”text/javascript”>’;

        sleep( 5 );

        echo ‘window.location.href=”‘ . $filename . ‘”;’;

        echo ‘</script>’;

        echo ‘<noscript>’;

        echo ‘<meta http-equiv=”refresh” content=”0;url=’ . $filename . ‘” />’;

        echo ‘</noscript>’;

    }

    }

Leave a Reply

Your email address will not be published. Required fields are marked *