Link Shortening Script
Rohan Sakhale 6/27/2011 code-examplephp
# Summary
This is a small project quickly made by me after seeing link shortners on web. We require only three files for our small project
- index.php
- .htaccess
- go.php
Also we require a database connection inorder to maintain all the links with their access id's
# Code
# index.php
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>URL Shortening Project</title>
</head>
<body>
<form action="" method="get">
<input type="text" name="url" /> <input type="submit" />
</form>
<?php
if($_GET['url'])
{
$con = mysql_connect("localhost","root","");
if(!$con)
{
die('Could not connect: '. mysql_errno());
}
mysql_select_db("url",$con);
$id = uniqid();
$url = $_GET['url'];
$sql = "INSERT INTO `url`.`t_link` (`id`, `url`) VALUES ('$id', '$url');";
mysql_query($sql);
echo "Your Short URL: http://localhost/url/v-". $id;
mysql_close($con);
}
?>
</body>
</html>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# .htaccess
RewriteEngine On
Options +FollowSymlinks
RewriteRule ^v-(.*)$ go.php?id=$1 [QSA,L,NC]
1
2
3
4
5
2
3
4
5
# go.php
<?php
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
if ($_GET['id']) {
$id = $_GET['id'];
$con = mysql_connect("localhost", "root", "");
if (!$con) {
die('Could not connect: ' . mysql_errno());
}
mysql_select_db("url", $con);
$sql = "SELECT * FROM `t_link` where id = '$id'";
$result = mysql_query($sql);
$row = mysql_fetch_array($result);
$new_url = $row['url'];
echo "<html><head><meta HTTP-EQUIV='REFRESH' content='0;url=$new_url'></head></html>";
mysql_close($con);
}
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# SQL Script
create table url (
id integer primary key autoincrement,
access_id varchar (20) not null,
url varchar (100) not null
);
1
2
3
4
5
2
3
4
5
Inorder to run this program XAMPP was used as Local Server.
# Enhancements
This project can be improved further with the following points
- Adding User Login Details
- Restricting Guest with limited URL shortening
- Restricting Guest from making many URL shorten at once by adding flood limit
- Logged in user has more access and features on making URL short
- Logged in users can maintain their shortened URL's using control panel
- Custom URL's can be made for Premium User [Paid/VIP]
- Adding session for checking logged in users
- Restricting bots from flooding server load
- Admin Panel to maintain users & URL's
- Filter to ban restricted sites
- Ads on links of free user & premium users link without ads [Full Page Ads]
- Getting a good custom made skin for this project
- Listed too many features to make this project much better, if i get time will start implementing one after the other