Poll

Was this tutorial Helpful?

Yes
4 (80%)
No
0 (0%)
Useful but not really what i'm after
1 (20%)

Total Members Voted: 3

Voting closed: February 15, 2003, 11:35:17 PM

Author Topic: Hit Counter & Users Online Script  (Read 16643 times)

evilcoder

  • New Member
  • *
  • Posts: 10
  • Karma: +0/-0
    • View Profile
Hit Counter & Users Online Script
« on: February 15, 2003, 11:33:14 PM »
Hey everyone, this is just a simple script which will tell you how many people are online on your site and how many pages hits you've had in both the last 24 hours and all up.

Create database and place this dump inside it:

Code: [Select]
#
# Table structure for table `counter`
#

CREATE TABLE `counter` (
`counter` text NOT NULL
) TYPE=MyISAM;

#
# Dumping data for table `counter`
#

INSERT INTO `counter` (`counter`) VALUES ('0');
# --------------------------------------------------------

#
# Table structure for table `daily_hits`
#

CREATE TABLE `daily_hits` (
`date` text NOT NULL,
`ip` text NOT NULL
) TYPE=MyISAM;

#
# Dumping data for table `daily_hits`
#

#
# Table structure for table `usersonline`
#

CREATE TABLE `usersonline` (
`timestamp` int(15) NOT NULL default '0',
`ip` varchar(40) NOT NULL default '',
`file` varchar(100) NOT NULL default '',
PRIMARY KEY (`timestamp`),
KEY `ip` (`ip`),
KEY `file` (`file`)
) TYPE=MyISAM;

#
# Dumping data for table `usersonline`
#

INSERT INTO `usersonline` (`timestamp`, `ip`, `file`) VALUES (1035423939, '127.0.0.1', 'Mailing List');

Next you have to make the background script which will run this baby!

--------------------------------------------------------------------------------
Create a file called: stats.php , and place this inside it.

Code: [Select]
<?php
@mysql_connect&#40; "localhost","username","password" &#41;;
@mysql_select_db&#40; "database_name" &#41;;
$date date&#40; "j/m/Y" &#41;;
$daily1 "INSERT INTO daily_hits 
           &#40;date, ip&#41; 
           VALUES 
           &#40;'$date', '$REMOTE_ADDR'&#41; "
;
            
$re mysql_query&#40; $daily1 &#41;;
$delete "DELETE FROM daily_hits 
           WHERE date <> '$date' "
;

$del mysql_query&#40; $delete &#41;;
$main_counter mysql_query&#40; "SELECT counter 
                              
FROM counter " &#41;;
                               
$gy = mysql_fetch_array&#40; $main_counter &#41;;
$new_count = $gy['counter'&#93; + 1;
$up = mysql_query&#40; "
UPDATE counter 
                    SET counter
='$new_count'" &#41;;
                    
$sql = mysql_query&#40; "
SELECT 
                     
FROM users" &#41;;
                     
$users = mysql_num_rows&#40; $sql &#41;;

$sql = mysql_query&#40; "
SELECT 
                     
FROM daily_hits" &#41;;
                     
$daily = mysql_num_rows&#40; $sql &#41;;

$sql = mysql_query&#40;"
select counter from counter"&#41;;
$count = mysql_fetch_array&#40;$sql&#41;;

$timeoutseconds = 30; 
$timestamp = time&#40;&#41;; 
$timeout = $timestamp-$timeoutseconds; 
$insert = mysql_query&#40;"
INSERT INTO usersonline VALUES &#40; '$timestamp','$REMOTE_ADDR','$page' &#41; " &#41;;
 
if &#40; !&#40; $insert &#41; &#41; 
&#123; 
print "Useronline Insert Failed > "
&
#125; 
$delete mysql_query&#40; "DELETE FROM usersonline WHERE timestamp<$timeout" &#41;; 

if &#40;!&#40; $delete &#41; &#41; 
&#123; 
print "Useronline Delete Failed > ";
&
#125; 
$result mysql_query&#40; "SELECT DISTINCT ip FROM usersonline" &#41;; 
if &#40; !&#40; $result &#41; &#41; 
&#123; 
print "Useronline Select Error > "
&
#125; 
$user mysql_num_rows&#40; $result &#41;; 
if &#40; $user == '1' &#41; 
&#123; 
$online "$user";
&
#125; else &#123; 
$online "$user"
&
#125; 

?>


Now thats the hard work done, all you have to do now is call that script onto your page:

On the page you want the counter, place this before anything else on the page (Before <HTML> tag ):


Code: [Select]
<?php
include&#40; "/path/to/stats&#46;php" &#41;;
?>


Now, where you want the users online and site hits to be placed on your page just put this code:
Code: [Select]
<b><?=$online ?></b> user online. With: <b><?=$daily ?></b> Pages views today, and a total of: <b><?php echo $count[counter&#93; ?></b> views.


This will be the first in a number of tutorials i'll be putting up. Hope this helps someone.
« Last Edit: May 16, 2003, 09:49:05 PM by evilcoder »
I will die before YOU GET MY BARBIE!

milesburton

  • Regular Member
  • **
  • Posts: 75
  • Karma: +0/-0
    • View Profile
(No subject)
« Reply #1 on: February 16, 2003, 07:48:20 AM »
Hmm, interesting, I may give it a try.

Anonymous

  • New Member
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
Good info
« Reply #2 on: February 20, 2003, 04:26:32 PM »
DELETED

evilcoder

  • New Member
  • *
  • Posts: 10
  • Karma: +0/-0
    • View Profile
(No subject)
« Reply #3 on: February 22, 2003, 12:20:00 AM »
unfortunately that script doesn't work as an addon to a membership as is. You would have to make variations to it for that sort of system to work.
I will die before YOU GET MY BARBIE!

Anonymous

  • New Member
  • *
  • Posts: 11
  • Karma: +0/-0
    • View Profile
(No subject)
« Reply #4 on: March 13, 2003, 03:38:09 PM »
DELETED

pointsplat

  • New Member
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
(No subject)
« Reply #5 on: May 16, 2003, 04:03:21 PM »
I have yet to put it into action, I will when I get home from work tonight but assumming everything goes smoothly its exactly what I need :)

pointsplat

  • New Member
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
(No subject)
« Reply #6 on: May 16, 2003, 07:09:29 PM »
There is at least one type in this code (I only used the session part)  down toward the bottom it says

Code: [Select]
$result = mysql_query SELECT DISTINCT ip FROM usersonline" );
should read
Code: [Select]
$result = mysql_query("SELECT DISTINCT ip FROM usersonline" );

Silly typos  8)

evilcoder

  • New Member
  • *
  • Posts: 10
  • Karma: +0/-0
    • View Profile
(No subject)
« Reply #7 on: May 16, 2003, 09:49:38 PM »
thanks for the correction. I have fixed it and it should be all good now.
I will die before YOU GET MY BARBIE!

pointsplat

  • New Member
  • *
  • Posts: 3
  • Karma: +0/-0
    • View Profile
(No subject)
« Reply #8 on: May 18, 2003, 12:56:22 PM »
:roll:  :roll: I would also suggest using this code below

Code: [Select]
$rs = mysql_query("SELECT file FROM usersonline WHERE ip = '$REMOTE_ADDR'");

if($row = mysql_fetch_array($rs))
{
mysql_query("UPDATE usersonline SET timestamp='$timestamp',file='thisisanupdate' WHERE ip = '$REMOTE_ADDR'");
}
else
{
mysql_query("INSERT INTO usersonline (timestamp,ip,file) VALUES ('$timestamp','$REMOTE_ADDR','thisisaninsert')");
}

[color="red"]in place of[/color]
Code: [Select]
$insert = mysql_query("INSERT INTO usersonline VALUES ( '$timestamp','$REMOTE_ADDR','$page' ) " );

if ( !( $insert ) )
{
print "Useronline Insert Failed > ";
}


Simply because with the previous way mentioned you are going to flood your database with entries everytime someone hits that code (which will prolly be every page depending on your choice).

EvilCoder if you feel this is a worthy mod feel free make the modification and remove my post (if your a moderator) if not I will remove it as to not clutter the post.

john00

  • New Member
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
(No subject)
« Reply #9 on: June 19, 2003, 07:03:26 PM »
n00b here.. how do you make a database? ahh im hopin this isnt too complicated/confusing..

PunkGo

  • New Member
  • *
  • Posts: 1
  • Karma: +0/-0
    • View Profile
(No subject)
« Reply #10 on: August 16, 2003, 09:36:27 PM »
hey, the code works GREAT cept 1 thing, it says this....
Code: [Select]
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in c:appservwwwstats.php on line 26
heres line 26
Code: [Select]
$users = mysql_num_rows( $sql );

any suggestions, thank you
A Newbie To PHP, But Experienced In Photoshop and HTML.

searcher

  • New Member
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
how much load on server though?
« Reply #11 on: August 19, 2003, 10:53:47 AM »
Can such a script accurately count 500 to 1000 hits per minute? What is maximum load this has been tested with?

thx searcher

Bane

  • Senior Member
  • ****
  • Posts: 146
  • Karma: +0/-0
    • View Profile
    • PHP help forums
(No subject)
« Reply #12 on: August 19, 2003, 03:21:19 PM »
A website with a hit counter will never recieve 500-1000 hits a minute. Imaging Yahoo with a hit counter... That would be horrible. Hit counters are about the best way to get a site to look amateur. You can count your visitors for internal use, but no need to display it, no one care. When you reach a website, you don't want to know how many people saw it, you want to see the actual content.

MySQL probably won't scale to 500-1000 hits a minute, not without replication. I don't know if you calculated, but it's 720k to 1.4m hits a day.
Louis-Philippe Huberdeau
lphuberdeau {at} phpquebec {dot} {org|com|net}

Blog
http://www.drone-alliance.org

PHP Conference in Montreal - PHP Quebec Conference
http://conf.phpquebec.org

searcher

  • New Member
  • *
  • Posts: 2
  • Karma: +0/-0
    • View Profile
(No subject)
« Reply #13 on: August 19, 2003, 06:19:01 PM »
Quote from: "Bane"
A website with a hit counter will never recieve 500-1000 hits a minute. Imaging Yahoo with a hit counter... That would be horrible. Hit counters are about the best way to get a site to look amateur. You can count your visitors for internal use, but no need to display it, no one care. When you reach a website, you don't want to know how many people saw it, you want to see the actual content.

MySQL probably won't scale to 500-1000 hits a minute, not without replication. I don't know if you calculated, but it's 720k to 1.4m hits a day.


I get 120,000 to 180,000 hits a day, depending on day of the week. At peak load during mid-day I'm getting 500 to 800 hits per minute (and growing). I don't get that all day, but I do get it for several hours straight, which is what the problem is with counters. You may feel a visible counter is a sign of a "amateur", but when it is used for marketing purposes, a little counter can make or break a sale. Maybe individual viewers could care less about hits, views and clickthroughs, but paying advertisers do. If a visible page counter gives my customers a little reassurance, then I'll use one. Question is which will do the job with less server load, new PHP/MySQL sollution or current Perl cgi?

Bane

  • Senior Member
  • ****
  • Posts: 146
  • Karma: +0/-0
    • View Profile
    • PHP help forums
(No subject)
« Reply #14 on: August 19, 2003, 11:35:11 PM »
Apache automatically generates a log file with the request. Simply get a cron task to run every minute to count how many hits you had. When you need massive performences, avoid using databases. Cache as much files statically as you can. Database connections and requests are always the slowest part in a script, even if they are very useful and required. Some pages simply don't need to be all that dynamic.

An other solution is to use SRM, which is a deamon to create PHP application variables. Using a counter with that would be a lot faster. It's not yet the most stable application around, but if you are serious, you might want to work on it, I think Derick could use some help.
Louis-Philippe Huberdeau
lphuberdeau {at} phpquebec {dot} {org|com|net}

Blog
http://www.drone-alliance.org

PHP Conference in Montreal - PHP Quebec Conference
http://conf.phpquebec.org