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.
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.
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")
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 :))