Re: Error when execute header('location: otherpage.php') after email been sent out. Any Workaround?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Fri, Aug 28, 2009 at 9:36 AM, Floyd Resler <fresler@xxxxxxxxxxxxx> wrote:

> Another solution would be to use JavaScript.  In your process.php script
> you could do this:
> print<<<here
> <script language="javascript">
>        window.location.href="index.php"
> </script>
> here;
>
> Not strictly a PHP solution but it works.
>
> Take care,
> Floyd
>
>
> On Aug 28, 2009, at 5:34 AM, Ashley Sheridan wrote:
>
>  On Fri, 2009-08-28 at 17:32 +0800, Keith wrote:
>>
>>> I have a user sign up page that collects sign up information from user
>>> with
>>> form.
>>> This form will then be submitted to another process.php page for setting
>>> up
>>> the user account and send email to the user.
>>> After the email been sent out, the user will be directed back to homepage
>>> with header("location: index.php").
>>> However, error happen with warning: [Cannot modify header information -
>>> headers already sent by...]
>>> Any workaround for this?
>>>
>>> Thanks for help!
>>> Keith
>>>
>>>
>>>
>>>
>> You don't need a workaround. The header("Location...") thing will only
>> work if you have not sent any content to the browser yet. That includes
>> even a single space or newline.
>>
>> It looks like in your process.php page, something is being output to the
>> browser. Are you able to do a code dump of that page?
>>
>> Thanks,
>> Ash
>> http://www.ashleysheridan.co.uk
>>
>>
>>
>>
>> --
>> PHP General Mailing List (http://www.php.net/)
>> To unsubscribe, visit: http://www.php.net/unsub.php
>>
>>
>>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>
Actually there aren't a safe way to make a redirect. Every technique relies
on the client-side to perform the redirection.

We have 4 alternatives (correct me if I miss anything) to do a safe
redirection.

   1. header Location
   2. meta tag refresh
   3. javascript
   4. the user


We will try every one in the given order...

Let's suppose we want to redirect the client to $url...

<?php

session_write_close();
header( 'Location: '. $url ); # redirect by http header (1)

?><html>
    <head>
        <title>Redirecting...</title>

        <!-- redirect by meta tag (2) -->
        <meta http-equiv="refresh" content="2;url=<?php echo $url?>" />

        <script type="text/javascript">/*<![CDATA[*/
            // redirect by javascript (3)
            window.location = '<?php echo $url ?>';
        /*]]>*/</script>

    </head>
    <body>
        <p>You are about to be redirected to <?php echo $url?> in 2
seconds</p>
        <p>
            <!-- redirect by user (4) -->
            <a href="<?php echo $url?>">
                Click here if your browser do not redirect automatically
            </a>
        </p>
    </body>
</html>
<?php exit; ?>


It's recommended that your use a common redirect method in your site, this
is not mandatory but it is a good practice.

This simple script also helps to prevent session problems under some evil
web-server by closing the session before any output.



-- 
Martin Scotta

[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux