Jump to content
xisto Community
khalilov

Php Counter

Recommended Posts

A good way to figure out the traffic in your site and what pages need improvement is to keep a record of the visits of every page. In this tutorial, i will show you how to make a simple counter system.

 

Simple counter:

Keeps track of views of each page.

Shows you an overview of your website's traffic.

Procedure:

First of all create a database and call it Counter, also create a table in that database with 2 fields, 'Page' which as a 20 character (can be more) varcharacter format and 'Count' which has an intermediate integer format.

Insert rows equal to the number of pages on your site, and enter 'Count' as zero.

At every page insert the following code:

<?phpmysql_connect("localhost","username","password") or die(mysql_error);mysql_select_db("counter");$result=mysql_query("SELECT* FROM counter WHERE `Page`='Main page'");$row=mysql_fetch_array($result);$i=$row['Count']+1;mysql_query("UPDATE `counter`.`Counter` SET `Count` = '$i' WHERE CONVERT( `counter`.`Page` USING utf8 ) = 'Main page'");echo "This page has ".$i." views!";?>

mysql_connect("localhost","username","password") or die(mysql_error);mysql_select_db("counter");$result=mysql_query("SELECT* FROM counter WHERE `Page`='Main page'");$row=mysql_fetch_array($result);
This code connects to the database and fetches the array(in this case row) of the viewed page.

Note: change the 'Main page' according to the names you entered in the table, example 'index.php', 'forums.php'.....

 

$i=$row['Count']+1;mysql_query("UPDATE `counter`.`Counter` SET `Count` = '$i' WHERE CONVERT( `counter`.`Page` USING utf8 ) = 'Main page'");
This code increments the number of views of the selected page which was stored in $row['Count'], next the code updates the database with the new count.

Note: again change the 'Main page' according to the names you entered in the table, example 'index.php', 'forums.php'.....

 

echo "This page has ".$i." views!";
This is optional, incase you want the page to view how many times it has been visited so far, not necassary. You can change the message into something more attractive.

Now for the overview of your website's traffic:

Note:You can insert this code anywhere.

<?phpmysql_connect("localhost","username","password") or die(mysql_error);mysql_select_db("counter");$result=mysql_query("SELECT* FROM counter ");$row=mysql_fetch_array($result);echo "<table border=1 celspacing=1><tr><th>Page</th><th>Views</th></tr>";echo "<tr><td>".$row['Page']."</td><td>".$row['Count']."</td></tr>";while($row=mysql_fetch_array($result)){echo "<tr><td>".$row['Page']."</td><td>".$row['Count']."</td></tr>";}echo "</table>";?>

mysql_connect("localhost","username","password") or die(mysql_error);mysql_select_db("counter");$result=mysql_query("SELECT* FROM counter ");$row=mysql_fetch_array($result);
This gets the FIRST row in the table we made.

echo "<table border=1 celspacing=1><tr><th>Page</th><th>Views</th></tr>";echo "<tr><td>".$row['Page']."</td><td>".$row['Count']."</td></tr>";
This makes a table and views the data of the first page in it.

while($row=mysql_fetch_array($result)){echo "<tr><td>".$row['Page']."</td><td>".$row['Count']."</td></tr>";}echo "</table>";
Using the while loop here, we can get every row in the table until it ends, when the table ends the mysql_fetch_array function returns false. So, with each value a new row is shown. When the table ends and false is return the loop ends and the table is closed. If you are wondering why i didn't do this in the begining instead of addressing the first row alone, well that is because in some cases the first row will be skipped, according to my experience anyways.

There you go a simple counter for your website, feel free to use,modify but not sell it ;).

Share this post


Link to post
Share on other sites

Thanks. Your tuto is really interesting because it shows on a simple case how php and mysql interact in order to give you the info you want.
Of course, you can also use the Xisto counter which is in your cpanel, in the "CGI center" part.

CounterYou can create a counter to count and show how many visitors have been to your site.

And if you want a more sophisticated counter, you can get a nice but very complete one for free at http://statcounter.com/

Share this post


Link to post
Share on other sites

Thanks. Your tuto is really interesting because it shows on a simple case how php and mysql interact in order to give you the info you want.

Of course, you can also use the Xisto counter which is in your cpanel, in the "CGI center" part.

 

And if you want a more sophisticated counter, you can get a nice but very complete one for free at http://statcounter.com/

My personal opinion on using other people's counters is, quite simply, not to. They're easily one of the simplest things to make, and a great way to start learning PHP (as well as a little of MySQL). Great tutorial khalilov, it should definitely give beginners a prod in the right direction. One little quibble I have with it is the lack of "in-code" commenting. Sure, you've explained what you're doing outside of the code blocks, which I think you should certainly keep as this is a tutorial, but I tend to have a comment or two within the code itself just to aid both myself and others. For something this simple it's not really necessary, admittedly, but it's a good habit to get in to.

Share this post


Link to post
Share on other sites

The reason i did not do a detailed explanation about what each section of code does is because iam assuming that the reader knows basic PHP, I'll offer more indepth explanation in my coming tutorials =). Iam gona work on a complicated counter, I already have several ideas for it, the options already present in this one + viewing views per day till 7 days , then views last week, last month, total views and maybe a message to your email if you hit a certain amount of views. I might even make each one of the users acount contain how many views the guy did ;). The concept is very easy, you only need to do some cronjobs but still i'll make a tutorial for it.In case anyone wants to see more in it say so =).Also iam just wondering i might add these tutorials to my site, i don't have to turn them into quotes right? because iam kinda quoting my self. If i have to quote them then i won't add them to my site because quoting them will make credits go negative :/.Btw when i add this complicated counter tutorial do i have to merge it with the main post? because that one will be big and probably worth the whole 10 credits. I know the subjects are the same but i hope i can start another topic, i won't be repeating my self because the coding will have alot of modifications.

Share this post


Link to post
Share on other sites

If it's another different (though more complicated) counter, you can start a new topic.If it's some improvements on the same counter, you should add a post to the current topic, without re-saying what was already said.Of course, you guess that you can't post twice the same subject.Else, I would already have done this with my nice phpbb tutorial, with a new version at each text improvement I can imagine.Simply add an answer to the present topic for each part you want to comment, that would be the best and most coherent way doing that.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×
×
  • Create New...

Important Information

Terms of Use | Privacy Policy | Guidelines | We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.