This will allow you to create a script that will count the amount of times a certain link has been clicked and display the results on your page.
SQL Query
| DROP TABLE IF EXISTS 'LinkCounter';
CREATE TABLE 'LinkCounter' ( 'ID' int(11) NOT NULL auto_increment, 'name' text NOT NULL, 'address' text NOT NULL, 'description' text NOT NULL, 'count' int(11) NOT NULL default '0', KEY 'ID' ('ID') ) TYPE=MyISAM AUTO_INCREMENT=1; |
The next file is named 'linkout.php'. If you wish to switch this name you must also switch it in the display code accordingly. I don't suggest you switch this unless you know what you are doing.
Remember to include your database information on all your pages. It is best to keep your details in a separate page.
| <?php
$dbHost =; #Your db host $dbUser =; #username $dbPass =; #Pass $dbname =; #Name of the database. ?>> |
| <?php
#include your database variables function dbinsans($text) { $text = strip_tags ($text, ""); $text = str_replace(chr(10),"",$text); $text = str_replace(chr(13), "<br>", $text); $text = str_replace(""","*",$text); $text = str_replace("'","*",$text); $text = addslashes($text); return($text); } /* This function is used to check the input, a hacker could break the query and use his own query if we're not careful and don't check this stuff */ $link=dbinsans($_GET['link']); $db = mysql_connect("$dbHost","$dbUser","$dbPass"); mysql_select_db($dbname,$db); $requete = "SELECT * FROM LinkCounter WHERE ID='$link'"; $result = mysql_query ($requete,$db); #Should return only one value, our link if it exists $article = mysql_fetch_object($result); #Gives an object with our row back $addy = $article->address; #Gets the address of the link $count = $article->count + 1; #Gets and adds one to counter variable $sql = "UPDATE LinkCounter SET count='$count' WHERE ID='$link'"; #Updates count mysql_query($sql, $db); #Queries the database and updates the count at ID. mysql_free_result($result); header("Location: ".$addy); #like Leo's tutorial this redirects the browser exit(); ?> |
This next piece of code can go where you want the links to go, alternatively if you only want certain links to appear you could manipulate the query, maybe by adding another field with a section name and using the WHERE command in the query to match it.
| <?php
#include database variables $db = mysql_connect("$dbHost","$dbUser","$dbPass"); mysql_select_db($dbname,$db); $requete = "SELECT * FROM LinkCounter"; $result = mysql_query ($requete,$db); while ($article = mysql_fetch_object($result)) { echo '<a href="linkout.php?link='.$article->ID.'">'.$article->name.'</a> Clicked ('.$article->count.') Times - '.$article->description.'<br />'; } mysql_free_result($result); ?> |
Admin Section Script, remember to include the database variables. This section will allow you to easily add links... I highly suggest you put this in an admin section or a password protected directory for protection.
| <?php
if (!isset($_POST['submit'])) { echo '<form method="post" action="'.$_SERVER['SCRIPT_NAME'].'" />'; echo 'Name Of Link : <input type="text" name="name" /><br />'; echo '<br />'; echo 'URL of Link (Include http://) : <input type="text" name="address" /><br />'; echo '<br />'; echo 'Description of Link : <input type="text" name="description"><br /><br />'; echo "<input type="submit" name="submit" value="Submit" />"; echo '</form>'; } else { $db = mysql_connect("$dbHost","$dbUser","$dbPass"); mysql_select_db($dbname,$db); # I think we can trust our own input # since this should be in a protected spot $name = $_POST['name']; $address = $_POST['address']; $description = $_POST['description']; $sql="INSERT INTO LinkCounter (name, address, description) VALUES ('$name','$address', '$description')"; mysql_query($sql, $db); echo 'Thank you your input it is now added.!'; } ?> |



