Link Shortening Script

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

  1. index.php
  2. .htaccess
  3. 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" /> &nbsp; <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

# .htaccess

RewriteEngine On
Options +FollowSymlinks


RewriteRule ^v-(.*)$ go.php?id=$1 [QSA,L,NC]
1
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

# 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

Inorder to run this program XAMPP was used as Local Server.

# Enhancements

This project can be improved further with the following points

  1. Adding User Login Details
  2. Restricting Guest with limited URL shortening
  3. Restricting Guest from making many URL shorten at once by adding flood limit
  4. Logged in user has more access and features on making URL short
  5. Logged in users can maintain their shortened URL's using control panel
  6. Custom URL's can be made for Premium User [Paid/VIP]
  7. Adding session for checking logged in users
  8. Restricting bots from flooding server load
  9. Admin Panel to maintain users & URL's
  10. Filter to ban restricted sites
  11. Ads on links of free user & premium users link without ads [Full Page Ads]
  12. Getting a good custom made skin for this project
  13. Listed too many features to make this project much better, if i get time will start implementing one after the other