--- Pete <[EMAIL PROTECTED]> wrote:

> What I am wondering is this : when the visitor visits the site, is the
> cookie data automatically sent to the server, or does the server have to
> request it?  
> 
> Or, to put it another way, does a call to getcookie get data which has
> just been passed to the server, or is it a direct call to the visitors
> computer?
> 
> -- 
> Pete Clark

The cookie written to the browser and stored either in memory (session level
cookie) or disk (cookie with an expiration date).  It is associated with the
domain name used on the initial request of the script that wrote the cookie. 
Hence you could have problems if you used links with www.domain.com in some
places and domain.com in others.

Each time the visitor's browser requests a page it offers the cookies which are
associated with that domain name as part of the HTTP Request.  PHP stores the
information in the cookie portion of the HTTP request in the super global
$_COOKIE where it may be inspected and retrieved.

Your script is not initiating a request for this information.  Your script is
usually done running by the time the generated HTML and whatever is received by
the browser.  Anything else would require that the browser be listening for
such a request and have a server program to respond to it.  This would be very
bad since it could be abused.

Since cookies are written to the header portion of the HTTP (not HTML) headers,
you generally have to do all of your header actions (setcookie(), header(),
etc)  calls before you output anything which PHP would interpret as content to
go into the body of the HTTP response to the browser.  

One good way to prevent this is to place the opening PHP tag on the first line
first character position.  This means before your <html> tag and before any
blank lines.  The setcookie() or header() functions must be run before any
print or echo statements.

If you fail to do this correctly you will get an error message about "cannot
modify the header because the header has already been sent" (or something close
to that).  The message will include a line in your program where the output
started.

There are other ways to avoid this such as using ob_start() and ob_flush() to
collect all of the output and send it at the end of the code processing.  PHP
will sort out the content which should go in the HTTP header and body at the
time of the flush.  However, to get this to work you still have to start the
PHP tag on the first line first character position to ensure no output before
the ob_start() is "sent" to the browser.

I consider the "headers already sent" error to be one of the top five we see in
this PHP group.

James

Reply via email to