Help - Search - Members - Calendar
Full Version: Advanced MySQL Hit Counter Tutorial.
Pixel2Life Forum > Member Tutorials and Requests > Forum Tutorial Archives > PHP Tutorials
Pages: 1, 2
. Adam .
This tutorial has been moved to publishing...

http://www.pixel2life.com/publish/tutorial...utorial_on_p2l/

Thanks.
. Adam .
ph34r.gif Comments Welcome

- Adam bigwink.gif
Rory
Very Very nice tutorial. Basic yet very effective and easy to implement. Great work adam thats a really nice hit counter!
. Adam .
Thanks smile.gif

I hope to write some more tutorials just as good, maybe even better tongue.gif

- Adam bigwink.gif
Adαm
Tutorials posted on these forums are meant to be exclusive to P2L, especially if you have gotten a custom member title for writing it, yet you have posted it on this website.

http://www.techtuts.com/?view=tutorials&am...rial&id=962
. Adam .
Hang on....

I asked Donna Shall I post this?

reply: yes

Point Made victory.gif

- Adam bigwink.gif
Adαm
Ah ok smile.gif Didn't know.
. Adam .
QUOTE(Adam C @ Sep 17 2006, 08:49 PM) *
Ah ok smile.gif Didn't know.


Ok lol victory.gif

- Adam bigwink.gif
Trix06
Nice post Adam. This will come in handy somewhere shortly. I might even modify it slightly. bigwink.gif Thanks again champ!
Donna
QUOTE(Adam C @ Sep 18 2006, 08:49 AM) *
Ah ok smile.gif Didn't know.


blink.gif unsure why you have reported this he asked if he could post it, it maybe pointless to you but many new members will think otherwise.

Please don't use the report post when there is nothing to report thanks
Slashman X
QUOTE
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home2/ali/public_html/hit.php on line 14
:s
. Adam .
QUOTE(emcfc @ Sep 23 2006, 04:14 PM) *
QUOTE


Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home2/ali/public_html/hit.php on line 14
:s


You have done the SQL and your config file is correct?

- Adam bigwink.gif
Markster
Hi there, just tested this out and found that the SQL does not run properly..

Error

SQL query:

CREATE TABLE `stats` (
`ip` varchar( 20 ) NOT NULL default '',
`date` varchar( 10 ) NOT NULL default '00-00-0000',
`hits` int( 10 ) NOT NULL default '1'
) ENGINE = MYISAM DEFAULT CHARSET = latin1;

MySQL said: Documentation
#1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'DEFAULT CHARSET=latin1' at line 5
. Adam .
QUOTE(Markster @ Sep 27 2006, 03:58 PM) *
Hi there, just tested this out and found that the SQL does not run properly..

Error

SQL query:

CREATE TABLE `stats` (
`ip` varchar( 20 ) NOT NULL default '',
`date` varchar( 10 ) NOT NULL default '00-00-0000',
`hits` int( 10 ) NOT NULL default '1'
) ENGINE = MYISAM DEFAULT CHARSET = latin1;

MySQL said: Documentation
#1064 - You have an error in your SQL syntax. Check the manual that corresponds to your MySQL server version for the right syntax to use near 'DEFAULT CHARSET=latin1' at line 5


CODE
CREATE TABLE `stats` (
  `ip` varchar(20) NOT NULL default '',
  `date` varchar(10) NOT NULL default '00-00-0000',
  `hits` int(10) NOT NULL default '1'
) ENGINE=MyISAM;


Try that victory.gif

- Adam bigwink.gif
Markster
Well i just added it manually onto the DB in the end... but i get this error thrown up from hit.php

Warning: mysql_fetch_object(): supplied argument is not a valid MySQL on line 14.

All the tables and fields are the same.
felguard
config.php file should be like this:
CODE
<?

$connect = mysql_connect("localhost","DATABASE USERNAME","DATABASE PASSWORD") or die(mysql_error);
// Fill in where it says database username and password
mysql_select_db("DATABASE NAME HERE") or die(mysql_error);
// Fill in where it says database username

?>


You added extra ")" in the mysql_error part.

And in stats.php, the variable needs to be fixed:
CODE
    Total Page Views ".$totalhits."<br /> ";


Other than that, this is probably the best tutorial on stats like this out of all the ones I've searched so far.

Edit:
Okay, I'm getting the same error stated above:
CODE
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/deimonhi/public_html/stats/hits.php on line 14
. Adam .
QUOTE(felguard @ Sep 29 2006, 04:30 AM) *
config.php file should be like this:
CODE
<?

$connect = mysql_connect("localhost","DATABASE USERNAME","DATABASE PASSWORD") or die(mysql_error);
// Fill in where it says database username and password
mysql_select_db("DATABASE NAME HERE") or die(mysql_error);
// Fill in where it says database username

?>


You added extra ")" in the mysql_error part.

And in stats.php, the variable needs to be fixed:
CODE
    Total Page Views ".$totalhits."<br /> ";


Other than that, this is probably the best tutorial on stats like this out of all the ones I've searched so far.

Edit:
Okay, I'm getting the same error stated above:
CODE
Warning: mysql_fetch_object(): supplied argument is not a valid MySQL result resource in /home/deimonhi/public_html/stats/hits.php on line 14


Ok,

I have fixed config.php, and the variable in stats.php

I have updated the tutorial, So it should work now bigwink.gif

QUOTE
Other than that, this is probably the best tutorial on stats like this out of all the ones I've searched so far.


Thanks, victory.gif

- Adam bigwink.gif
Nerve
Nice but very basic, Not sure whats so 'advanced', Anyways may be good for beginners
Krypton
Thanks, I will be implementing this in with a CMS I have going, and each item in the CMS will have a hit counter. victory.gif
rmontalvo23
I'm new around here and came across this tutorial.

I am wondering if this hit counter can be made to read certain file types that are being called on.
for example instead of displaying hits, page views, etc.. I want to display number of hits to .mpg, .wmv, and .mov files.

I have a video site and want people to know how many videos are being hit, at the moment I am posting the hits and the end of the month on the home page. I would love to have it automatically list the video hits in real time for my site www.bmxvideo.net

Thanks
syphon
suggestion:

CODE
        $hit = mysql_fetch_array($select);
        // selects the data from the database
        $hits = $hit['hits'] + 1;
        // Adds one onto current hit counter
        $select = mysql_query("UPDATE `stats` SET `hits` = '".$hits."' WHERE `ip` = '".$_SERVER['REMOTE_ADDR']."' && `date` = '".date("d-m-Y")."'");
        // Updates Database with there new hit count


can very easily be shortened to

CODE
        $select = mysql_query("UPDATE `stats` SET `hits` = hits + 1 WHERE `ip` = '".$_SERVER['REMOTE_ADDR']."' && `date` = '".date("d-m-Y")."'");


It has the same funtionality, and cuts out an entire query. It is a nice mysql shortcut.

du
Tarquin
Hi there, excellent tutorial by the way!

However, I am receiving the following message 'Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 5 in /home/******/public_html/stats.php on line 8'


This is the code in line 8
CODE
$thits = mysql_result(mysql_query("SELECT SUM(`hits`) as total FROM `stats` WHERE `date` = '".date("d-m-Y")."' GROUP BY `date`"), 0, "total");
unstopabl3
Adam i'm still getting this error

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/pt/public_html/test/hit.php on line 14

Are you sure you tested this before putting it up ? Can you fix the issues :/
. Adam .
I'm going to create a new one soon smile.gif

Yes this did work for me.

- Adam bigwink.gif
unstopabl3
QUOTE(. Adam . @ Jan 23 2007, 11:37 AM) *
I'm going to create a new one soon smile.gif

Yes this did work for me.

- Adam bigwink.gif


How come it doesn't work for me :/ I've tried this on different hosts and asked a few friends to try it and it doesn't work !

How soon do you think you will update this one or when are you planning on releasing the newer one your talking about ?
. Adam .
I will code the new one now, and publish it.

- Adam bigwink.gif
. Adam .
Here: http://www.pixel2life.com/tutorials/count/..._php_and_mysql/

Enjoy!
- Adam bigwink.gif
unstopabl3
QUOTE(. Adam . @ Jan 23 2007, 08:36 PM) *


Great work, much neater code and worked in the first try bigwink.gif
Are you accepting suggestions or feature requests regarding this tutorial ? If so i would love to see an advance users online tutorial implemented into this nifty tutorial !
. Adam .
Sure.. tell me any suggestions smile.gif

- Adam bigwink.gif
unstopabl3
QUOTE(. Adam . @ Jan 24 2007, 12:18 AM) *
Sure.. tell me any suggestions smile.gif

- Adam bigwink.gif


Like i've mentioned before, i would like to see an advance users online tutorial implemented into this nice tutorial. I would like to show how many users are currently visiting my website and what page they are viewing. That should add extra kewlness to this tutorial biggrin.gif
Mr. Matt
A floor in this code is that if a user is behind a company like NTL ( English ISP ( Internet Service Provider ) ) $_SERVER['REMOTE_ADDR'] will show the same IP address as everybody else in that area on NTL. So even if you had 10 people in the same area all on NTL or an ISP alike, it would record it as 1 unique hit, when infact there are 10 unique hits.

So you need to make a function which finds the users real IP address to make the hits unique.

Matt
unstopabl3
QUOTE(Mr. Matt @ Jan 26 2007, 12:31 PM) *
A floor in this code is that if a user is behind a company like NTL ( English ISP ( Internet Service Provider ) ) $_SERVER['REMOTE_ADDR'] will show the same IP address as everybody else in that area on NTL. So even if you had 10 people in the same area all on NTL or an ISP alike, it would record it as 1 unique hit, when infact there are 10 unique hits.

So you need to make a function which finds the users real IP address to make the hits unique.

Matt


Mayb try a different method to get the ip of a user ? I read something like this somewhere

CODE
if(!isset($_SERVER["HTTP_X_FORWARDED_FOR"])){
    $ip=$_SERVER["REMOTE_ADDR"];
}else{
    $ip=$_SERVER["HTTP_X_FORWARDED_FOR"];
}


Don't know if this will work or not :/
. Adam .
QUOTE(unstopabl3 @ Jan 25 2007, 11:56 PM) *
QUOTE(. Adam . @ Jan 24 2007, 12:18 AM) *
Sure.. tell me any suggestions smile.gif

- Adam bigwink.gif


Like i've mentioned before, i would like to see an advance users online tutorial implemented into this nice tutorial. I would like to show how many users are currently visiting my website and what page they are viewing. That should add extra kewlness to this tutorial biggrin.gif


I will add how many users are currently online, not sure that what page they are viewing is necessary. Obviously you can add it as an extra bigwink.gif

QUOTE(Mr. Matt @ Jan 26 2007, 12:31 PM) *
A floor in this code is that if a user is behind a company like NTL ( English ISP ( Internet Service Provider ) ) $_SERVER['REMOTE_ADDR'] will show the same IP address as everybody else in that area on NTL. So even if you had 10 people in the same area all on NTL or an ISP alike, it would record it as 1 unique hit, when infact there are 10 unique hits.

So you need to make a function which finds the users real IP address to make the hits unique.

Matt


Okay, Will do smile.gif

Thanks,
- Adam bigwink.gif

edit: Tutorial updated. Now supports users online.
unstopabl3
I tested the new updated version and saw two mistakes

1) The mysql connection query for the config file is either incomplete or missing some data

CODE
<?php

$connect = mysql_connect( 'localhost', 'root', '' ) or die(mysql_error()); // Connect to MySQL.

mysql_select_db( 'test' ); // Select the database.

function _ip( )
{
    return ( preg_match( "/^([d]{1,3}).([d]{1,3}).([d]{1,3}).([d]{1,3})$/", $_SERVER['HTTP_X_FORWARDED_FOR'] ) ? $_SERVER['HTTP_X_FORWARDED_FOR'] : $_SERVER['REMOTE_ADDR'] );
}

$ip = _ip(); // Get the users IP using the function above
$time = date( 'd-m-Y' ); // Get the current date, in the format of: 12-12-2006
$timestamp = time();

?>


Unless things have changed how can one connect to a database without host, database, username, and password combination.

2) After i setup the query myself and configured the mysql query myself I ran the hit.php file and I got the following error

CODE
Unknown column 'online' in 'field list'


Are you sure you tested the updated version of this tutorial before putting it up Adam ?
Can you fix the issues and update the tutorial. Thanks in advance
. Adam .
Fixed.

- Adam bigwink.gif
unstopabl3
QUOTE(. Adam . @ Jan 27 2007, 09:13 AM) *
Fixed.

- Adam bigwink.gif


You didn't test it did you Adam rolleyes.gif
There are no errors now but whenever someone goes to hit.php it's counting the hits/page refreshes twice !
You can see an example of what i'm saying here

http://pwnagetime.net/test/hit.php
http://pwnagetime.net/test/stats.php

Please fix these errors and i also have another suggestion.

I appreciate you adding the users online timbit into the tutorial but can you also add a feature which will allow the user to click on the # of users online to see which user is online on which page ? For example

Guests Online: 1 <<< #1 is linked to another page named details.php and upon visiting the page it shows information, such as their ip, host, browser and anything extra you wanna throw in, on the users who are online

I believe if you can achieve this, then this tutorial really will become the best simple yet advance hits counter biggrin.gif
unstopabl3
QUOTE(unstopabl3 @ Jan 27 2007, 04:14 PM) *
QUOTE(. Adam . @ Jan 27 2007, 09:13 AM) *
Fixed.

- Adam bigwink.gif


You didn't test it did you Adam rolleyes.gif
There are no errors now but whenever someone goes to hit.php it's counting the hits/page refreshes twice !
You can see an example of what i'm saying here

http://pwnagetime.net/test/hit.php
http://pwnagetime.net/test/stats.php

Please fix these errors and i also have another suggestion.


I found the culprit ! Check stats.php and remove the following line

require_once 'hit.php'; // Record the hits to this page

And it now counts once only biggrin.gif

Let me know about the users online feature.
. Adam .
Dude, the script works fine.

If you remove that line from stats.php, you wont be counting the traffic to that page...

and Yes of course I tested it blink.gif

- Adam bigwink.gif
unstopabl3
QUOTE(. Adam . @ Jan 27 2007, 05:31 PM) *
Dude, the script works fine.

If you remove that line from stats.php, you wont be counting the traffic to that page...

and Yes of course I tested it blink.gif

- Adam bigwink.gif


Nope the script doesn't work it has issues after issues.
Now after i fixed the previous mentioned issue i found another bug/glitch which is that after the 5 unique hits, it stops counting anymore unique hits whereas the page refreshes/hits keep going up. Can you look into this matter ? Thx appreciate your time and effort !
. Adam .
It works fine for me. You must be doing something wrong.

Can someone else Test this for me pleaseee victory.gif


Thanks,
- Adam bigwink.gif
unstopabl3
QUOTE(. Adam . @ Jan 27 2007, 09:56 PM) *
It works fine for me. You must be doing something wrong.

Can someone else Test this for me pleaseee victory.gif


Thanks,
- Adam bigwink.gif


I've tested it on three of my other hosts and same issue, after 5 unique hits it stops counting unique hits !
. Adam .
QUOTE(unstopabl3 @ Jan 28 2007, 05:13 AM) *
QUOTE(. Adam . @ Jan 27 2007, 09:56 PM) *
It works fine for me. You must be doing something wrong.

Can someone else Test this for me pleaseee victory.gif


Thanks,
- Adam bigwink.gif


I've tested it on three of my other hosts and same issue, after 5 unique hits it stops counting unique hits !


Yea, I'm sorry rolleyes.gif I realised the bug now.

I will try and fix victory.gif

- Adam bigwink.gif
unstopabl3
QUOTE(. Adam . @ Jan 28 2007, 10:47 AM) *
Yea, I'm sorry rolleyes.gif I realised the bug now.

I will try and fix victory.gif

- Adam bigwink.gif


Great, any updates on this ? Have you fixed it yet ? I'm so impatient to use the fixed version biggrin.gif
. Adam .
Is now working for me, updated code.

- Adam bigwink.gif
unstopabl3
QUOTE(. Adam . @ Jan 29 2007, 05:15 PM) *
Is now working for me, updated code.

- Adam bigwink.gif


Good stuff, it's working properly now but there still are a few things i would like to point out bigwink.gif I hope you don't mind.

1) config.php

CODE
$connect = mysql_connect( 'localhost', 'root', '' ) or die(mysql_error()); // Connect to MySQL.
mysql_select_db( 'test' ); // Select the database.


It would be alot easier if you did this the following way

CODE
$connect = mysql_connect( 'localhost', 'YOURDATABASEUSERNAME', 'YOURDATABASEPASSWORD' ) or die(mysql_error()); // Connect to MySQL.
mysql_select_db( 'YOURDATABASENAME' ); // Select the database.



2) Almost all the files contain loads of breaks/spaces between the code, is that really necessary ?

3) In stats.php you've included two files

CODE
require_once 'config.php'; // Include config file.

require_once 'hit.php'; // Record the hits to this page


Why are we including the hit.php file here ? Unless we want to count hits for people viewing our stats.php page i don't think it's needed. You should comment this in stats.php to make things easier for users.

4) Thanks for taking my suggestion and adding on the users online feature but wouldn't it be much better if a user could click on the # of guests/users online and view which page others are viewing and other useful information on the users/guests like their browser, host, ip, referrer and so on ? I know i would love to see something like this implemented in this already awesome script ! I can help you implement this if you want smile.gif

Thanks again for all your hard work smile.gif Keep up the good work.
. Adam .
1. Sorry, I C & P straight from my localhost so I forget to change the details again.

2. It's just how I code, it wont hurt for you to remove the spaces yourself smile.gif

3. If you're counting your site stats, when someone is viewing your stat's... that's another hit for your site. So yes it is needed.

4. I am very capable of doing this, and don't need your help. Thanks anyway. I may add it, If I do it will be before Dan has looked at the tutorial and approved / declined. Once published, I will be adding nothing major to it.

Thanks,
- Adam bigwink.gif
. Adam .
1 fixed, 4 added ( Page 2 ).

- Adam bigwink.gif
unstopabl3
Everything is in place and works like a charm without any errors. I love the users online feature but it has a little bit space for improvement. How about including useful information about user like i've mentioned before

QUOTE(unstopabl3 @ Jan 30 2007, 01:40 AM) *
useful information on the users/guests like their browser, host, ip, referrer and so on ? I know i would love to see something like this implemented in this already awesome script ! I can help you implement this if you want smile.gif


To give you a better idea of what i mean please take a look at the following demo script and i would love to see you implement these options onto the "whose online and on which page" of your tutorial.

http://www.terrier-bytes.com/browzin/browzin_demo.html

Including with the above may i suggest you add a feature which shows where the user came from ? Like a referrer ? I would love to show something like top 5 or top 10 referers to my users, so this would be a very simple yet advance addition to this already awesome script.

After these two things are done i think you should wrap it up with a job well done card bigwink.gif Cheers mate, great work !
. Adam .
I know what your saying, but no offence.. I can't adapt the script to how you want it, cause its not just for you.

You can add this stuff yourself, but I am not going to be adding this into the tutorial.

Thanks,
- Adam bigwink.gif
unstopabl3
QUOTE(. Adam . @ Jan 31 2007, 04:05 PM) *
I know what your saying, but no offence.. I can't adapt the script to how you want it, cause its not just for you.

You can add this stuff yourself, but I am not going to be adding this into the tutorial.

Thanks,
- Adam bigwink.gif


No offense taken Adam and i hope you don't take one either when i say that i'm simply providing you with feedback and suggestions to help you make this basic hit counter advance and reflect the features the topic suggests. I can implement these features myself and use it for myself, but considering it's your work i don't want to be the one ripping your code snippets by calling it my work bigwink.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Invision Power Board © 2001-2009 Invision Power Services, Inc.