views:

362

answers:

3

i am on half of url shortening system i get the url from user then create code in mysql for that.Then i have to append coming code to my domain name(now i am working on localhost) like http://localhost/a5c3 then redirect it to real domain.I stuck in here.Code snippet would be good for me at least to understand what i am going to do or you can explain what i am gonna do.

+1  A: 

if you are not associating short code with a url then you need to do that, and redirection will be easy.

Algorithm:

Save url from the form and generated code to the database for later use.

$url = $_POST['url']
generate code
Concatenate your url with the code
$full_url = $url.$code

show shortened url to user.

if you want redirect user, after he/she put the url in the browser addres then do this. create .htaccess file add following lines to it and drop it to your root folder.,

RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php?code=$1 [L]

.htaccess file will redirect everything to your index.php. for example if user types http://example.com/ujijui then .htaccess will call http://example.com/index.php?code=ujijui. so you can capture query string in the url by using $_GET.

in your index.php

$code = $_GET['code']
mysql_connect('localhost', 'user', 'password')
mysql_select_db('your_db')
$sql = "SELECT url from Table where code=$code" 
$result = mysql_query($sql)
loop through result and get url

header("Location: $url")

get it, this is just algorithm.

Mohamed
user enter url to the form then create the code in background concatenate with my domain name (http://localhost),then show it to the user.Then when user put that link in browser bar which code will be executed
Burak Dede
sorry if not clear can you explain the lines in .htaccess file
Burak Dede
your question is not clear. somehow you need to save values to the dabase for later us. and as far as I can see you are not doing that.
Mohamed
in database table i have id,url and code coloumns.when i get the url from user at the back i check if it is in db before then get the code from database created before ,and if not get the next id available and craete code according to that id(36 base convertion) like 'a34b'.then i am stuck here how to create link with my domain name like http://localhost/a34b and send user to actual link that want to shorten.
Burak Dede
+1  A: 

You need to have your server redirect non-existant URL's to an existing page (for example, using mod_rewrite on Apache). This 'catch-all' page will read the URL, check if the code given exists in the database, and if so, redirect to the proper url. Ainab's pseudocode explains the last part.

if you are not associating short code with a url then you need to do that, and redirection will be easy.

SELECT url from Table where code=code header("Location: $url")

Duroth
+1  A: 

For the redirection problem, you should try something like this:

the .htaccess file:

RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteRule ^(.*)$ index.php?url=$1 [L]

And in the index.php:

<?php
$url = $_GET['url'];

// These will be taken from database
$urls_associations = array(
  '1234' => "http_//www.example.com",
  '5678' => "http_//www.google.com",
  '90AB' => "http_//stackoverflow.com",
);

// Redirect
if (isset($urls_associations[$url])) {
  $redirect = $urls_associations[$url];
  header("Location: $redirect");
  echo "<a href='$redirect'>Go To : $redirect</a>";
} else {
  echo "Unknown URL code.";
}

Then , when the user goes to eg. http_//localhost/1234, he gets redirected to http_//example.com, etc. Of course, you should run a query on the database instead of reading from an array, but it looks quite easy, just use something like:

$code = mysql_escape_string($url);
$res = mysql_query("SELECT url FROM mytable WHERE code='$code'");
if ($redirect = mysql_result($res)) {
  header("Location: $redirect");
  echo "<a href='$redirect'>Go To : $redirect</a>";
} else {
  echo "Unknown URL code.";
}

(NOTE: in $urls_associations replace '_' with ':', i cannot post three links because I'm a new user here :))

redShadow