Need Help With Small Code<

windows

Guest
Can anyone tell me what is the matter with this code to set a cookie?

<head>
<title>set</title>
</head>

<body>
<?php
$name = "Bob";
setcookie("username",$name,time()+1209600);
?>
</body>I thought that you had to have the code before the <html> tag?? I could be wrong thoughno Nin, you can have to code anywhere, as long as it is between the <?php ...?>tags,

Rob, you might be better off placing this question in the php forumatchually...Chris is right.Has to be before any output,or you have to use output buffering....a cookie is considered a header.


<?php
$name = "Bob";
setcookie("username",$name,time()+1209600);
?>
<head>
<title>set</title>
</head>

<body>

</body>Well there you go, I just asumed that it would be similar to ASPIt's like the redirect thing in ASP. You can't have it after output as Will just said. So, I guess best practice is to put it before your header (as will said), or before everythingI use output buffering top to bottom line 1 to last line of my code so I don't worry about headers already sent by....errors.Less limiting and easier to debug then proper call placement :)Originally posted by Kram
Well there you go, I just asumed that it would be similar to ASP
ASP works the same way, you'll get an error if you don't enable buffering.Originally posted by Ninkasi
It's like the redirect thing in ASP. You can't have it after output as Will just said. So, I guess best practice is to put it before your header (as will said), or before everything

With the newer versions of ASP, you can have them anywhere you want because buffering is turned on by default.

Otherwise, you have to turn buffering on (Response.buffer = true at the top of your page) and then do a

response.clear
response.redirect("goTo.asp")
response.end


The buffering tells IIS to hold on to all output until it has all been processed so the response sits in a "waiting dock" to be shipped back to the requesting browser.
Response.clear tells IIS to clear out everything on that dock (which would include that pesky <html> header) and then you can do the redirect and then terminate the process sitting in IIS.

And, after all that, he's needing PHP, so I'll shut my pie hole now.response.buffer = true?your serious?it's like...a variable....O_o...we have to use ob_start(); and ob_end_flush();Originally posted by willamoose
response.buffer = true?your serious?it's like...a variable....O_o...we have to use ob_start(); and ob_end_flush();

More like a switch to flip, but yeah.
 
Back
Top