Re: [PHP4BETA] Header() in PHP4

From: Date: Wed, 25 Aug 1999 15:10:12 +0000
Subject: Re: [PHP4BETA] Header() in PHP4
References: 1  Groups: php.version4 
Request: Send a blank email to php-version4+get-3717@lists.php.net to get a copy of this message
To throw in my two cents worth... First, I thought that the reason that header() has to be called first is because it generates HTTP statements as opposed to HTML? I.e., the standard says that all headers have to be sent first? For example, the browser can't be sent: <html> <head> <title>Hi</title> Location: http://www.mysite.com/ The other thing is that I can sympathize with the problem and the way I handle it (assuming my understanding of the issue isn't faulty) is something like this: $p = mkHead("Welcome", "JSSetCookie"); $p .= "Some stuff for my page\n"; if (! isset($aCookie)) { header("Location: http://www.mysite.com/"); exit; } if (! isset($foo)) { $p = mkHead("Error"); $p .= "Sorry! There was an error!\n"; $p .= mkFoot(); print $p; exit; } $p .= "Everything is fine!\n"; $p .= mkFoot(); print $p; In this, the hypothetical mkHead() function takes a title and script name as arguments and then returns HTML with JavaScript for that function (selected via a switch) in the <HEAD> portion of the HTML. The switch actually invokes a function that returns a string that is the code for that routine and appends it to the rest of whatever mkHead() is going to return. I tend to do almost everything with functions that return strings that are page fragments until the page is completely built, and then I print the completed page to the browser. This lets me change my mind at any moment until the very end. -- Peter. ----- Original Message ----- From: Michael Stearne <MStearne@eisolutions.com> To: <php4beta@lists.php.net> Sent: Tuesday 24 August 1999 05:53 PM Subject: [PHP4BETA] Header() in PHP4 > People ask about the differences between PHP and Cold Fusion regularly. > One thing that I liked about Cold Fusion was the CFLOCATION tag, which > is works like the header() tag in PHP. > One big difference between the CFLOCATION and header is that it is > possible to have HTML and output on a page before the tag is called. For > example (I am unsure of CF syntax): > > <HTML> > <body> > This is something > > <CFIF #VAR# is 3> > <CFLOCATION location="page.html"> > </CFIF> > > </body> > </html> > > Whereas PHP requires: > > <? > if($var==3) > header("Location: page.html") > ?> > <html>.... > > Either way is easy to use, but there is sometimes an advantage in > running some output before the page is redirected. I know this can be > done with a <META> tag, but I don't like the fact that the user can hit > Stop on the browser. > > The reason why I am asking about this feature in PHP4 is because I > (maybe wrong) am under the impression the script is "compiled" > completely before output is sent to the browser. I don't believe this > was the case with 3. And I believe this is the way CF works. > > Sooo, is there anyway that this functionality can be added to version > 4. This would also be a valuable feature for the SetCookie function. I > don't always have the opportunity to set a cookie before any output has > been sent consequently I use a function JSSetCookie(below) which works > like SetCookie but prints Javascript out for the browser to pick up on. > But that requires the user to have JS on. > > > Any thoughts? > Michael Stearne > > -------------------------------------------------------------------------- ----- > > function JSSetCookie($CName,$CValue,$CExpr=FALSE){ > // $CXpr is in years > // Will allow cookies to be set even after HTML has been printed on the > page > if($CExpr!=FALSE){ > $CookieString="astr= '$CName' + '=' + '$CValue' + > ';expires=' + expr + > ';path=/';"; > $Cookie.="\n<script language=\"javascript\">\n"; > $Cookie.='function makeYearExpDate(yr){ > var expire = new Date(); > expire.setTime(expire.getTime() + ((yr *365) *24 * 60 * 60 * 1000)); > expire = expire.toGMTString() > return expire > } > expr =makeYearExpDate('.$CExpr.');'; > $Cookie.="\n".$CookieString."\n"; > $Cookie.="document.cookie=astr;\n</script>\n"; > }else{ > file://$CookieString="astr= '$CName' + '=' + '$CValue' + > ';expires=;path=/';"; > $Cookie.="\n<script language=\"javascript\">\n"; > $Cookie.="document.cookie='$CName=$CValue;path=/';"; > $Cookie.="\n</script>\n"; > } > echo $Cookie; > } > > -- > PHP 4 Beta > >

« previous php.version4 (#3717) next »