PHP User Stats

PHP User StatsIn this tutorial I am going to teach you how to make a User Stats menu in PHP and MySQL which displays: Unique hits, Unique hits today, Total hits, Total hits today. If you don't have MySQL you can get one for free here. Once you registered a MySQL database we can start coding. But first I want to point out what this tutorial covers. Here's a brief scheme of the tutorial:

1. Build a MySQL table
2. Make a PHP file that connects to the database
3. Make a PHP file that defines the user stats and fills in the database
4. Make a PHP file that defines the hit stats
5. Make a PHP file that shows the stats

Before you begin this tutorial I should recommend you to not copy the code but just type it yourself… but of course you can choose for yourself.

1. Building the MySQL table:

Login into your MySQL in PHPMyAdmin and start a new database. Call it whatever you like. Now we have to think which rows we need to have.

- ID, which counts the number of rows.
- IP, which defines the users' IP
- Referer, which defines the users' referal page.
- Time, which defines the time the user was online
- Date, which defines the date of the users' visit

Now go to the SQL tab and type this code:

Create TABLE 'stats' (
'ID' tinyint(11) NOT NULL AUTO_INCREMENT,
'IP' varchar(18) NOT NULL,
'referer' varchar(255) NOT NULL,
'time' varchar(30) NOT NULL,
'date' varchar(30) NOT NULL,
PRIMARY KEY('ID'))
TYPE = MyISAM;

2: The connect file:

Your SQL table is now completed. Now we have to make some files where the information comes from. First we need a connect.php file!

//Connect.php
<?php
$connect = mysql_connect("hostname","username","password") or die("Failed to connect to database");
$db = mysql_select_db("database") or die("Failed to connect to database");
?>

Now that wasn't too hard. The $connect variable basically connects to the database, with your hostname, username and password. If there is found an error in your host, username, or password it will return "Failed to connect to database". The $db variable selects the database and else display the error message.

The reason why we make this file separate is because we need it in the two other files we are going to make, and else we have to type them 3 times instead of one!

3. The stats and hit define file:

Now we need to make a file that defines the users' stats and add them to the SQL table we just created.

//add_hits.php
<?php
Include("connect.php");
$IP = $_SERVER['REMOTE_ADDR'];
$referer = $_SERVER['HTTP_REFERER'];
$time = time();
$date = date("M, d, Y");
$add_stats = mysql_query("INSERT INTO stats(IP,referer,time,date) VALUES ('$ip', '$referer', '$time', '$date')");
?>

Now for the explanation. The first line includes the file we just created, connect.php. The variables are now assigned the users' stats. This is done automatically. The last line is a MySQL query which inserts the variables into the correct columns.

Note: The date display is in format M, d, Y so e.g: Jul, 16,2005

4: The PHP file that sets the hit variables

Now we have to create the third part of the tutorial, the defining of the hit stats.

//set_hits.php
<?php
Include("connect.php");
$date = date("M, d, Y");
$unique_hits_select = mysql_query("SELECT DISTINCT ip FROM stats");
$unique_hits_nr = mysql_num_rows($unique_hits_select);
$unique_hits_today_select = mysql_query("SELECT DISTINCT ip FROM stats WHERE date = '$date'");
$unique_hits_today_nr = mysql_num_rows($unique_hits_today_select);
$total_hits_select = mysql_query("SELECT * FROM stats");
$total_hits_nr = mysql_num_rows($total_hits_select);
$total_hits_today_select = mysql_query("SELECT * FROM stats WHERE date = '$date'");
$total_hits_today_nr = mysql_num_rows($total_hits_today_select);
?>

Well this was quite a bunch of code, but it's actually repetitive. The second line selects unique ip's from the stats table in the database. The third line counts the results of that selection. If there are 4 unique ip's in the table, there are 4 rows. The rest is actually the same, except for the different selections.

5: The show hits file:

Now for the last part of our tutorial the show_hits.php script. A very simple script!

//show_hits.php
<?php
Echo "Total Unique hits: $unique_hits_nr;
Echo "Today's Unique hits: " $unique_hits_today_nr;
Echo "Total hits: " $total_hits_nr;
Echo "Total hits today: " $total_hits_today_nr;
?>

Don't forget to add a breakline of some sort after each line!

Well this was it! I hope you all liked it!


Related Articles

  • Statistic sites, should you pay attenion to them?
    This tutorial will show you some web stats sites as well as show the benefits and disadvantages of stats!
  • PHP User Stats
    In this tutorial I am going to teach you how to make a User Stats menu in PHP and MySQL which displays: Unique hits, Unique hits today, Total hits, Total hits today. If you dont have MySQL you can get one for free here.
  • Stop Analyzing, Start Promoting Your Web Site!
    As web promotion grows more and more popular everyday, so does over analyzing your stats, rankings, page rank, popularity rank, etc. We are all caught up in an analyzing funnel that seems to have no end in sight...
  • A Nice Hit Counter
    I shows
    Total Hits Unique Hits Todays Hits Todays Unique Hits
  • The 5 Most Important Things You Must Know About AdSense
    Whether youre just starting out with AdSense or whether youve been using it for years, the success of your ads will depend on knowing five absolutely essential pieces of information about AdSense:
  • The Importance of Your Website Statistics
    One of the best tools you have as a webmaster is your website statistics. The most popular site stats programs are Awstats, Webalizer, and Analog. Usually, they come free of charge and are accessible through your websites control panel provided by your web host...
  • Tracking Your Web Visitors
    The web statistics and analytics available to web site owners to track web visitors is often an underutilized tool. While everyone wants access to their web stats, they dont usually know what to do with the information, nor do they seek assistance in analyzing the data.
  • What are Usage Statistics?
    Usage Statistics are very important to have. They give you endless amounts of data that you can use to shape the future of your own Web site. With a few clicks of the mouse, you can find out if your latest idea is working or what you might need to do to get more visitors to your Web site...
  • Why A CSS Website Layout Will Make You Money
    Although CSS layouts have been around for years, they havent become so commonplace until recently.
  • Do Your Know Enough About Your Visitors? Probably Not!
    Your site is up, its matured for a few months and now its finally getting some visitors; congratulations.
    But just how much do you know about where these visitors are actually coming from and what exactly theyre doing once they reach your site?

Contact Web Design Outsource and get started today

Need Website Designing, Development, Redesigning, Maintenance and SEO services or help growing your company's web presence? Request a free Quote Now.