tags:

views:

360

answers:

6

I Like to call the page using header(), but the following error occurs,

Cannot modify header information - headers already sent

why?. How to sort it out. Help me. My code is attached

if ( $_SERVER['REQUEST_METHOD'] == 'POST' )
   {
         $adduser=new Users();
         if($adduser->checkmob()==true)
        {
         $adduser->addDB();
         $adduser->addCarrer();
         $adduser->addCricket();
         $adduser->addHealth();
         $adduser->addJokes();
         $adduser->addThoughts();
        ?>
    <script>alert ("Congratulations, You are now member of alertme serives");
    </script>

            <? header('Location: index.php');
          }
        else
         {
          ?><script>alert ("Mobile Number, Already Registered");</script>
                    <?}

    }
+4  A: 

header() fails if PHP has already sent content to the user agent. In your case, you already sent the <script>alert()</script> thing.

To prevent this from happening, simply insert this call before your script produces any output:

ob_start();

All the details are here: http://php.net/manual/en/book.outcontrol.php

Looking at your code, there seems to be a bit of a misunderstanding, too: the redirect actually happens immediately when the header is sent. Since the header must be sent before any content is sent, this means that the redirect will happen before the user ever sees the JavaScript alert box. A different way to get things working (which, too, would depend on JavaScript, but that's a different story) would be to use a JavaScript-based redirect that immediately follows the alert().

Jan Krüger
thanks, the code now works, Is there any problem using ob_start??
Rajasekar
You're right that the redirect will happen before the user sees the alert, but it is not *immediate*. Unless you explicitly `die`/`exit` after a `header('location: blah')` call, *your script will continue to process normally*
Frank Farmer
@Rajasekar: ob_start slightly increases memory requirements, particularly for huge pages, since it keeps all of the output in memory and then flushes it all in one go. It's not normally an issue, though.@Frank: good point. My main point was that a redirect doesn't wait for JavaScript actions. I didn't make it very clear that it doesn't usually influence the PHP side of things.
Jan Krüger
A: 

From the PHP docs:

Remember that header() must be called before any actual output is sent, either by normal HTML tags, blank lines in a file, or from PHP.

Basically, your <script>alert();</script> javascript is being outputted before you call header. Perhaps a javascript redirect is in order? Or you could use confirm();

Typeoneerror
+2  A: 

You cannot send headers after doing any other output to the page - as described here: http://php.net/header

You need to either

  • buffer the output, or
  • use a different type of redirect
warren
Buffering the output would be (IMHO) a bad idea as it hides a problem, as it would look as though the html on the page would get displayed where as it wouldn't, as the http headers redirect the browser away from that page.
Yacoby
+4  A: 

You cannot use the header() function after any output has been sent. You have sent the <script> tag and content and then tried to redirect the user.

If you must output HTML before redirecting, use HTML and Javascript to change the page location (the HTML in case Javascript isn't active). Something like this:

<script>
alert('You are now a member of ...');
window.location = 'http://www.example.com';
</script>
<p> 
    If you are not automatically redirected, please <a href="http://www.exam..."&gt;click here</a>
</p>
Yacoby
+4  A: 

When you end your php script ( '?>' ) and start outputting html with your tag then the headers are sent. If you want to store content that should be output and then send the headers later with additional information, then you should look at the Output Control functions of PHP.

Noah Goodrich
A: 
bhaskaragr29