setcookie() not working - help!
setcookie() not working - help!
am 28.12.2006 01:24:53 von Beraru Liviu
--0-497580242-1167265493=:43405
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
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:
$file="counter.txt";
if (!file_exists($file)){
$ix=fopen($file, "w");
fwrite($ix, "0");
fclose($ix);
}
$ix=fopen($file, "r");
$counter=fread($ix, 10);
fclose($ix);
$cookie = $_COOKIE['cookie'];
if(!isset($cookie)){
//FOLLOWS LINE 55:
setcookie ("cookie" , "ok");
$counter++;
$ix=fopen($file, "w");
fwrite($ix, $counter);
fclose($ix);
}
$lung=strlen($counter);
$counter_grafic="";
for ($pos=0; $pos<$lung; $pos++){
$cifra=substr($counter, $pos, 1);
$counter_grafic.="";
}
?>
Liviu Beraru
__________________________________________________
Do You Yahoo!?
Tired of spam? Yahoo! Mail has the best spam protection around
http://mail.yahoo.com
--0-497580242-1167265493=:43405--
Re: setcookie() not working - help!
am 28.12.2006 15:49:47 von Stut
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:
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 and
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
--
PHP Windows Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php
Re: setcookie() not working - help!
am 28.12.2006 19:41:17 von Niel Archer
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
Re: setcookie() not working - help!
am 29.12.2006 00:25:00 von Beraru Liviu
--0-1637112001-1167348300=:82478
Content-Type: text/plain; charset=iso-8859-1
Content-Transfer-Encoding: 8bit
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 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
--0-1637112001-1167348300=:82478--