Newbie: Simple Counter
Moderator: General Moderators
-
ldomingues
- Forum Commoner
- Posts: 41
- Joined: Fri Aug 06, 2004 1:15 pm
- Location: Portugal
That will count page hits. If you want to count visitors, if should add before this code:
Code: Select all
<?php
session_start();
if(!isset($_SESSION["already_counted_this_guy"]))
{
$_SESSION["already_counted_this_guy"]=true;
......
.. put here LiquidPro's code...
......
}
?>Another thing you may want to consider doing before putting this script into action on a website is incorporate some way to do some data validation.
For example, you might want to check to make sure the data pulled from counter.txt is an integer and not a string before incrementing, or perhaps see if the file exists, and depending on whether it exists or doesn't exist create the file with an initial value.
Just some things to consider.
For example, you might want to check to make sure the data pulled from counter.txt is an integer and not a string before incrementing, or perhaps see if the file exists, and depending on whether it exists or doesn't exist create the file with an initial value.
Just some things to consider.
That's like saying you should learn to drive in a Ferarri Enzo and then driving in a more moderate car.Sami wrote:I'd much rather prefer they learned it the best way first, instead of learning an inferior way first and then realizing there is a better method weeks/months later.
It's good to learn how to write to files first then go onto MySQL. MySQL might be too complicated for some people at first. We all know how files work.
-
d3ad1ysp0rk
- Forum Donator
- Posts: 1661
- Joined: Mon Oct 20, 2003 8:31 pm
- Location: Maine, USA
Why?nigma wrote:Another thing you may want to consider doing before putting this script into action on a website is incorporate some way to do some data validation.
For example, you might want to check to make sure the data pulled from counter.txt is an integer and not a string before incrementing, or perhaps see if the file exists, and depending on whether it exists or doesn't exist create the file with an initial value.
Just some things to consider.
Take another look at the script. $filename never changes, therefor even if the file has somehow been replaced with a malicious file, it's not like changing the contents of it is going to hurt your system.. o_O
For a simple counter, this ISNT the inferior way. mysql would be a bad decision. Creating the db, creating the table/vars, connecting to the db, selecting the table, creating the query, getting the values, updating the value(s), saving the data, disconnecting from the db.. OR.. open file, read file, update data, write to file, close file.Sami wrote:I'd much rather prefer they learned it the best way first, instead of learning an inferior way first and then realizing there is a better method weeks/months later.
Once you get started on more than one thing (ips, refferer, etc.) dbs become a good idea. tracking JUST hits, files seem better IMHO..
Yea, you don't need to add anything. But, say somehow this counter.txt file has the string "blah" in it and not a number, then you increment "blah" and get "blai" and then you output that to the screen saying that is how many hits your site has. Now chances are that this might not ever happen, but still, some people may want to consider adding some ways to check for such an abnormality.LiLpunkSkateR wrote: Why?
Take another look at the script. $filename never changes, therefor even if the file has somehow been replaced with a malicious file, it's not like changing the contents of it is going to hurt your system.. o_O
I'm not trying to criticise or belittle liquidpro's work, and I hope my post wasn't interpreted as a criticism. If you read the tutorial to learn something you will, you'll learn a fairly simple way to store hit counter data (a text file) and you'll learn how to read/write files.
I hope you didn't feel "put out" by my post liquidpro?
