/    Sign up×
Community /Pin to ProfileBookmark

Header Already Sent?

I have [URL=http://kd7pyo.infinitypages.com/]my page[/URL] and I now have this counter script which will count all my unique hits to my website and log them:

[code=php]<?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);
}
?>[/code]

If you go to my page you will see that I’m getting this error:

[B]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[/B]

And I can’t figure out what is wrong. Please help! ?

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@DaiWelshAug 08.2003 — 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
Copy linkTweet thisAlerts:
@JickauthorAug 08.2003 — 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. ?
Copy linkTweet thisAlerts:
@pyroAug 08.2003 — [code=php]$contents = file("count.txt");
echo $contents[0];[/code]
Copy linkTweet thisAlerts:
@JickauthorAug 08.2003 — Thanks alot. It works now. I'm done with all my counter threds and post now that I have this working. Thanks. ?

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

[URL]http://kd7pyo.infinitypages.com/[/URL]
Copy linkTweet thisAlerts:
@JickauthorAug 08.2003 — I have a contact page thats in another dir and it's displaying this error:

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

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

Here is my current code so you can help me fix it:
[code=php]<?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);
}
?>[/code]
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 [B]count.txt[/B]:
[code=php]<?PHP include ("count.php"); ?>[/code]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:
[code=php]<?PHP echo "$counter_file_line[0]"; ?>[/code]This also works on files that are in the same dir. Please help. Thank you. ?
Copy linkTweet thisAlerts:
@pyroAug 08.2003 — 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)
×

Success!

Help @Jick spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...