PHP and MySQL Code

Start typing to search for PHP and MySQL Code Snippets and Articles Search

Submit a code Example / Snippet Join us on FaceBook
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Resources
Web Development Content
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder

Go Back Add a Comment Send this Article to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
Title : Webstatistics with Redirectors
Categories : PHP, HTML, HTML and PHP
Mark Kronsbein
Mark Kronsbein
Date : 1999-10-23
Grade : 0 of 5 (graded 0 times)
Viewed : 11356
Search : More Articles by Mark Kronsbein
Action : Grade This Article
Tools : My Favotite Articles

Submit your own code examples 

This little tutorial has been written originally in German for

You know, that people visit your page, okay. But what you really want to know is, where all these people go afterwards. So what I am going to write about might be interesting for you.I asked myself just the same question some time ago. The answer is a redirector. This little script tracks the clicks on your links, writes the URL into a MySQL-DB and also shows you grafical statistics.

All we need are three files: MyRedirector.sql, the SQL-Statements for MySQL.
redirect.php3 , which tracks the clicks and viewstats.php3 to view the statistics.

Let's start with redirect.php3. First, there are some variables to define:

$host = "";

This is the URL of your Domain.

$hostname = "localhost";

This is the hostname on which MySQL lives

$username = "your-username";
$password = "your-password";

Your login and password for MySQL.

$userstable = "redirect";

The Table in which the data for the redirector will be stored. If you just inserted MyRedirector.sql into your DB this will be "redirect".

$dbName = "your-db-name";

The name of your database, where the redirect table is.

if ($url != $host){
mysql_connect($hostname, $username, $password) or die(mysql_error());
@mysql_select_db("$dbName") or die(mysql_error());

We first have to check, if the URL called be the redirector is not the Domain the script works on. Then we connect to MySQL with the variables defined above.

$query = mysql_fetch_row(mysql_query("select * from $userstable where(url
= '$url')"));

The script checks the table for the URL to be redirected.

if ($query == false){
$hits = "1";
$query2 = "insert into $userstable (url,hits) values('$url','$hits')";
else {
$hitquery = "select hits from $userstable where url = '$url'";
$result = mysql_query($hitquery);
$hits = mysql_result($result, 0, "hits");
$query2 = "update $userstable set hits = hits+1 where url = '$url'";


If the URL is already in the table, the hits are increased by 1, if not, the URL is inserted and the value for the hits is set to "1".

if(!$url) {
$url = "$host";
else {
Header( "Location: $url");

So, as the insertion into MySQL has been done, we are ready to redirect.
First, it is checked, if there really is an URL to call. If not, your homepage will be shown. Else the user is redirected.

That's all. But as i wrote above, there are also grafical statistics.

$hostname = "localhost";
$username = "your-username";
$password = "your-password";
$userstable = "redirect";
$dbName = "your-db-name";

First of all, we have to define the variables to connect to MySQL again.

$color10 = "#FFFF00"; /* Color for Hits between 1 and 9 */
$color20 = "#FF0000"; /* Color for Hits between 10 and 19 */
$color50 = "#008000"; /* Color for Hits between 20 and 49 */
$color1k = "#0000FF"; /* Color for Hits between 50 and 99 */
$color2k = "#0000A0"; /* Color for Hits between 100 and 199 */
$color5k = "#000040"; /* Color for Hits over 200*/

As this script works with tables and their backgroundcolors, you'll now have to define the colors showed for the number of hits.

mysql_connect($hostname, $username, $password) or die(mysql_error());
@mysql_select_db("$dbName") or die(mysql_error());
$query = "select * from $userstable order by hits desc";
$result = mysql_query($query);
$number = mysql_num_rows($result) or die (mysql_error());

Then the connection to MySQL is done again. The results from the SQL-statement are orderd descending by the number of hits.

$i = 0;

if ($number == 0) {
print "<center><p><b>No Links tracked yet!</b></center>";

I there are no links tracked yet, an error message is shown.

elseif ($number >= 1) {
while ($i < $number){
$hits = mysql_result($result,$i,hits);
$url = mysql_result($result,$i,url);

if ($hits < 10){
$color = "$color10";
elseif (($hits >= 10 ) and ($hits < 20)){
$color = "$color20";
elseif (($hits >= 20 ) and ($hits < 50)){
$color = "$color50";
elseif (($hits >= 50 ) and ($hits < 100)){
$color = "$color1k";
elseif (($hits >= 100 ) and ($hits < 200)){
$color = "$color2k";
elseif ($hits >= 200){
$color = "$color5k";

The above defined colors are set for the different numbers of hits.

print "<div align=\"left\">";
print "<table cols=3 border=\"0\" width=\"100%\"><tr><td align=left with=\"400
\"><b><a href=\"$url\">$url</a></b></td>";
print "<td align=right width=\"60\"><b>$hits</b></td>";
print "<td align=left width=\"$hits\" bgcolor=\"$color\"> </td></tr>";
print "</table>\n";

This is the last step: The table is built with the results of the MySQL-query.
In the first colomn, the URL is printed, in the second the number of hits and the third has the defined color as the backgroundcolor.

Now call the script like redirect.php3?url= and wait some days. Then have a look at the viewstats.php3. Perhaps you will be suprised, which links are in the top 10.:

tracking where and what on your site people are clicking
Categories : PHP, MySQL, HTML and PHP, HTML
Uploading files to the server with PHP
Categories : PHP, File System, HTML and PHP, HTTP
Sending Form Data in EMail
Categories : PHP, Email, HTML and PHP
Alternating row colors with PHP and mySQL
Categories : PHP, Databases, MySQL, HTML and PHP
Multicolumn Output from a Database with PHP
Categories : PHP, Databases, HTML and PHP, MySQL
Static HTML Generation With PHP
Categories : PHP, HTML and PHP
How To add paging (Pagination) with PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, HTML and PHP
Extracting Elements from a Database into a Select Form Field
Categories : PHP, HTML
Making PHP Forms Object-Oriented
Categories : PHP, HTML and PHP, Object Oriented
Sending Mail Using Flash 4 and PHP
Categories : PHP, PHP options/info, Flash
Creating a Credit Card Validation Class With PHP
Categories : PHP, Credit Cards, PHP Classes
How to Make AJAX Read between the Lines
Categories : PHP, AJAX, Java Script
Function Based PHP
Categories : PHP, Functions
Simple ODBC Connections with PHP
Categories : PHP, ODBC, Databases
Installing PHP Under Xitami
Categories : PHP, Web Servers, Xitami
cowboy boy wrote : 276
hi there really nice explaination thanx for all what u did