[thelist] PHP site - Cannot send cookies???

Seb seb at members.evolt.org
Thu Jul 26 07:20:51 CDT 2001


Yesterday, FayeC asked:

> I am working on a site for a school assignment. The teacher told us to
> host the site in a "real" server and keep a copy of it at the school's
> server.The site was initially built and debugged at the school's server
> and it works there. After transfering the files to f2s and changing the
> db connection the site doesn't work as before and I get many errors ...
> The site in question is:
> www.fayec.f2s.com  the school's counterpart is at:
> http://delta.humberc.on.ca/summer2001php/fayec/index.php
>
> As We just started PHP a week ago my knowledge of the language is
> minimal and I can't figure out why it is telling me that the cookies
> cannot be sent...

Hi Faye,

My guess is that your code has already output something before you call the
setcookie() function. setcookie() is a variant of the header() function,
which sends an HTTP header to the browser. Because of the way in which the
HTTP response works, you must send all headers, including cookies, before
any content is sent to the browser.

Common things to check for include ANY whitespace outside of the <? ?>
delimiter tags. If there is ANYTHING at all outside of these tags before
your call to setcookie(), then you will not be able to send a cookie, or any
other header. FYI, there should never be any whitespace outside the php
delimiter tags unless you are intentionally sending it to the browser, as
whitespace outside tags is not generally considered a valid method of
formatting php code.

To check this, immediately before calling setcookie(), use the
headers_sent() function. This will return TRUE if the HTTP headers have
already been sent, meaning you cannot send your cookie at this point in the
code. It will return FALSE if the headers hae not been sent, and you will be
able to call setcookie() successfully.

You might need to restructure your code in order to ensure that setcookie()
is called before any content is sent to the browser. This should be quite
straightforward because your HTML output should not affect the value of your
cookie. If you are still having problems, you might want to investigate the
use of the output buffer. For more information on this, see:
http://www.php.net/manual/en/ref.outcontrol.php and
http://zend.com/zend/art/buffering.php

Hope this helps, feel free to mail me if you have any problems.

Cheers,

Seb
http://www.sebpotter.org







More information about the thelist mailing list