Unable to set cookies from php code.

Status
Not open for further replies.

talk2azs

Member
Messages
84
Reaction score
4
Points
8
Was the php server updated over the weekend? My code is no longer able to set a cookie in a visitors browser! It seems that your PHP server is now sending a header to the browser before I set my header later in my code inside other files. Many of my php files are now broken because of this issue. Just for grins, I verified this behavior with a simple php cookie checker I just wrote, which generates a random number, sets the cookie and then tries to read the cookie.
Here is the simple code in a file named cc.php on my root public-html folder:
============
<?php
$ccheck = rand(10000000, 99999997);
setcookie('ccheck',$ccheck,time() + 86400); // 86400 = 1 day
$ccheck = $_COOKIE['ccheck'];
if ($ccheck == null) { $ccheck = 0; } // If ccheck equals 0 then cookies are NOT enabled
else { $ccheck = 1; } // If ccheck equals 1 then cookies are enabled
?>
============
THIS IS WHAT THE PHP SERVER RETURNS TO ALL OF MY BROWSER ON ALL DEVICES:
Warning: Cannot modify header information - headers already sent by (output started at /home/talk2azs/public_html/cc.php:2) in /home/talk2azs/public_html/cc.php on line 8

As you can see in my simple code, I am not sending a header.

Please look into this and let me know if this can be fixed as soon as possible. Many of my web apps are now broken because of what happened Friday night. Everything was working prior to Saturday morning.

Thanks
 

descalzo

Grim Squeaker
Community Support
Messages
9,373
Reaction score
326
Points
83
In your test script, make sure there is nothing before the opening <?php

Not a blank line. Not a space. Not a BOM (byte order mark, an unprintable character that some text editors add to the beginning of text documents).

If there is anything, then it gets sent to output. Which automatically sends out headers. Then setting cookies causes the error message.
 

talk2azs

Member
Messages
84
Reaction score
4
Points
8
The was the first thing I made sure of. There is nothing before the '<?php'

Thanks for replying with that suggestion though. :)

Like I said, all of my scripts were working prior to Saturday morning. I changed nothing.
 

Dead-i

x10Hosting Support Ninja
Community Support
Messages
6,084
Reaction score
368
Points
83
Hi talk2azs,

I checked your PHP script, and what @descalzo said is correct. Before your <?php tag, you have a BOM, likely due to a misconfigured editor. ;)

Thank you,
 

talk2azs

Member
Messages
84
Reaction score
4
Points
8
Thanks guys for your quick response... You are the best! Turns out my editor defaulted back to it's installation defaults. I guess I just needed fresh eyes in this case.
 
Status
Not open for further replies.
Top