NYCPHP Meetup

NYPHP.org

How to proceed?

ceo at l-i-e.com ceo at l-i-e.com
Fri Feb 7 02:20:25 EST 2003


When a web page is sent to a browser, there are actually two (2) parts: 
The "headers" and the "body" (not to be confused with the HTML head/body
tags).

So a web-page might *REALLY* look like this:

------------------
Content-type: text/html
Content-length: 42
Date: FEB 07 2003 01:06:37 UTC

<HTML><BODY>Web page</BODY></HTML>
-----------------

The "blank line" in between the headers and the body is ABSOLUTELY CRUCIAL!

The existence of that blank line tells the browser "Yo, no more headers,
here comes the body."

So, here's the thing:  "You can't step in the same stream twice."

In this case, once you have sent CONTENT in the body, such as the <HTML>
tag, or even some blank lines, you simply *CANNOT* send headers.  It's too
late!

You already told the browser you were done with the headers.  You lied :-)

You've suppresed the error message PHP is giving you using @ but that only
masks the diagnosis, rather than fixing the problem.

You *MUST* move that switch() statement to be the VERY FIRST THING in the
file, or the header() will simply not work.

*** OR *** Use php.ini settings (*) and/or http://php.net/ob_start to
"buffer" the output.

This will cause PHP to not really really send the output until the page is
done, and it can then internally shuffle things around to get the headers
in order before the actual content.  Note that this does cause a slight
performance "hit" particularly if any LARGE chunks of content (audio,
video, hi-res images) are being processed by/through PHP and end up being
buffered in whole.

* I forget the precise php.ini setting names, but search for "buffer" in
the php.ini file and you should find them.

That said:  The "location" header was *NEVER* meant by original design to
be a programming construct.  It was designed to inform
visitors/browsers/surfers that a web page has *MOVED*  Abusing the
"Location" header instead of coding your site sensibly and logically will
almost always bite you in the butt sooner or later.

Specific instance:  Mixng a "Location" header with Cookies will BREAK the
cookies in *some* very specific/minor versions of various browsers.  This
is not a case of IE vs Netscape, but rather IE 3.0.7 vs IE 3.0.8 (not
real-world examples of which are broken) -- And you really don't want to
mess with something where you have to fight with users over which minor
release version of a browser they are using.

Re-design the application as three pages (how many runs, fill in data,
actually run) or use the same page with a 3-way switch() for all the
functionality.

I'm willing to bet that a half-dozen people on these lists will jump up
and say "Oh, I use header("Location:") all the time, it MUST be okay!" 
Well, we're all entitled to our own opinions, aren't we?  Mine is above. 
Some day, after you've been bit on the butt, you'll agree with me. :-)

Re-factoring your pages into more logical break-downs is just better
programming anyway.

>
> Hi,
> the script below first presents a textbox for the user to enter a
> number. then it echoes itself and presents that many textboxes for the
> user to enter info.
>
> after entering this info, i want the script to call another script where
> the processing is done which i am not able to get it happen. the same
> "user_run.php" seems to be toggling the entry fields.
>
> How to set the action of the script to address the issue?
>
> thankz
> Tracy
>
> ####### user_run.php #########
> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
> <HTML>
> <HEAD>
> <TITLE> Test Network </TITLE>
> </HEAD>
>
> <BODY>
>
> <?
> foreach ($_POST as $k => $v) {
>  $$k = $v;
>  echo $k."=". $v;
> }// echoes fine, flag = 1 on first run and flag = 2 on the second... so
> i guess the switch below isint being processed...
>
> switch ($_POST[flag]) {
>  case 1 : @header("Location: user_run.php");
>   break;
>  case 2: @header("Location: xor_run.php");
>   break;
> }
> ?>
>
> <form method="post" action="">
> <?
> if(!$_POST[tstcnt]) {
>  ?>
>  How many test cases do u want to have ?
>  <input type="text" size="3" name="tstcnt" value="">
>  <input type='hidden' name='flag' value =1 >
>  <input type=submit value=Next>
>  <?
> }
>
> else {
>  echo '<font color=blue>ENTER THE TEST CASES BELOW:</font>'."<br>\
";
> for ($i=1; $i<=$_POST[tstcnt]; $i++) {
>   echo '<p>TEST CASE '.$i.' : ';
>   echo '<input type="text" name="case'."$i".'" value="">'."<br>\
";
>  }
>  echo '<p><input type=submit value="test network">';
>  echo '<p><input type="hidden" name="flag" value =2 >';
> }
> ?>
> </form>
>
> </BODY>
> </HTML>
>
>
>
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> Coming together is a beginning...
>    keeping together is progress...
>       working together is success !!!
> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>
>
> ---------------------------------
> Do you Yahoo!?
> Yahoo! Mail Plus - Powerful. Affordable. Sign up now






More information about the talk mailing list