Newbie: Simple Counter

Tutorials on PHP, databases and other aspects of web development. Before posting a question, check in here to see whether there's a tutorial that covers your problem.

Moderator: General Moderators

LiquidPro
Forum Commoner
Posts: 37
Joined: Wed Aug 04, 2004 6:33 pm
Location: Akron, OH

Newbie: Simple Counter

Post by LiquidPro »

Title: Simple Counter
Level: Easy
Author: LiquidPro
Link: Click Here to View
ldomingues
Forum Commoner
Posts: 41
Joined: Fri Aug 06, 2004 1:15 pm
Location: Portugal

Post by ldomingues »

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...
  ......

}

?>
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Good code, however I would prefer to use mysql instead of setting full permissions on a file!
LiquidPro
Forum Commoner
Posts: 37
Joined: Wed Aug 04, 2004 6:33 pm
Location: Akron, OH

Post by LiquidPro »

Well, like I said... this is for a newbie.

I personally like to use a full fledged counter system, which will track IP addresses, hostnames, dates, pages, referrals, etc.

But for a newbie that's kind of out of the question.
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

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. ;)
User avatar
Joe
Forum Regular
Posts: 939
Joined: Sun Feb 29, 2004 1:26 pm
Location: UK - Glasgow

Post by Joe »

Yup my thoughts exactly Sami :D
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

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.
User avatar
phice
Moderator
Posts: 1416
Joined: Sat Apr 20, 2002 3:14 pm
Location: Dallas, TX
Contact:

Post by phice »

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. ;)
That's like saying you should learn to drive in a Ferarri Enzo and then driving in a more moderate car. ;)

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.
Image Image
LiquidPro
Forum Commoner
Posts: 37
Joined: Wed Aug 04, 2004 6:33 pm
Location: Akron, OH

Post by LiquidPro »

Thanks for agreeing with with phice, appreciate it... lol

And everyone else, don't get me wrong... I definitely think that a MySQL way is much better.
d3ad1ysp0rk
Forum Donator
Posts: 1661
Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA

Post by d3ad1ysp0rk »

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.
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
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. ;)
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.
Once you get started on more than one thing (ips, refferer, etc.) dbs become a good idea. tracking JUST hits, files seem better IMHO..
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

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
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.

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?
LiquidPro
Forum Commoner
Posts: 37
Joined: Wed Aug 04, 2004 6:33 pm
Location: Akron, OH

Post by LiquidPro »

It's OK, I just thought it was kind of strange that so many people started criticizing my newbie tutorial (as it says in the subject).
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

they are only trying to provide better solutions in the hope for others caring to learn to learn the proper way (not saying yours is/isnt proper)

theres always room to argue over the process or learning. and theres ALWAYS room to learn more.
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

lol.

dont leave! give us another chance, we promise we'll be good!
LiquidPro
Forum Commoner
Posts: 37
Joined: Wed Aug 04, 2004 6:33 pm
Location: Akron, OH

Post by LiquidPro »

Hahah, I'm definintely not leaving... I love it here, and I've only been here for what, 3 or 4 days?

This is definitely an amazing place, to come and learn, and to help others. Not to mention response time on topics is amazing, I don't think I've ever been to a board that responds so quickly.
Post Reply