[thelist] Tracking users on a site
NanHarbisonSmith at aol.com
NanHarbisonSmith at aol.com
Thu Mar 7 11:08:00 CST 2002
--
[ Picked text/plain from multipart/alternative ]
Alan,
I have a crude counter that I use on my site, no use of a database involved.
I am the only one that looks at it, so it is pretty basic. I think I stole
this code from somewhere, maybe from evolt, I can't remember now.
On the page that I want to count hits on I have this code:
<?php
include ("mycounter.php");
?>
+++++++++++++++++++++++++++++++++++++++++++
the page that is mycounter.php looks like this:
<html>
<head>
<title>Hit Counter Page</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF" text="#000000">
<?php
// text file that stores the number of hits for the page
$counter_file = ("mycount.txt");
//open the file
$visits = file($counter_file);
//increments the counter value by 1
$visits[0]++;
//open the counter file for writing "w"
$fp = fopen($counter_file , "w");
//put the new count value into the counter file
fputs($fp , "$visits[0]");
//close the file
fclose($fp);
//display the count
//echo $visits[0];
print("<font color=\"#FFFFFF\">$visits[0]</font>");
?>
</body>
</html>
+++++++++++++++++++++++++++++++++++++++++++
Then you need to have a .txt file in the same directory called mycount.txt
(or if you use a different name, change the code to reflect the new name.
This page just starts out blank and gets written to.
If I want to count different pages that are in the same folder, then I
renamed the text file so the hits get written to a different txt file.
+++++++++++++++++++++++++++++++++++++++++++
Last, the page you look at to see the hits looks like this:
<html>
<head>
<title>hit counters</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body bgcolor="#FFFFFF">
<?php
function ReadFromFile($TheFile)
{
//$TheFile = "mycount.txt";
$Open = fopen ($TheFile, "r");
//print($TheFile);
if ($Open) {
$Data = file ($TheFile);
for ($n = 0; $n < count($Data); $n++) {
$GetLine = explode("\t", $Data[$n]);
print ($GetLine[0]);
}
fclose ($Open);
//} else {
//print (" Unable to print hits<BR>\n");
}
} //end of ReadFromFile
print("hits to home page:<br>");
ReadFromFile("mycount.txt");
print("<br><br>Hits to Associated Programs page:<br>");
ReadFromFile("assocprograms/mycount.txt");
print("<br><br>Hits to Contact page:<br>");
ReadFromFile("contact/mycount.txt");
print("<br><br>Hits to Curriculum page:<br>");
ReadFromFile("curriculum/mycount.txt");
print("<br><br>Hits to District page:<br>");
ReadFromFile("district/mycount.txt");
print("<br><br>Hits to Directions page:<br>");
ReadFromFile("maps/mycount.txt");
print("<br><br>Hits to Announcements page:<br>");
ReadFromFile("district/announcements/mycount.txt");
print("<br><br>Hits to School Committee page:<br>");
ReadFromFile("schoolcomm/mycount.txt");
print("<br><br>Hits to School Committee minutes page:</font><br>");
ReadFromFile("schoolcomm/mycount2.txt");
?>
</body>
</html>
I know someone out there has a more elegant solution, but I am pretty new to
PHP and programming in general. I am sure my code could be cleaned up and
made simpler.
Nan
In a message dated 3/7/2002 10:45:22 AM Eastern Standard Time,
alan.lloyd0 at btinternet.com writes:
> Just want to know if anyone has any ideas on how I can track users of a
> site. My server logs don't show what I need.
> What I want is to know how long users stay on my site and what pages they
> looked at.
>
> There is about 30 pages in the site. I don't realy want a "free tracker /
> counter" as these do go offline and normaly display an image that is called
> from a remote server.
>
> I can use php cgi perl so any sugestions greatfully recieved.
>
Nan Harbison Smith
481 Elm Street
Concord, MA 01742
978-369-1224
978-369-1681
Give a man a fish and you feed him for a day; teach him
to use the Internet and he won't bother you for weeks.
More information about the thelist
mailing list