NYCPHP Meetup

NYPHP.org

[nycphp-talk] PHP REDIRECT

Hans Zaunere lists at zaunere.com
Wed Sep 5 08:41:20 EDT 2007



PaulCheung wrote on Wednesday, September 05, 2007 8:36 AM:
> Thanks David, Dan and bz
> 
> the missing quote was just a copying error. Using the
> header('location: http://localhost/mainmenu.php');
> 
> this is the value of $number_of_records  = 0
> Warning: Cannot modify header information - headers already sent by
> (output started at C:\xampp\htdocs\etestform.php:4) in
> C:\xampp\htdocs\etestform.php on line 59
> 
> This is the error message the 4 at the end of
> C:\xampp\htdocs\etestform.php:4 - points to script line 4. which is
>  <script language="JavaScript" type="text/javascript">
> 
> Line 59 is
> 57      if ($b == 0)
> 58        {echo 'this is the value of $number_of_records  = ' .
> $number_of_records;
> 59        header('location: http://localhost/mainmenu.php');}
> 60        else
> 
> Using PseudoCode - Is there anything in PHP that will simply just
> sayes the following
> 
> If  $number_of_records == 0
>     goto http://www.localhost/mainmenu.php ??

There is, and you're trying to do it.  But the error you're getting explains
what the problem.  You can't send content to the browser before you send the
headers (which cause the redirect).

At the very first line of your script, try adding:

ob_start();

The error message is not about the Javascript.  It's saying that you can't
send headers after you've already sent page content.  ob_start() adds output
buffering, so you might be able to get away with just adding the above line.
But you should read the comments in the php.net/header document page to
understand why it's working/not working.

H




More information about the talk mailing list