PDA

View Full Version : link counter



tkv
12-29-2007, 07:43 PM
This is the SQL query to use on link counter.


#
# Table structure for table `LinkCounter`
#

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 I have named to (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 it unless you know what you're doing.
Remember to include your database information or an external file with these variables.


<?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.
Remember to include a file with your database variables or type them out.

<?php
$dbHost =; #Your db host
$dbUser =; #username
$dbPass =; #Pass
$dbname =; #Name of the database.
?>

<?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.
Remember to include a file with your database variables or type them out.

<?php
$dbHost =; #Your db host
$dbUser =; #username
$dbPass =; #Pass
$dbname =; #Name of the database.
?>

<?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
#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.!';


}

?>