PDA

Click to See Complete Forum and Search --> : [RESOLVED] Ubuntu doesn't like cookies...


ididntdoit
Aug 4th, 2008, 08:06 PM
I had a home web server running on Windows XP recently, but I decided to move it to Ubuntu Linux, so I made an image of the hard drive, then installed Ubuntu, now after installing Apache2 and PHP5, I copied back all of my server files (.html and .php mostly), but now I am getting some errors for no apparent reason. I get an error in my PHP code whenever I try to make a cookie. I never had this problem before, and I changed no code...

My counter file is below, and I called it like "<?php $counterfile = "/home/browner87/Documents/apache2dir/counter/counter.dat"; $page = "home"; include'counter/counter.php'; ?>"

$imagedirectory = "/counter/";
$digits = 4;

//print_r(error_get_last());

$opencount = fopen($counterfile, "r");
$currentcount = fread($opencount, filesize($counterfile));
if($_COOKIE["$page"] != "set")
{
setcookie("$page", "set", 0);
++$currentcount;

//$opencount = fopen($counterfile, "w");
fwrite($opencount, $currentcount);
//fclose($opencount);
}
fclose($opencount); //

//print_r($currentcount);

$newcount = $currentcount;
$numdigits = strlen($newcount);
if ($numdigits > $digits) {
$newcount = "0";
} else {
}
$actdigits = strlen($newcount);

for ($a=0; ; $a++)
{
if ($a == $digits)
{
break;
}
else
{
$img[$a] = "$imagedirectory";
$img[$a] .= "0.gif";
}
}

for ($a=0; ; $a++)
{
if ($a == $actdigits)
{
break;
}
else
{
$showdig = substr($newcount, $a, 1);
$img[$digits - $actdigits + $a] = "$imagedirectory";
$img[$digits - $actdigits + $a] .= "$showdig.gif";
}
}

$displayblock = "";
for ($a=0; $a < $digits; $a++)
{
$displayblock .= "<td><img src=$img[$a]></td>";
}

$displayblock .= "<br />";
//$opennewcount = fopen($counterfile,"w");
//fwrite($opennewcount, $newcount);
//fclose ($opennewcount);
echo $displayblock

I read a bit about not being able to make cookies after printing to the screen, but I don't want to rewrite my whole script from scratch (and rewrite every page with a counter on it). SO, what changed from going from XP to Ubuntu? Does XP have a hidden extra ability to send cookies after the header is gone? Can anyone provide a simple solution?

visualAd
Aug 5th, 2008, 02:27 AM
The "simple" solution is to use an output buffer (http://www.php.net/output-buffering), which, was probably enabled by default in the version of PHP you were running on XP. That aside, there is no reason why, provided the scripts are written properly that you should need to send a cookie after you have started page output.

A tool such as a counter is easily implemented in this way and setting a global or template variable is all that is required to display the information in the later output. So my suggestion is, instead of taking the simple solution of putting ob_start(); at the beginning of every page, you rework your script so it follows best practice.

P.s: there is no hidden abilities in XP / Apache that prevent or allow the sending of cookies after output has started as cookies are sent within the header using the Set-Cookie field. An HTTP response / request is formatted (see below) in such a way that the headers come before the body; headers by definition and as stated in RFC 2616 cannot be sent to the client once you have started sending the body. Output buffering simply causes PHP to hold all the output until the script has finished; once again however, a properly written script should only ever need an output buffer for performance enhancing reasons.

HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Date: Tue, 05 Aug 2008 07:26:29 GMT
Expires: -1
Content-Type: text/html; charset=UTF-8
Set-Cookie: PREF=ID=10fa357da548a300:TM=1217921189:LM=1217921189:S=GWyPvAxJ5_2LsyHl; expires=Thu, 05-Aug-2010 07:26:29 GMT; path=/; domain=.google.fr
Content-Encoding: gzip
Server: gws
Content-Length: 2743

<html>
<head>
....

ididntdoit
Aug 5th, 2008, 05:00 PM
Thank you. I have used a simple output buffering for a bandaid solution, but I'll get around to rewriting the code soon. The thing is that I never properly learned PHP, it's just similar to Java so I can kinda improvise. When I get time I'll look into how exactly how to get it working more smoothly. Thanks!