PHP Text Counter unique reporting fails (still increments)

In case you have problems with some other PHPJunkyard script (not that there are THAT many).
Post Reply
MtheK
Posts: 5
Joined: Fri Dec 19, 2014 6:22 pm

PHP Text Counter unique reporting fails (still increments)

Post by MtheK »

Script URL: counter.php
Version of script: 1.6
Hosting company:
URL of phpinfo.php:
URL of session_test.php:
What terms did you try when SEARCHING for a solution: PHP Text Counter
Write your message below:

The .php I downloaded this week (1.6) has an error when counting unique users;
it would still increment regardless. I was forced to change it:

// If counting unique hits is enabled make sure it's a unique hit
if ( $count_unique == 1 )
{
if ( ! isset($_COOKIE[$cname]))
{
// Update count by 1 and write the new value to the log file

if ( ! isset($_GET['skip']) )
{
$count = $count + 1;
rewind($fp);
fwrite($fp, $count);
}
else
{
$count = $count + 0;
rewind($fp);
fwrite($fp, $count);
}

// Print the Cookie and P3P compact privacy policy
header('P3P: CP="NOI NID"');
setcookie($cname, 1, time()+60*60*$unique_hours);

}
else
{

// dummy

}
}
else
{
if ( ! isset($_GET['skip']) )
{
$count = $count + 1;
rewind($fp);
fwrite($fp, $count);
}
else
{
$count = $count + 0;
rewind($fp);
fwrite($fp, $count);
}
}

I'm sure you can write a more efficient way of doing it, as this is my
first .php, but this now works for me correctly; hitting refresh on my
browser does NOT increment it, but clearing the cookies and re-doing it
DOES increment it once, as I believe it should. I'm very familiar with
IF structured-programming constructs w/multiple arguments causing
duplicate code in BAL and MASM (macros help).

While I was at it, I need to only view the counter for tracking.
I added the 'skip' parameter; if it exists, do NOT increment the counter.

Otherwise, this is a very nice piece of code. Just gotta remember CHMOD!
My ISP had removed counter support and I was forced into replacing it;
I chose your product. Thankx.
Klemen
Site Admin
Posts: 10116
Joined: Fri Feb 11, 2005 4:04 pm

Re: PHP Text Counter unique reporting fails (still increment

Post by Klemen »

The original code works fine for me, are you sure you had $count_unique set to 1 and a valid number for $unique_hours variable?

Also, make sure you don't make any other changes to the file as premature sending out of http headers may cause problems setting cookies.

I'm not sure how/when you append the "skip" parameter to the query string, but if it's there on all pages your counter may not be counting anything.
Klemen, creator of HESK and PHPJunkyardWas this helpful? You can buy me a drink here Image

Image You should follow me on Twitter here

Help desk software | Cloud help desk | Guestbook | Link manager | Click counter | more PHP Scripts ...

Also browse for php hosting companies, read php books, find php resources and use webmaster tools
MtheK
Posts: 5
Joined: Fri Dec 19, 2014 6:22 pm

Re: PHP Text Counter unique reporting fails (still increment

Post by MtheK »

Yes, it was 1.

Yes, I left it as 24:

// Count unique visitors? 1 = YES, 0 = NO
$count_unique = 1;

// Number of hours a visitor is considered as "unique"
$unique_hours = 24;


At first, I copied it un-altered to my ISP and it fails
(it increments). OK, at least it works. Then I changed
that '1' and it still failed. Then I replaced your
section of code with mine, and it then worked.

EXACTLY! The 'skip' does NOT increment, which is what I want.
I use my own page w/the 'skip' parm added. "Normal" pages
do not have it and so increment.

Perhaps my ISP has an older PHP translator that fails to
recognize your IF statement properly...
MtheK
Posts: 5
Joined: Fri Dec 19, 2014 6:22 pm

Re: PHP Text Counter unique reporting fails (still increment

Post by MtheK »

I found the problem: CHMOD! I noticed it during installation
when the target DIRs' content wasn't changing(?) tho it said it
did it. Took several attempts. So, I deleted everything and re-installed
it from scratch. This time, since it didn't return a number, I changed
CHMOD individually, one at a time, until it returned a number. 555 for
the top-level DIR and the .php (this was the key!), and 777 for the \logs
DIR and test.txt. This then worked. I then changed the '1' and uploaded it,
but, due to the 555, it SAID it did it but the DIRs' content didn't change.
I viewed it and it was indeed still a 0, so I changed the .php to 755,
re-uploaded it (this time, the DIR's content changed, and View showed a 1),
and re-tested. Now it works as expected. So, I added my 'skip' parameter
code, uploaded it, and re-tested. Now all functions work as expected.
Thankx.
MtheK
Posts: 5
Joined: Fri Dec 19, 2014 6:22 pm

Re: PHP Text Counter unique reporting fails (still increment

Post by MtheK »

Spoke too soon. This works great in IE8, but IE9 fails to even return a number.
F12 says: SEC7112: Script from ... was blocked due to mime type mismatch

???
Klemen
Site Admin
Posts: 10116
Joined: Fri Feb 11, 2005 4:04 pm

Re: PHP Text Counter unique reporting fails (still increment

Post by Klemen »

Try adding

Code: Select all

header("Content-type: text/javascript");
to the top of your counter.php file (just below error_reporting(E_ALL ^ E_NOTICE); )

Also, add type="text/javascript" to your counter script code:

Code: Select all

<script type="text/javascript" src="http://example.com/counter/counter.php?page=test"></script>
Klemen, creator of HESK and PHPJunkyardWas this helpful? You can buy me a drink here Image

Image You should follow me on Twitter here

Help desk software | Cloud help desk | Guestbook | Link manager | Click counter | more PHP Scripts ...

Also browse for php hosting companies, read php books, find php resources and use webmaster tools
MtheK
Posts: 5
Joined: Fri Dec 19, 2014 6:22 pm

Re: PHP Text Counter unique reporting fails (still increment

Post by MtheK »

TA-DA! That worked perfectly. It now works on both IE8 and IE9.
FYI: the 'enable mime sniffing' in IE9's options is 'disable'; I left it as it was.
Thank you!
Post Reply