Click to See Complete Forum and Search --> : Header Already Sent?


Jick
08-08-2003, 10:16 AM
I have my page (http://kd7pyo.infinitypages.com/) and I now have this counter script which will count all my unique hits to my website and log them:
<?PHP
$c_ip = $HTTP_COOKIE_VARS["user_ip"];
$counter_file = "count.txt";
$counter_file_line = file($counter_file); if(!$c_ip) {
setcookie("user_ip", $REMOTE_ADDR, time()+360000); $counter_file_line[0]++;
$cf = fopen($counter_file, "w+");
fputs($cf, "$counter_file_line[0]"); fclose($cf);
}
elseif($c_ip != $REMOTE_ADDR){
$counter_file_line[0]++; $cf = fopen($counter_file, "w+");
fputs($cf, "$counter_file_line[0]");
fclose($cf);
}
?>
If you go to my page you will see that I'm getting this error:

Warning: Cannot modify header information - headers already sent by (output started at /home/infini15/public_html/kd7pyo/index.php:9) in /home/infini15/public_html/kd7pyo/count.php on line 5

And I can't figure out what is wrong. Please help! :(

DaiWelsh
08-08-2003, 10:22 AM
In order to set a cookie you must do so before you send the http headers to the browser, which effectively means before you send any html at all to the browser (unless you are using buffering). Your html output probably starts with <html> or something similar which seems to be at index.php line 9 according to the error message. Either move count.php to be included before that line or move the html output to later.

HTH,

Dai

Jick
08-08-2003, 10:27 AM
Ok I figured out my problem you were right. I needed to move it to the top of the page. I now need a peice of code I can add to this that will go and look at the log file it wrote to and get the number thats in it and then echo it! Can you or some body else help me? Thanks. :)

pyro
08-08-2003, 10:51 AM
$contents = file("count.txt");
echo $contents[0];

Jick
08-08-2003, 10:58 AM
Thanks alot. It works now. I'm done with all my counter threds and post now that I have this working. Thanks. :D

Would you guys mind going to my page so I can see if it's workin right?

http://kd7pyo.infinitypages.com/

Jick
08-08-2003, 11:37 AM
I have a contact page thats in another dir and it's displaying this error:

Warning: file(count.txt): failed to open stream: No such file or directory in /home/infini15/public_html/kd7pyo/count.php on line 4

I think it's because the way I have the code it thinks there is a file called count.txt in the same dir when it is actually in the base dir so how can I fix the count.php file so that if there are pages in other dir's it will still write to the count.txt file in the base dir? Thanks. :D

Here is my current code so you can help me fix it:
<?PHP
$c_ip = $HTTP_COOKIE_VARS["user_ip"];
$counter_file = "count.txt";
$counter_file_line = file($counter_file); if(!$c_ip) {
setcookie("user_ip", $REMOTE_ADDR, time()+360000); $counter_file_line[0]++;
$cf = fopen($counter_file, "w+");
fputs($cf, "$counter_file_line[0]"); fclose($cf);
}
elseif($c_ip != $REMOTE_ADDR){
$counter_file_line[0]++; $cf = fopen($counter_file, "w+");
fputs($cf, "$counter_file_line[0]");
fclose($cf);
}
?>And here is the code I'm using to include it in the files which works fine for files that are in the same dir as the count.txt:
<?PHP include ("count.php"); ?>And then on pages I display the number on I use the code above and then put this where I want to display the number:
<?PHP echo "$counter_file_line[0]"; ?>This also works on files that are in the same dir. Please help. Thank you. :)

pyro
08-08-2003, 11:56 AM
Read up on the difference between relative and absolute paths.

Basically, since you are using a relative path, it will only work inside the one directory. If you want it to work in all directorys, change the paths to the files to absolute paths (either through the document root, or with a http address)