473,785 Members | 2,446 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Help on click tracking code

I write a piece of code to tracking the number of clicks on a link
each day. The link usu. gets about 3000-6000 clicks per day. I just
store today's click count in a one-line text file, like,

01/07/2004 1234

The first field is the date and the second field is number of clicks.
I just read the current count, increase by 1 and then write back. When
another day comes, I write today's count to mysql DB.

The problem I am having is that sometimes during a single day, the
click count seems to be reset to zero and starts counting from zero
again. Normally I'd get at least 3000 clicks a day. But sometimes, the
clicks I've got are only around 600. I am sure there is something
wrong with my code. I've pasted my code. I'd appreciate if sombody can
help me with some tips.

$hitcount=0;
$today=date("m/d/Y");
$hitdate=$today ;

$fdaycount="day count.txt";

if (file_exists($f daycount))
{
//read out today's count from file
$handle=fopen($ fdaycount,"r");
if (!$handle) exit(0);
if (!feof($handle) )
{
$s=fgets($handl e);
$p=strpos($s," ");
$hitdate=substr ($s,0,$p);
$hitcount=(inte ger)substr($s,$ p+1);
}
fclose($handle) ;
}

if ($hitdate==$tod ay)
{
//increase todays' count by 1
$hitcount++;
$fp=fopen($fday count,"w");
if (!$fp) exit(0);
$s=$today." ".$hitcount ;
fputs($fp,$s);
fclose($fp);
}
else
{
//write today's data into database
.....
//

//reset today's count to 1
$fp=fopen($fday count,"w");
if (!$fp) exit(0);
$s=$today." 1";
fputs($fp,$s);
fclose($fp);
}
Jul 17 '05 #1
3 2437
"Shanfeng Cheng" <sh***********@ yahoo.com> wrote in message
news:c8******** *************** ***@posting.goo gle.com...
I write a piece of code to tracking the number of clicks on a link
each day. The link usu. gets about 3000-6000 clicks per day. I just
store today's click count in a one-line text file, like,

01/07/2004 1234

The first field is the date and the second field is number of clicks.
I just read the current count, increase by 1 and then write back. When
another day comes, I write today's count to mysql DB.

The problem I am having is that sometimes during a single day, the
click count seems to be reset to zero and starts counting from zero
again. Normally I'd get at least 3000 clicks a day. But sometimes, the
clicks I've got are only around 600. I am sure there is something
wrong with my code. I've pasted my code. I'd appreciate if sombody can
help me with some tips.

$hitcount=0;
$today=date("m/d/Y");
$hitdate=$today ;

$fdaycount="day count.txt";

if (file_exists($f daycount))
{
//read out today's count from file
$handle=fopen($ fdaycount,"r");
if (!$handle) exit(0);
if (!feof($handle) )
{
$s=fgets($handl e);
$p=strpos($s," ");
$hitdate=substr ($s,0,$p);
$hitcount=(inte ger)substr($s,$ p+1);
}
fclose($handle) ;
}

if ($hitdate==$tod ay)
{
//increase todays' count by 1
$hitcount++;
$fp=fopen($fday count,"w");
if (!$fp) exit(0);
$s=$today." ".$hitcount ;
fputs($fp,$s);
fclose($fp);
}
else
{
//write today's data into database
.....
//

//reset today's count to 1
$fp=fopen($fday count,"w");
if (!$fp) exit(0);
$s=$today." 1";
fputs($fp,$s);
fclose($fp);
}


Proccess the logs, its cleaner,

run a cron job, and place the results in your dbase
--
Mike Bradley
http://www.gzentools.com -- free online php tools
Jul 17 '05 #2
Look what happens in the code when the file doesn't exist. Now consider
what happens if two (or two hundred!) sessions try to open the file at the
same time. What happens when you use file_exists() on a file that someone
else has already opened for write? Does file_exist return false? Out of
200 simultaneous attempts, how many actually would succeed in updating the
counter?

You've got a database - use that and manage the count like this: UPDATE
hitcountertable SET hitcount = hitcount +1. The DBMS will serialize access
to the hitcount, be faster and work better.
--

"Shanfeng Cheng" <sh***********@ yahoo.com> wrote in message
news:c8******** *************** ***@posting.goo gle.com...
I write a piece of code to tracking the number of clicks on a link
each day. The link usu. gets about 3000-6000 clicks per day. I just
store today's click count in a one-line text file, like,

01/07/2004 1234

The first field is the date and the second field is number of clicks.
I just read the current count, increase by 1 and then write back. When
another day comes, I write today's count to mysql DB.

The problem I am having is that sometimes during a single day, the
click count seems to be reset to zero and starts counting from zero
again. Normally I'd get at least 3000 clicks a day. But sometimes, the
clicks I've got are only around 600. I am sure there is something
wrong with my code. I've pasted my code. I'd appreciate if sombody can
help me with some tips.

$hitcount=0;
$today=date("m/d/Y");
$hitdate=$today ;

$fdaycount="day count.txt";

if (file_exists($f daycount))
{
//read out today's count from file
$handle=fopen($ fdaycount,"r");
if (!$handle) exit(0);
if (!feof($handle) )
{
$s=fgets($handl e);
$p=strpos($s," ");
$hitdate=substr ($s,0,$p);
$hitcount=(inte ger)substr($s,$ p+1);
}
fclose($handle) ;
}

if ($hitdate==$tod ay)
{
//increase todays' count by 1
$hitcount++;
$fp=fopen($fday count,"w");
if (!$fp) exit(0);
$s=$today." ".$hitcount ;
fputs($fp,$s);
fclose($fp);
}
else
{
//write today's data into database
.....
//

//reset today's count to 1
$fp=fopen($fday count,"w");
if (!$fp) exit(0);
$s=$today." 1";
fputs($fp,$s);
fclose($fp);
}

Jul 17 '05 #3
Thanks. I did find problem with the file_exists() function call. It
does return false sometimes.

But I don't know why updating a database field can be faster. You have
to take time making the database connection each time. And somehow my
ISP limits the number of simultaneous MySQL connections.
"Herb Kauhry" <ti****@tulips. me> wrote in message news:<PO******* *************@c omcast.com>...
Look what happens in the code when the file doesn't exist. Now consider
what happens if two (or two hundred!) sessions try to open the file at the
same time. What happens when you use file_exists() on a file that someone
else has already opened for write? Does file_exist return false? Out of
200 simultaneous attempts, how many actually would succeed in updating the
counter?

You've got a database - use that and manage the count like this: UPDATE
hitcountertable SET hitcount = hitcount +1. The DBMS will serialize access
to the hitcount, be faster and work better.
--

"Shanfeng Cheng" <sh***********@ yahoo.com> wrote in message
news:c8******** *************** ***@posting.goo gle.com...
I write a piece of code to tracking the number of clicks on a link
each day. The link usu. gets about 3000-6000 clicks per day. I just
store today's click count in a one-line text file, like,

01/07/2004 1234

The first field is the date and the second field is number of clicks.
I just read the current count, increase by 1 and then write back. When
another day comes, I write today's count to mysql DB.

The problem I am having is that sometimes during a single day, the
click count seems to be reset to zero and starts counting from zero
again. Normally I'd get at least 3000 clicks a day. But sometimes, the
clicks I've got are only around 600. I am sure there is something
wrong with my code. I've pasted my code. I'd appreciate if sombody can
help me with some tips.

$hitcount=0;
$today=date("m/d/Y");
$hitdate=$today ;

$fdaycount="day count.txt";

if (file_exists($f daycount))
{
//read out today's count from file
$handle=fopen($ fdaycount,"r");
if (!$handle) exit(0);
if (!feof($handle) )
{
$s=fgets($handl e);
$p=strpos($s," ");
$hitdate=substr ($s,0,$p);
$hitcount=(inte ger)substr($s,$ p+1);
}

fclose($handle) ;
}

if ($hitdate==$tod ay)
{
//increase todays' count by 1
$hitcount++;
$fp=fopen($fday count,"w");
if (!$fp) exit(0);
$s=$today." ".$hitcount ;
fputs($fp,$s);
fclose($fp);
}
else
{
//write today's data into database
.....
//

//reset today's count to 1
$fp=fopen($fday count,"w");
if (!$fp) exit(0);
$s=$today." 1";
fputs($fp,$s);
fclose($fp);
}

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2563
by: mark | last post by:
I've been working on an Access 2000 database for a couple of weeks now. I took a course in access about a year ago, a crash course, and I learned a ton, but I didn't touch Access for the year since then so I forgot everything I learned : ( So this little project I envisioned has turned out to be much harder than I thought. But I think I'm close with it, so I want to see it through. What I wanted to create is a database that will track my...
7
1885
by: Hal | last post by:
Hi, Please see http://webmonky.myby.co.uk/problem.JPG I have 1 Text Box in a form (ms access), one for an Order # .I currently have the form working so when I enter (or scan) an order number, that order number (which is a field in a record) is marked as DESPACTHED in a Tickbox Field under that record. Here is the code..
4
1454
by: Henry Stockbridge | last post by:
Hi, I am working on a query that displays depleted inventory. I need the criteria to be: CURRENT WEEK (Format(Now(),"ww") QTY = 0 and PREVIOUS WEEK (Format(Now(),"ww")-1) QTY <> 0. Ultimately, the query results would be similar to this:
2
1948
by: MyNameIsnt | last post by:
Can anyone tell me why, when I click on the buttons it register 2 characters on the display? if you use the right mousebutton it works ok, but the buttons dont flash?? it works fine without the aqua buttons, (using just windows forms buttons... Main Form cs file //----------------------------------------------------------------------------------------------------------------------
2
2298
by: Lior | last post by:
Hi, I have an ASP.NET website that crashes under heavy load. I use a SQL Server DB. I get around 5500 hits per day. I keep getting the timeout expieried connection pool error. Sometimes it even throws and error about a DataReader connection being already open even though I only use Data Sets in my code. Please take a look and see if you can find my leak because I'm going nuts here and am losing hope... I keep blaming the server and the...
3
1472
by: sunnysails | last post by:
Hi, is there a javascript function which tells the mouse pointer to "click" whereever it is on the screen? Best regards-sunnysails
2
1450
by: settyv | last post by:
Hi, I have webform which has 4 Datagrid controls with (built-in Pagination) and 4 search buttons to perform the search operation.When i click on search operation it is displaying the results in grid .when i click next it is telling error...Please let me know how to solve this problem. Below method can be used for all the datagrids.
5
2275
by: Paul M | last post by:
Hi I need to create a map for a room (chairs, bed, TV, forniture, etc). All object allready exists like png images. I need to create a map for each room, i mean the user need to create. How can I do this? TIA, Paul
2
2232
by: tonsi | last post by:
I have a page but it wont center the div. Can anyone take a look at it and help. When I try to center in the body using text-align: center; it doesn't work with the main container (called Table_01). Please help! I would be ever so greatful. The page is located here: http://www.cpiequipment.com/test_site/index.html Here is the code: <html> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
0
9647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9491
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10163
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10104
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9959
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8988
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6744
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5397
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
3668
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.