OMG!I am geek
Posts: 53
434 credits Members referred : 0
« on: Nov 04, 2005, 02:57:11 pm »
well, for those that dont know, i wrote a tutorial for referers of a site. It is a mysql database referer. The code comes with the comments and should be self explanitary
Code:
<?php /*================================== USER EDITS BELOW ==================================*/ $DB = array( "host" => "localhost", //Your host "user" => "", //Sql User "pass" => "", //Sql Password "db" => "", #Sql Database Name ); /*================================== DONT EDIT BELOW UNLESS YOU NO WHAT YOUR DOING! ==================================*/ //connect to the database $conn = mysql_connect($DB["host"], $DB["user"], $DB["pass"]); mysql_select_db($DB["db"], $conn) or die("Mysql Said:". mysql_error().":"mysql_errno()); //before we carry on lets make the table... /*===================================================== //NOTE : MUST DELETE THIS AFTER YOU HAVE RUN IT ONCE!\\ =====================================================*/ if(!mysql_query("CREATE TABLE `referers` ( `id` int(11) NOT NULL auto_increment, `url` varchar(255) NOT NULL default '', `name` varchar(255) NOT NULL default '', `hits` int(11) NOT NULL default '0', PRIMARY KEY (`id`) ) TYPE=MyISAM;")) { //if we cant make it, display why we cant make it... die("Mysql Said:". mysql_error().":"mysql_errno()); } //DONT DELETE NO MORE /*Use of this function? checks to see if there is a referer if there is check if it is in the db if it isnt in the db insert it if it is in the db update it*/ function updateref() { global $conn, $DB; //get the main link of the referer //http://test.com?page=test&trim=2 //shortened to http://test.com $referer = preg_replace("/(http:\/\/(.*)\/)[\S]*/", "\\1", $_SERVER['HTTP_REFERER']); $name = preg_replace("/http:\/\//", "", $referer); $name = preg_replace("/\//", "", $name);
//is there a referer?
if(!$referer) {
//no?! :O return false return FALSE;
}
if($referer) {
//good there is a referer $query = mysql_query("SELECT * FROM referers WHERE url='$referer'", $conn);
//yes he has, update the link mysql_query("UPDATE referers SET hits=hits+1 WHERE url='$referer'", $conn);
}
} } /*Use of this function? show the referers on your site name, link, how many hits they have*/ function showrefs() { global $DB, $conn; //the query... //Select and order in descending order the hits they have $query = mysql_query("SELECT * FROM referers ORDER BY hits DESC LIMIT 10", $conn); while($ref = mysql_fetch_array($query)) { //echo the link, name and how many times they have been referered to ur site echo "<a href=\"{$ref['url']}\">{$ref['name']} ({$ref['hits']})</a><br>"; } } ?>
when you save the file include it into your page and call the functions
just to make things simple and easy for people who have no idea what anything in php is, regulate the code a little
From:
Code:
<?php /*================================== USER EDITS BELOW ==================================*/ $DB = array( "host" => "localhost", //Your host "user" => "", //Sql User "pass" => "", //Sql Password "db" => "", #Sql Database Name ); /*================================== DONT EDIT BELOW UNLESS YOU NO WHAT YOUR DOING! ==================================*/ //connect to the database $conn = mysql_connect($DB["host"], $DB["user"], $DB["pass"]); mysql_select_db($DB["db"], $conn) or die("Mysql Said:" mysql_error().":"mysql_errno()); //before we carry on lets make the table... /*===================================================== //NOTE : MUST DELETE THIS AFTER YOU HAVE RUN IT ONCE!\\ =====================================================*/ if(!mysql_query("CREATE TABLE `referers` ( `id` int(11) NOT NULL auto_increment, `url` varchar(255) NOT NULL default '', `name` varchar(255) NOT NULL default '', `hits` int(11) NOT NULL default '0', PRIMARY KEY (`id`) ) TYPE=MyISAM;")) { //if we cant make it, display why we cant make it... die("Mysql Said:"mysql_error().":"mysql_errno()); }
To:
Code:
<?php /*================================== USER EDITS BELOW ==================================*/ $DB_host = "localhost";//Your host $DB_user = "user"; //Sql User $DB_pass = "pass"; //Sql Password $DB = "db"; //Sql Database Name
/*================================== DONT EDIT BELOW UNLESS YOU NO WHAT YOUR DOING! ==================================*/ //connect to the database $conn = mysql_connect($DB_host, $DB_user, $DB_pass); mysql_select_db($DB, $conn) or die("Mysql Said: ".mysql_error().":".mysql_errno());
Then save this as a new file:
Code:
<?php /*===================================================== //NOTE : RUN ONCE, THEN REMOVE THE FILE FROM YOUR SERVER\\ =====================================================*/ if(!mysql_query("CREATE TABLE `referers` ( `id` int(11) NOT NULL auto_increment, `url` varchar(255) NOT NULL default '', `name` varchar(255) NOT NULL default '', `hits` int(11) NOT NULL default '0', PRIMARY KEY (`id`) ) TYPE=MyISAM;")) { //if we cant make it, display why we cant make it... die("Mysql Said: ".mysql_error().":".mysql_errno()); }
p.s. also fixed some possibly errors with the die()'s check to see if your's worked (im pretty sure the mission '.'s would cause an error)
« Last Edit: Nov 04, 2005, 03:30:48 pm by Nikolas »
Trackback URI for this entry : http://www.webdigity.com/trackback.php?topic=652