Web Hosting Forum - A Web Hosting community for Webmasters


Divi WordPress Theme

Author Topic: Create a simple hit counter using PHP and MySQL  (Read 9626 times)

0 Members and 1 Guest are viewing this topic.

Offline Tech

  • Junior Guru
  • **
  • Posts: 183
  • iTrader: +2/-0
    • View Profile
Create a simple hit counter using PHP and MySQL
« on: October 23, 2007, 09:25:04 AM »
In this article I describe how to use PHP and MySQL to produce a simple counter that can be placed on a web page. PHP and MySQL work very well together, and this article shows, hopefully, how easy they are to use to produce a useful little utility.

In order for the counter to work, the web server you upload the files to needs to support PHP and MySQL. Most good hosting solutions do.

You can download the various scripts used to produce the counter from the following web address: http://www.computernostalgia.net/counter/counter.zip. The scripts are also listed below.

The counter needs a database called 'counter', a table in that database called 'countertable', and a field in the table called 'count'. If you want to use a different database, table, or field name, make sure you change the appropriate references to these names in the scripts.

Files

The zip file (counter.zip) contains the following files:

    * create_database.php
    * create_table.php
    * reset_counter.php
    * counter.php

Note that for display considerations, in the following listings, opening and closing angle brackets for tag names ('<..>') are replaced by opening and closing square brackets ('[..]').

create_database.php

This script creates a MySQL database called 'counter'. Upload this script to your web server and run it first to create the database.

[head][title]Create MySQL Database[/title][/head]
[body]
[?php

//This script creates a database on the MySQL server.
//The name of the database is counter.

//Connect to MySQL server
$link = mysql_connect("localhost");

//If you need to supply a username and password, then use the following line
//of code instead of the one above, substituting the correct username and password.
//$link = mysql_connect("localhost", "username", "password");

//If the connection cannot be made, display an error message
if (! $link)
die("Cannot connect to MySQL");

//Create a database called counter
mysql_create_db("counter")or die("Error: ".mysql_error());

//Close the connection to the MySQL server
mysql_close($link);
?]
[/body]


create_table.php

This script creates a table (countertable) in the counter database. The table has one field, called 'count', which can store an eight digit number. This allows a counter value up to 99,999,999. Upload this and run it once the database has been created.

[head][title]Create Table in Database[/title][/head]
[body]
[?php

//This script creates a table (countertable) in the database (counter).

//Assign the name of the database (counter) to the variable $db.
$db="counter";

//Connect to MySQL server.
$link = mysql_connect("localhost");

//If you need to supply a username and password, then use the following line
//of code instead of the one above, substituting the correct username and password.
//$link = mysql_connect("localhost", "username", "password");

//If the connection cannot be made, display an error message.
if (! $link)
die("Cannot connect to MySQL");

//Select the database. If the database cannot be selected, display an error message.
mysql_select_db($db , $link)
or die("Select DB Error: ".mysql_error());

//Create a table called countertable in the database.
//The table contains one field: count, which should allow up to 99,999,999 hits
mysql_query("CREATE TABLE countertable( count INT(8))")or die("Create table Error: ".mysql_error());

//Close connection to MySQL server.
mysql_close($link);

?]
[/body]


reset_counter.php

This script sets/resets the counter to zero. Upload this and run it to initialise the counter to zero. You can run it at any time to reset the counter to zero.

[head][title]Reset Counter[/title][/head]
[body]

[?php

//Point your browser at this page to set/reset the counter to zero.

$db="counter";

$link = mysql_connect("localhost");

//If you need to supply a username and password, then use the following line
//of code instead of the one above, substituting the correct username and password.
//$link = mysql_connect("localhost", "username", "password");

if (! $link) die("Cannot connect to MySQL");
mysql_select_db($db , $link) or die("Cannot open $db: ".mysql_error());

// Set the counter to zero
mysql_query("INSERT INTO countertable (count) VALUES ('0')");

//close link to MySQL server
mysql_close($link);
?]

[/body]


counter.php

This is the actual counter. The code in this file should be pasted into the web page that will contain the counter (or it can be run on its own). This web page, which will typically be part of a web site, must have a .php file extension, otherwise the PHP code will be ignored by the web server.

[head][title]Increment Counter[/title][/head]
[body]

[comment]
Include everything below this comment (down to the closing body tag) in the page
on which you want to put the counter.
[/comment]

[?php

//Set database to counter
$db="counter";

//connect to server and database
$link = mysql_connect("localhost");

//If you need to supply a username and password, then use the following line
//of code instead of the one above, substituting the correct username and password.
//$link = mysql_connect("localhost", "username", "password");

if (! $link) die("Cannot connect to MySQL");
mysql_select_db($db , $link) or die("Cannot open $db: ".mysql_error());

//Increment counter
mysql_query("UPDATE countertable SET count=count+1");

//extract count from database table
$counter = mysql_query("SELECT * FROM countertable");

//Display counter. If you want to change the appearance of the counter, edit
//the following table and font settings.
print "[table border=1 cellpadding=3 cellspacing=0 width=80]";
while ($get_count = mysql_fetch_row($counter)){
print "[tr]";
foreach ($get_count as $field)
print "[td align=right][font face=arial size=2]$field[/font][/td]";
print "[/tr]";
print "[/table]";
}

//close link to MySQL server
mysql_close($link);
?]

[/body]


That's it!

About the Author: John Dixon is a web developer and technical author. He enjoys developing database-driven websites using PHP, Perl, and MySQL. John is also interested in anything to do with computer history. Visit http://www.computernostalgia.net/listDo ... utline.htm to see the current list of computer history articles on his site. To find out more about John, visit http://www.dixondevelopment.co.uk.

Article Source: http://EzineArticles.com/?expert=John_Dixon
« Last Edit: January 01, 1970, 05:30:00 AM by Tech »

VPS Hosting - AccuWebHosting

Offline evalauren

  • Newbie
  • *
  • Posts: 4
  • iTrader: +0/-0
    • View Profile
Re: Create a simple hit counter using PHP and MySQL
« Reply #1 on: August 11, 2010, 09:06:51 AM »
Here you provide useful information on "Create a simple hit counter using PHP and MySQL " and links include huge stuff related this and provide great guide to those who have no knowledge about it and who are new to this field .

Divi WordPress Theme
Tags:
 

Related Topics

  Subject / Started by Replies Last post
0 Replies
2543 Views
Last post August 30, 2007, 02:04:54 AM
by Tech
0 Replies
2467 Views
Last post August 31, 2007, 01:51:40 AM
by Tech
0 Replies
5998 Views
Last post August 21, 2008, 06:57:25 PM
by Kevin
0 Replies
3430 Views
Last post September 21, 2008, 11:31:52 PM
by Kevin
0 Replies
2837 Views
Last post July 25, 2015, 02:08:35 AM
by SethPenman