Okay now i'm going to teach y'all how to keep records about your links.. see how many times they have been clicked, when and what addresses..
First we must create one table:
MySQL Code:
CREATE TABLE `test`.`links` (
`id` INT( 10 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ;
`url` VARCHAR( 100 ) NOT NULL ,
`clicks` INT( 10 ) NOT NULL ,
`TIME` VARCHAR( 10 ) NOT NULL ,
`DATE` VARCHAR( 30 ) NOT NULL
) ENGINE = INNODB
Now let's create the main script:
links.php
PHP Code:
<?php
// Define URL and some other stuff
$url = $_GET['url'];
$date = date("l, jS F, Y");
$time = date("H:i");
if(!isset($url) || empty($url) || $url == ""){
die("<h1>You don't have permissions to view this file!</h1>");
}
// Connect to database
$con = mysql_connect("localhost", "username", "password");
if(!$con){
die(mysql_error());
}
// Select your database
$selDB = mysql_select_db("database", $con);
if(!$selDB){
die(mysql_error());
}
// Now let's find do we have any records about the url you entered
$findURL = mysql_query("SELECT * FROM links WHERE url='$url'");
$findURLnum = mysql_num_rows($findURL);
// If we don't have any, let's create it's first record
if($findURLnum == "0"){
$insert = mysql_query("INSERT INTO links (url, clicks, time, date) VALUES ('$url', '1', '$time', '$date')");
if(!$insert){
die(mysql_error());
// If we have records about that url, let's update it
}
}elseif($findURLnum != "0"){
$findClicks = mysql_query("SELECT * FROM links WHERE url='$url'");
$query = mysql_fetch_array($findClicks);
$findClicksOld = $query['clicks'];
$oldnum = $findClicksOld;
$newnum =1;
$clicksNew = $oldnum+$newnum;
// Update clicks
$updateClicks = mysql_query("UPDATE links SET clicks='$clicksNew' WHERE url='$url'");
if(!$updateClicks){
die(mysql_error());
}
// Update time
$updateTime = mysql_query("UPDATE links SET time='$time' WHERE url='$url'");
if(!$updateTime){
die(mysql_error());
}
// Update date
$updateDate = mysql_query("UPDATE links SET date='$date' WHERE url='$url'");
if(!$updateDate){
die(mysql_error());
}
}
?>
Now let's create that HTML part:
HTML Code:
<html>
<head>
<?php
echo "<META http-equiv='refresh' content='3;URL=$url'>";
?>
</head>
<body>
<center><font color="#FF0000">You will be redirected to <?php echo $url; ?>!</font></center>
</body>
</html>
Here's full script:
Code:
<?php
// Define URL and some other stuff
$url = $_GET['url'];
$date = date("l, jS F, Y");
$time = date("H:i");
if(!isset($url) || empty($url) || $url == ""){
die("<h1>You don't have permissions to view this file!</h1>");
}
// Connect to database
$con = mysql_connect("localhost", "username", "password");
if(!$con){
die(mysql_error());
}
// Select your database
$selDB = mysql_select_db("database", $con);
if(!$selDB){
die(mysql_error());
}
// Now let's find do we have any records about the url you entered
$findURL = mysql_query("SELECT * FROM links WHERE url='$url'");
$findURLnum = mysql_num_rows($findURL);
// If we don't have any, let's create it's first record
if($findURLnum == "0"){
$insert = mysql_query("INSERT INTO links (url, clicks, time, date) VALUES ('$url', '1', '$time', '$date')");
if(!$insert){
die(mysql_error());
// If we have records about that url, let's update it
}
}elseif($findURLnum != "0"){
$findClicks = mysql_query("SELECT * FROM links WHERE url='$url'");
$query = mysql_fetch_array($findClicks);
$findClicksOld = $query['clicks'];
$oldnum = $findClicksOld;
$newnum =1;
$clicksNew = $oldnum+$newnum;
// Update clicks
$updateClicks = mysql_query("UPDATE links SET clicks='$clicksNew' WHERE url='$url'");
if(!$updateClicks){
die(mysql_error());
}
// Update time
$updateTime = mysql_query("UPDATE links SET time='$time' WHERE url='$url'");
if(!$updateTime){
die(mysql_error());
}
// Update date
$updateDate = mysql_query("UPDATE links SET date='$date' WHERE url='$url'");
if(!$updateDate){
die(mysql_error());
}
}
?>
<html>
<head>
<?php
echo "<META http-equiv='refresh' content='3;URL=$url'>";
?>
</head>
<body>
<center><font color="#FF0000">You will be redirected to <?php echo $url; ?>!</font></center>
</body>
</html>
I will create stats page part later