count resets to 1 every now and then

Post your Click counter digestion problems here
Post Reply
armandt
Posts: 1
Joined: Thu May 11, 2006 9:33 am

count resets to 1 every now and then

Post by armandt »

Script URL:
Version of script:
Version of PHP:
Hosting company:
Have you searched THIS FORUM for your problem:
(if not please do before posting)
If so, what terms did you try:

Write your message below:

Hi there, it often happens that my counter resets to 1, I'm not sure if has got something to do with this line of code:

// If the log file doesn't exist we start count from 1 ...
if (! @$file = fopen($logfile,"r+"))
{
$count="1";
}

Can I just comment out the //$count="1"; part or what must I do? I'm not sure why this happens, maby the countet gets triggered from another page than the index page, because my link was setup like this: gcounter/graphcount.php?page=index.

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

Post by Klemen »

What probably happens is your log file gets corrupted because you are counting a lot of visits. Text databases have limits when it comes to performance and security, you should get a MySQL counter.
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
Jive
Posts: 7
Joined: Sat May 13, 2006 2:33 pm

Post by Jive »

I have myself to suffer of this very bad event!!!
Regularily, I find my page with a counter resetted to zero!!!

http://doomwadstation.com/dlw/Swl.html
You should get a MySQL counter.
It's not fair as answer!
You didn't warn us of it, and now all my links are changed, so that ccount can operate.
It was a huge work, and the idea that it was for nothing is simply terrible...

Can you explain a bit more what is happening?
There is nothing to do?!?
:shock:
Hello from Amazonia!
zr
Posts: 6
Joined: Sat May 06, 2006 10:40 pm

Post by zr »

Not sure if this would work, but try this...

Create a new database. Insert a table called "count" with one field called "page" with the attributes "VARCHAR 15"

Then replace counter.php with this...

Code: Select all


<?php


# PHP hit counter (PHPcount)
# Version: 1.0
# File name: counter.php
# Written 15th July 2004 by Klemen Stirn (info@phpjunkyard.com)
# Modified for MySQL use 14 May 2006 by zr
# http://www.PHPJunkYard.com

##############################################################################
# COPYRIGHT NOTICE                                                           #
# Copyright 2004 PHPJunkYard All Rights Reserved.                            #
#                                                                            #
# The PHPcount may be used and modified free of charge by anyone so long as  #
# this copyright notice and the comments above remain intact. By using this  #
# code you agree to indemnify Klemen Stirn from any liability that might     #
# arise from it's use.                                                       #
#                                                                            #
# Selling the code for this program without prior written consent is         #
# expressly forbidden. In other words, please ask first before you try and   #
# make money off this program.                                               #
#                                                                            #
# Obtain permission before redistributing this software over the Internet or #
# in any other medium. In all cases copyright and header must remain intact. #
# This Copyright is in full effect in any country that has International     #
# Trade Agreements with the United States of America or with                 #
# the European Union.                                                        #
##############################################################################

// SETUP YOUR COUNTER
// Detailed information found in the readme file

// Enable referer validation? 1 = YES, 0 = NO
$check_referer = 0;
// Domains that are allowed to access this script
$referers = array ("localhost","yourdomain.com");

//Enter MySQL Info

$user = "db_username";
$pass = "db_password";
$name = "db_name";
$host = "localhost";


#############################
#     DO NOT EDIT BELOW     #
#############################


$dbc = @mysql_connect ($host, $user, DB_$pass) OR die ('document.write("Could not connect to MySQL: ' . mysql_error() . '")');

mysql_select_db (DB_NAMES) OR die ('document.write("Could not select DB: ' . mysql_error() . '")');

// Get page and log file names
$page = htmlentities($_GET['page']);

// If $check_referer is set to 1 and if HTTP_REFERER is set to
// a value let's check refering site
if ($check_referer == 1 && !(empty($_SERVER['HTTP_REFERER'])))
{
check_referer($_SERVER['HTTP_REFERER']);
}


//add 1 to count
$n = @mysql_query ("INSERT INTO count (page) VALUES ('$page');");


//get count
$c = @mysql_query ("SELECT * FROM count WHERE page='$page';");

$count = mysql_num_rows($c);

// Print out Javascript code and exit
echo "document.write('$count');";
exit();

// function that will check refering URL
function check_referer($thisurl) {
	global $referers;
		for ($i=0;$i<count($referers);$i++)
        	{
				if (preg_match("/$referers[$i]/i",$thisurl)) {return true;}
			}
	die("Invalid referer!");
}
?>

Jive
Posts: 7
Joined: Sat May 13, 2006 2:33 pm

Post by Jive »

Thank you a lot for your answer, zr.

But I selected ccount simply because I'm an ass with the mysql thingy...
I know nothing about it, and your solution is for mysql... May I tell you that you are out of subject? :oops:

The author claimed that his script was made for text files, and not for databases and mysql !!! It's why I was enthousiast, and why I cry today when I see the counter resetted to zero pretty 2 or 3 times per week !!!
Today, this counter is out of interest, because the count is FALSE, now, after ALL of the resets.

And yes, I'm very anxious of this situation: I changed a lot of things on my code because of it, and I have now problem after problem!!!

I repeat that you could have WARNED us of this possible event !!! :evil:
If I had been informed of it, I would NEVER have tried to implement this code. NEVER!!!
Now, bad time is here.
To retrogress will bring big difficulties, and a large work to me (do have a look at the url given upper, and you'll understand why)

Sorry to be so rude, but I'm disgussed and disappointed... :(
Hello from Amazonia!
Klemen
Site Admin
Posts: 10116
Joined: Fri Feb 11, 2005 4:04 pm

Post by Klemen »

Jive,

Firstly, whenever you post on this board keep in mind these are FREE scripts and you get FREE support, so being rude is not exactly the right attitude to post here. Just because the scripts are pre-written doesn't mean you should abandon your ability to learn and research things you use.

Secondly, limitations of text databases are not a special "bug" of my scripts, but a problem each and every text database script encounters when put under heavy load. What heavy load is depends on script itself and a lot of the server.

Thirdly, are you sure you are using CCount? From what I've read so far I would say you are using text hit counter and not CCount (Click counter)? If you don't even take the time to read which forum you post to, how can I expect you to read any "warnings" about text databases or any of the XY posts about text database limitations on this forum?

Fourthly, with a little PHP knowledge you could simply edit the code to ignore counter resets in case the file can't be opened. For example a simple change from

Code: Select all

	// If the log file doesn't exist we start count from 1 ...
	if (! @$file = fopen($logfile,'r+'))
	{
		$count=1;
	}
	// If the log file exist lets read count from it
	else {
		$count = @fread($file, filesize($logfile)) or $count=0;
		fclose($file);
		// Raise the value of $count by 1
		$count++;
	}

	// Write the new $count in the log file
	$file = fopen($logfile,'w+') or die('Can\'t open/write the log file, please CHMOD logs folder to 777 (rwx-rwx-rwx)!');
	fputs($file, $count);
	fclose($file);
to

Code: Select all

	// If the log file doesn't exist we start count from 1 ...
	if (@$file = fopen($logfile,'r+'))
	{
		$count = @fread($file, filesize($logfile)) or $count=0;
		fclose($file);
		// Raise the value of $count by 1
		$count++;

	    // Write the new $count in the log file
	    $file = fopen($logfile,'w+') or die('Can\'t open/write the log file, please CHMOD logs folder to 777 (rwx-rwx-rwx)!');
	    fputs($file, $count);
	    fclose($file);
	}
should work fine and not reset the counter, but you will manually have to create any log files (or with the original counter.php file).
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
Jive
Posts: 7
Joined: Sat May 13, 2006 2:33 pm

Post by Jive »

Ccount 1.1
PHP click counter

PHP click counter (Ccount)
Version: 1.1 from 22nd January 2005
Originally written on 13th July 2004 by Klemen Stirn (info AT phpjunkyard DOT com)
and
<script language="Javascript">ccount_display('LINK_ID')</script>
Here are texts extracted from your html page.
So yes, I'm talking about PHP click counter (Ccount)

Yes, your scripts are FREE, ok!
Anyway, I don't want to debate about it now.
My sites are free also, like yours... And my work is free also, ok...

Of course, with the frequentation of my sites, I'm not ready to make experimentations, or to make heavy changes for nothing.
I made heavy changes, and you answered firstly that we have to use mysql to solve the problem.
I'm using your script simply because it doesn't use mysql !!!

This problem got my nerves!!
I thought that someone was deleting the files, and I advised my host about it. Yes, I know: I'm a dumbass!!!

You put your script under a Copyright, right?
So, I guess that you are sure that your scripts are efficient for everybody.

That said, I thank you a lot for your effort to give me a possible answer with the new change of the script.
I'll try it as soon as possible, and I'll tell you the result.

One more time: thank you! :wink:

P.S. : I have the same problem with your counter of visits (hit counter)... Resets with the same frequency.
PHPcount 1.0
PHP text hit counter

PHP hit counter (PHPcount)
Version: 1.0
File name: counter.php
Written 15th July 2004 by Klemen Stirn (info AT phpjunkyard DOT com)
Hello from Amazonia!
Klemen
Site Admin
Posts: 10116
Joined: Fri Feb 11, 2005 4:04 pm

Post by Klemen »

Sorry, the first post (about text counter) is posted by armandt and not you.
I'm using your script simply because it doesn't use mysql !!!
The answer remains the same - if you have a high traffic website or count clicks on many links you shouldn't use text database scripts. Not CCount nor any other. You should use MySQL from the start and this is something you obviously learned the hard way. If you don't have MySQL - get it, this is the only way you will be able to ensure smooth database operation and is something you should have considered before implementing any scripts.

In my honest opinion you should get a MySQL script and stop using ANY text database ones as your website is obviously causing too much load on the text database(s). And you should consider/research what databases/scripts to use depending on the load/traffic of your website.
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
Jive
Posts: 7
Joined: Sat May 13, 2006 2:33 pm

Post by Jive »

Ok!
Thank you for your honest answer!

Please, can you add an advise in your files, so that this bad issue will not be experimented by other ones? :cry:

If you have time, I will be glad to learn from you why text files are limited for such a purpose, and what is happening.

"Merci" for the time passed to read my comments, and to try to be of some help for my very specific problem.
Hello from Amazonia!
Post Reply