WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDESPHP CLASSESCODE SEARCHARTICLES SEARCHPHP FORUMSPHP MANUALPHP FUNCTIONS LISTWEB SITE TEMPLATES
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
Poker Tournaments Poker Tournaments
Poker Guide for Developers Poker Guide for Developers
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
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
פרייסז - השוואת מחירים בסופר
ZeroLag.com
Texas Holdem Poker Evangelists

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.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES SUBMIT AN ARTICLE PRINT
Title : Referer Statistics
Categories : PHP, MySQL, HTTP, Databases
Mark Kronsbein
Mark Kronsbein
Date : 1999-10-24
Grade : 0 of 5 (graded 0 times)
Viewed : 37954
Search : More Articles by Mark Kronsbein
Action : Grade This Article
Tools : My Favotite Articles


Submit your own code examples 
 


Related Files

Referer.sql

referer.phps

viewreferer_.phps


What is a Referer?


A Referer is the URL, from which the User comes to your Site. Let's say at
http://www.server.de/links.html there is a Link called "Your Site". He clicks
on it, then he'll get to http://www.your-site.de (this is your URL). Then
http://www.server.de/links.html is the Referer. The following Example shows
you, how to track these URLs and get a little Overview of the Pages which have
a Link to your Site.


$hostname = "localhost";
$username = "name";
$password = "passwort";
$userstable = "referer";
$dbName = "db-name";
$domain = "server.de";


Some Variables like the MySQL-Login and Password. $domain is your Domainname,
because we don't want it to show up in the Stats. If you can access
www.server.de as well as server.de, then only enter "server.de". This is
because everything containing $domain will be kicked out.


$ref = "$HTTP_REFERER";




Now we check the Referer, the Page from which the User came.


$ref2 = strtolower($ref);


With strtolower we set the Refere lowercase. This is because someone could have
accessed www.server.de as well as www.Server.de or WWW.SERVER.DE.


$check = strstr($ref2,"$domain");


In the lowercased Referer is $domain is searched.


if(!empty($check)){
}


If $check, the Search for the Domainname, doesn't find anything, nothing will
be printed out


elseif (empty($check)){
mysql_connect($hostname,$username, $password) or die(mysql_error());
mysql_select_db("$dbName") or die(mysql_error());


If a Referer from "outside", was found, a Connection to MySQL will be started.


$query = "select * from $userstable where url = '$ref2'";


Then the Table will be searched for $ref2, the lowercased Referer.


$result = mysql_query($query);
$rows = mysql_num_rows($result);


$rows counts all Entries found


if ($rows == 0){


If there is no Entry for $rows,





$hits = "1";


the Variable $hits will be set to1.


$query1 = "insert into $userstable (url,hits) values('$ref','$hits')";


Then the URL and the Hits will be inserted into the Database.


mysql_query($query1);
}


If an Entry was found (someone came from the Referer already),


else {
$hitquery = "select hits from $userstable where url = '$ref'";


this Entry will be used.


$result2 = mysql_query($hitquery);
$row = mysql_fetch_array($result2);
$hits = $row["hits"];


The hits-Field will be used in an Array.


$query2 = "update $userstable set hits = hits+1 where url = '$ref'";


Hits will be inreased by "1".


mysql_query($query2);
}
}


Now save this File as referer.php3 and call it like


include("referrer.php3");


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



The graphical Stats


We start again with the Variables to connect to MySQL.


$hostname = "localhost";
$username = "name";
$password = "passwort";
$userstable = "referer";
$dbName = "db-name";


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


$color10 = "#FFFF00";
$color20 = "#FF0000";
$color50 = "#008000"
$color1k = "#0000FF"
$color2k = "#0000A0"
$color5k = "#000040"


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


mysql_connect($hostname, $username,$password) or die(mysql_error());
mysql_select_db("$dbName") or die(mysql_error());


Now, all Data will be selected and sorted descending by the Number of Hits.


$query = "select * from $userstable order by hits desc";
$result = mysql_query($query);


The Number of Results will be checked..


$number = mysql_num_rows($result) or die (mysql_error());
$i = 0;


If no Referers have been tracked yet, an Error Message will be displayed.


if ($number == 0) {
print "<center><p><b>No Refererstrackedyet!</b></center>";
}


If Referers have been tracked:


elseif ($number > = 1) {
while ($i < $number){

$row = mysql_fetch_array($result);
$hits = $row[hits];

$ref = $row[url];



It will be checked if $ref is the Result of the Field "hits".


Now, Hits will be counted and the Color will be chosen.


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 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.


print"<divalign=\"left\">";
print "<tablecols=3 border= \"0\"width=\"100%\"><tr><td
align=leftwith=\"400\"><b><ahref=\"$url\">$url</a></b></td>";
print"<tdalign=rightwidth=\"60\"><b>$hits</b></td>";
print"<td align= leftwidth=\"$hits\" bgcolor=\"$color\"></td></tr>";
print"</table>\n";
}
}
$i++;
print"</table>\n";
}
}


Save this File as viewreferer.php3 and wait some Days. Then you'll have a
graphical Statistic of most of the Pages that have a Link to yours.









Watching The Web
Categories : PHP, Databases, MySQL, HTTP, MD5
Descriptions of Common Data Types
Categories : MySQL, Databases, PHP, PHP options/info, General
Beginners guide to PHP and MySQL
Categories : PHP, Beginner Guides, Databases, MySQL, Installation
Creating an IE-Only Database Driven Menu System With PHP, MySQL and DHTML
Categories : PHP, MySQL, Databases, DHTML
PHP, MySQL and Authentication 101
Categories : PHP, Databases, MySQL, Authentication
Building A Persistent Shopping Cart With PHP and MySQL
Categories : PHP, MySQL, Databases, Ecommerce
Case Study: Handling MySQL Growth With a PHP Class
Categories : Databases, MySQL, PHP
Practical Date and Time examples with PHP and MySQL
Categories : Databases, MySQL, PHP, Date/time
Simple Connection to MySQL with PHP
Categories : PHP, MySQL, Databases
Miles To Go Before I Sleep...
Categories : PHP, Calendar, Databases, MySQL
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
Date Arithmetic With MySQL
Categories : PHP, Databases, MySQL, Date Time
Time Is Money Part 1 of 2 - Designing and implementing a Web-based application
Categories : PHP, Databases, MySQL, Complete Programs
PHP and MySQL News with Comments
Categories : PHP, Databases, MySQL