[ Curiosity,Experimentation ]

Random stuff from the parallel universe of Ones and Zeroes

A URL Shortening Service with Code [PHP]

Posted by appusajeev on January 5, 2010


Alright,so we gonna create a URL shortening service in PHP this time,similar to tinyurl. The thing is pretty simple,involves basically database interactions only (nevertheless,tinyurl is a perfect example of how an idea can change the world,however simple it may be).

Anyway i have launched the service.Check it out:  http://su.coolinc.info (I couldnt find a shorter domain)

The URL entered by the the user is mapped to a short string which could be either the alias entered by the user or a random string of length three(the length can be increased in case we run out of new combinations,then again 26*26*26 combinations should suffice (using only lower case alphabets)).This mapping is stored in a table url_short with fields url and alias (MySQL is the DBMS).
When the shortened URL  is executed,the original URL corresponding to the alias is fetched from the database  and the user is redirected to that page.

Heres the source of init.php,to generate the database and table for use. You will have to modify the argumets of mysql_connect() (server,usename,password) depending on the settings of the hosting provider.The below code is what i ran on my computer.

Setup script to create the database

Setup script to create the database and table (MySQL)

Source of  index.php,the main page

Source of index.php, the main page

Source of index.php, the main page

Source of put.php,containing the script to insert the mapping into the database .

Source of put.php, the script to perform database interactions

Source of put.php, the script to insert into database

Advertisements

8 Responses to “A URL Shortening Service with Code [PHP]”

  1. Vishak said

    upload it to a new shorter domain

  2. Thank you for the post. I will want to read more from you.

  3. clark said

    great article, thanks for sharing.

  4. Wow! Thank you! I always wanted to write in my web page a thing like that. Can I take a part of the post to my blog?

  5. That’s Too nice, when it comes in india hope it can make a Rocking place for youngster.. hope which come true.

  6. Just desire to say your article is as astonishing. The clarity in your post is just cool and i can assume you are an expert on this subject. Fine with your permission allow me to grab your RSS feed to keep up to date with forthcoming post. Thanks a million and please keep up the gratifying work.

  7. I like the helpful information you provide in your articles. I’ll bookmark your blog and check again here frequently. I am quite certain I’ll learn lots of new stuff right here! Good luck for the next!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: