Click - Download Counter

Written by @m_k_amin 25 December 2012

You may want to know how many times is a link clicked in your page or a file is downloaded. This script let you to add new links and counts the times it is hit. It also has a capability to build its database and table, and there is no need for further configuration except Database server information. Enjoy!

Code Snippet:

                                                
                                                <!-- this script is provided by https://www.phpfreecode.com coded by: Kerixa Inc. -->
<!-- This Script is from www.phpfreecpde.com, Coded by: Kerixa Inc-->
<?php
	$host="localhost"; // Host name
	$username="root"; // Mysql username
	$password=""; // Mysql password

function setup(){
echo('
<p style="color: #008000;	text-align: left;	font-size: 15pt;"">-Automatic setup is started...</p>
');
global $host,$username,$password,$link;
//$link=mysql_connect($host, $username, $password);
$sql= 'CREATE DATABASE info';
if (!mysql_query ($sql, $link)) die('
<p style="text-align: center;	font-size: 20pt;"><span style="color: #FF0000;">Failed to 
create database! </span><br><span style="font-size: 12pt;">&gt;&gt;Please check the parameters and database server&lt;&lt;</span></p>
');
$sql = "CREATE TABLE `info`.`info1` (
`ID` INT NOT NULL ,
`name` TEXT NOT NULL,
`link` TEXT NOT NULL,
`CC` INT NOT NULL 
) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;";

mysql_query($sql) or die('Setup Failed');
echo('
<p style="color: #008000;	text-align: left;	font-size: 15pt;"">-Automatic setup completed successfully. Your Database is ready!</p>
');
}

$link=mysql_connect($host, $username, $password);
if(!$link) die('
<p style="text-align: center;	font-size: 20pt;"><span style="color: #FF0000;">Failed to connect to the database! </span>
<br><span style="font-size: 12pt;">&gt;&gt;Please check the parameters and database server&lt;&lt;</span></p>
');

$db_name="info"; 
$result=mysql_select_db($db_name);
if(!$result){
	setup();
}

if (isset($_GET['link'])){
	$id=$_GET['link'];
	$sql="SELECT * FROM info1 WHERE ID='$id' LIMIT 1";
	$result=mysql_query($sql);
	$rows=mysql_fetch_array($result);
	$cc=$rows['CC'];
	$des=$rows['link'];
	$cc++;
	$sql="UPDATE info1 SET CC='$cc' WHERE ID='$id'";
	mysql_query($sql) or die(mysql_error());
	header("Location: $des");
}

if (isset($_GET['id'])&& isset($_POST['link'])){
	$id=$_GET['id'];
	$name=$_POST['name'];
	$link=$_POST['link'];
	$sql="INSERT INTO info1(ID,name,link,CC)VALUES('$id', '$name','$link','0')";
	$result=mysql_query($sql);
	if (!$result) die (mysql_error());

}
?>


<table style="border: 1px solid #000000;width: 600" align="center">
	<tr>
		<td style="font-family: 'Times New Roman', Times, serif;font-size: 17pt;text-align: center;width: 151px;color: #2214B9;border-style: solid;border-width: 1px;"><strong>ID</strong></td>
		<td style="font-family: 'Times New Roman', Times, serif;font-size: 17pt;text-align: center;color: #2214B9;border-style: solid;border-width: 1px;">
			<strong>Information</strong></td>
	</tr>
<?php
$sql="SELECT * FROM info1";
$result=mysql_query($sql);
$cntr=1;
if($result){
while($rows=mysql_fetch_array($result)){
	$cntr++;
?>
	<tr>
		<td style="width: 151px;border-style: solid;border-width: 1px;text-align: center; height: 39px; font-size: 14pt;">
		<strong><?php echo $rows['ID'] ?></strong></td>
		<td style="border-style: solid;border-width: 1px; height: 39px;padding: 8px"><?php echo "<a href='".$_SERVER['PHP_SELF'].'?link='.$rows['ID']."'>". $rows['name']."</a><i>&nbsp;&nbsp;&nbsp;Click Counts: ".$rows['CC']."</i>" ?></td>
	</tr>
<?php }} ?>
	<tr><form method="post" action="<?php echo $_SERVER['PHP_SELF']."?id=$cntr" ?>">
		<td style="border-style: solid;border-width: 1px;text-align: center;width: 151px; height: 51px; font-size: 14pt;">
		<strong><?php echo $cntr?></strong></td>
		<td style="border-style: solid;border-width: 1px;padding: 5px; height: 51px; text-align: right;">
			<table style="width: 100%">
				<tr>
					<td style="font-family: 'Times New Roman', Times, serif; color: #700909; width: 100px">
					<strong>Link Name:</strong></td>
					<td style="padding:5px">
			<input name="name" style="width: 320px; height: 28px; font-size: 14pt; font-family: 'Times New Roman', Times, serif;"></td>
				</tr>
				<tr>
					<td style="font-family: 'Times New Roman', Times, serif; color: #700909; width: 100px">
					<strong>Address:</strong></td>
					<td style="padding:5px">
			<input name="link" style="width: 320px; height: 28px; font-size: 14pt; font-family: 'Times New Roman', Times, serif;"></td>
				</tr>
			</table>
		
		<input name="Sub1" type="submit" value="Add" style="width: 89px; height: 45px; font-size: 20pt; font-family: 'Times New Roman', Times, serif; color: #0B7724; margin-left: 0px;">
	</td></form></tr>
</table>

<div style="text-align: center; height: 15px">

<br><font face="Tahoma"><a target="_blank" href="http://www.phpfreecode.com/"><span style="font-size: 8pt; text-decoration: none">PHP Free Code</span></a></font></div>
<a target='_blank' href='https://www.phpfreecode.com' style='font-size: 8pt; text-decoration: none'>Php Best Codes</a>                                                
                                            

Example:


About @m_k_amin

This user is pending a biography.

M

Comments


Here you can leave us commments. Let us know what you think about this code tutorial!

0 / 300

TRENDING POST
1
2
3
4
5
VISITORS
Online Users: 12
Recent Members: grkkid, Manaakividuinfo.com, karticksv, sava, tinatina
advertisement 2