I'm a noob. I found [this URL shortener code][1] on the web. Pretty simple and straightforward and this noob was able to install, configure and get it up and running with little issue!
Now I want to add something. I want to throw an error to user if they enter a link from bit.ly, TinyURL.com or tr.im. I want it to say, "Sorry, we do not shorten short URLs." I know I need to create an if
and else
statement, but I don't know what things I need to call.
Cont... Thanks a lot for the input! Unfortunately I am totally confused. Where do I place these code suggestions? At the top of the index.php. Right now I have the following php code..
<?php
require_once('config.php');
require_once('functions.php');
if ($url = get_url())
{
$conn = open_connection();
$res = mysql_query("SELECT `id` FROM `urls` WHERE `url`='$url' LIMIT 1", $conn);
if (mysql_num_rows($res))
{
// this URL entry already exists in the database
// get its ID instead
$row = mysql_fetch_object($res);
$id = $row->id;
}
else
{
// a new guy, insert it now and get its ID
mysql_query("INSERT INTO `urls`(`url`) VALUES('$url')", $conn);
$id = mysql_insert_id($conn);
}
// now convert the ID into its base36 instance
$code = base_convert($id, 10, 36);
$shorten_url = "{$config['host']}/$code";
// and beautifully display the shortened URL
$info = sprintf('
<span class="long-url" style="visibility: hidden;">%s</span>
<span style="visibility: hidden">%d</span> <br>
Link to drop:
<a class="shorteen-url" href="%s">%s</a>
<span style="visibility: hidden">%d</span>',
$_GET['url'], strlen($_GET['url']),
$shorten_url, $shorten_url,
strlen($shorten_url));
}
?>
I'm not using $info = sprintf
... maybe I should replace $info = sprintf
with one of the suggestions below?
thanks for helping this noob!