php-windows Digest 28 Dec 2006 23:26:16 -0000 Issue 3098
Topics (messages 27333 through 27335):
Re: setcookie() not working - help!
27333 by: Stut
27334 by: Niel Archer
27335 by: Beraru Liviu
Administrivia:
To subscribe to the digest, e-mail:
[EMAIL PROTECTED]
To unsubscribe from the digest, e-mail:
[EMAIL PROTECTED]
To post to the list, e-mail:
[email protected]
----------------------------------------------------------------------
--- Begin Message ---
Beraru Liviu wrote:
Hello,
I am a newbie in php and have a problem with setting up a cookie.
The code should just verify if the user's browser has a cookie from my site,
and if there is one, then increase the counter file. If not, then the counter
should not be increased.
I have Xampp installed, the latest version, which means also php 5.
But, when I test the script I get from the my server the following error
message:
Warning: Cannot modify header information - headers already sent by (output started at C:\Programme\xampp\htdocs\Laborator\index.php:5) in C:\Programme\xampp\htdocs\Laborator\index.php on line 55
Line 55 is where stands the setcookie() method.
I have no idea what is wrong and what this error message means.
Could someone please help me?
The code I wrote is this one:
<snip code>
From the PHP manual page for setcookie (shocking that it would have
something useful for this problem isn't it!)...
"setcookie() defines a cookie to be sent along with the rest of the HTTP
headers. Like other headers, cookies must be sent before any output from
your script (this is a protocol restriction). This requires that you
place calls to this function prior to any output, including <html> and
<head> tags as well as any whitespace. If output exists prior to calling
this function, setcookie() will fail and return FALSE."
Read the manual page: http://php.net/setcookie - everything you need is
there.
-Stut
--- End Message ---
--- Begin Message ---
Hi
In the example code you sent, I can't see any reason for an error. Is
this a verbatim copy of your code?
As Stut implied, the reported error and the documentation tells you
everything you should need to know in this case, and as with any problem
the documentation should be your first resource. However the error is a
little vague as to whether the setcookie() is the cause of the error or
if header output is attempted afterwards. If this is not your complete
code, and the setcookie is the cause, then you have output before it
somewhere, else there is header output after it.
I think you might want to change your condition too.
> $cookie = $_COOKIE['cookie'];
this will set $cookie regardless (with the appropriate value or an
empty string), so that:
> if(!isset($cookie)){
always fails
it would be better to do
if(!isset($_COOKIE['cookie'])){
or
if(!empty($cookie)){
Niel
--- End Message ---
--- Begin Message ---
Hi,
Thank you for the reply.
The problem was in the line 55, as I told. This line was the one in which the
setcookie() was writen.
But now I solved the problem by just moving the whole script at the beginning
of the file, that is before the html and doctype tags. As is writen in the
documentation. (This documentation is great, by the way).
Cannot modify header information, as I understood in this way, means that
cookies actions, like setcookie(), are actions which take place before any html
document is created by php, which means that a setcookie method cannot take
place after the html code is created, that is when it is writen after any html
tag.
This make sense actually. Cookies are pieces of information that php needs in
order to create a "personalized" html document. Therefore, it makes no sense in
writing them after html tags.
Thanks once more for your support.
Liviu
Niel Archer <[EMAIL PROTECTED]> wrote:
Hi
In the example code you sent, I can't see any reason for an error. Is
this a verbatim copy of your code?
As Stut implied, the reported error and the documentation tells you
everything you should need to know in this case, and as with any problem
the documentation should be your first resource. However the error is a
little vague as to whether the setcookie() is the cause of the error or
if header output is attempted afterwards. If this is not your complete
code, and the setcookie is the cause, then you have output before it
somewhere, else there is header output after it.
I think you might want to change your condition too.
> $cookie = $_COOKIE['cookie'];
this will set $cookie regardless (with the appropriate value or an
empty string), so that:
> if(!isset($cookie)){
always fails
it would be better to do
if(!isset($_COOKIE['cookie'])){
or
if(!empty($cookie)){
Niel
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
--- End Message ---