Quick and dirty url shortener in php

· by Willy T. Koch · Read in about 2 min · (356 Words)

I neeeded a simple URL shortener for a framed image site (the framing wasn’t my idea). It had to work on non-php pages, so I came up with an iframe solution.

url/index.php (this file looks up the shortened URL and redirects)

<?php
$link = mysql_connect('localhost', 'user', 'password') or die('Could not connect: ' . mysql_error());
mysql_select_db('url') or die('Could not select database');
// Performing SQL query
$query = "SELECT * FROM links WHERE linkin = '".mysql_real_escape_string($_GET['u'])."'";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$line = mysql_fetch_array($result, MYSQL_ASSOC);
header("Location: ".$line['linkout']." ");
?>




<strong>url/iframe.php (this file looks up or creates a short url, and gets the url to be processed from the url of the embedded iframe src. And then displays the link so the user can copy it.</strong>
<?php
//Check to only allow the URL shortener to work from certain domains
if (strstr($_SERVER["HTTP_REFERER"],"example.com"))
{
$link = mysql_connect('localhost', 'user', 'password') or die('Could not connect: ' . mysql_error());
mysql_select_db('url') or die('Could not select database');
// Performing SQL query
$query = "SELECT * FROM links WHERE linkout = '".mysql_real_escape_string($_GET['url'])."'";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
$num_rows = mysql_num_rows($result);
if ($num_rows==0) // if no match, create a new shortened url
{
$id=rand(10000,99999);
$shorturl=base_convert($id,20,36);
$query = "INSERT INTO links (linkout,linkin) VALUES ('".mysql_real_escape_string($_GET['url'])."','".$shorturl."')";
$result = mysql_query($query) or die('Query failed: ' . mysql_error());
echo 'Link to this page:<br><a href="http://example.com/url/?u='.$shorturl.'">http://img.example.com/url/?u='.$shorturl.'</a>';
}
else
{
$line = mysql_fetch_array($result, MYSQL_ASSOC);
echo 'Link to this page:<br><a href="http://example.com/url/?u='.$line['linkin'].'">http://img.example.com/url/?u='.$line['linkin'].'</a>';
}
} //End domain check
?>
</body>

contentpage.html (this is the page on which you want to display the shortened link, in my case a framed page) The trick is getting the page url and passing it to the php iframe as a url parameter.  

<head>
<script type="text/javascript">

function sURL() {
    var href = escape(document.location.href);
    var site = "http://img.example.com/url/iframe.php?url="+href;
    document.getElementById('myIframe').src = site;
}
</script>
</head>
<body onLoad="sURL();">
<iframe id="myIframe" frameborder="0" name="myIframe" src="http://img.example.com/url/load.html" width="200" height="40" scrolling="no"></iframe>
</body>

****url/load.html (this file is just a filler, displays a loader. Could also just be an empty file) Get one at www.ajaxload.info


<html>
<body>
<img src="ajax-loader.gif">
</body>
</html>

If you have any questions, or if it doesn’t work as expected, just post a comment.