0

I found [this URL shortener code][1] on the web.

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.

Edit:

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?

chaser7016
  • 595
  • 2
  • 10
  • 28
  • here is the URL shortening code http://www.phoenixheart.net/2009/06/write-your-own-url-shortener/comment-page-1/#comment-4916 – chaser7016 Aug 03 '09 at 17:43
  • FWIW, bit.ly answers requests directed to its IPs directly. e.g.: http://128.121.254.129/nmP3X is the same as http://bit.ly/nmP3X Additionally, there are hundreds of shorteners out there -- awe.sm, digg.com, etc. etc.; attempting to enumerate them all is fairly futile. – Frank Farmer Aug 03 '09 at 17:49
  • Yeah. We might suggest a check more along the lines of `if(strlen($url) < 20)`. – chaos Aug 03 '09 at 17:58
  • @chaos: that check would leave out correct urls, too. What about a.blogspot.com? – Adriano Varoli Piazza Aug 03 '09 at 18:11
  • chaos's suggestion works if you replace `20` with the length of your shortener's urls. if the link doesn't qualify (ie. `a.blogspot.com`) then give a message stating "sorry but we can't make that link any shorter". if your shortener can produce a shorter url than another shortener, one might argue that you are still providing a service. – deizel. Aug 22 '09 at 11:58

3 Answers3

3
if(preg_match('!^(?:http://)?(?:www\.)?(?:bit\.ly|tinyurl\.com|tr\.im)/!i', $url))
    die("Sorry, we do not shorten short URLs.");
chaos
  • 122,029
  • 33
  • 303
  • 309
2

You can use the following to check if the URL already was shortened:

if(preg_match('#^https?://[^/]*?(tinyurl\.com|tr\.im|bit\.ly)#i', $myUrl) {
    echo "Sorry, we do not shorten short URLs.";
} else {
    echo "We can shorten that!";
}
Andrew Moore
  • 93,497
  • 30
  • 163
  • 175
1

For these cases, in which you know exactly the urls to match, and they aren't patterns, strpos is simpler than the preg functions.

strpos takes the string to check, the match, an optional offset, and returns FALSE if the match wasn't in the string.

$url_chk = strtolower($original_url);
if (strpos($url_chk, 'bit.ly') === FALSE
 || strpos($url_chk, 'tinyurl.com')  === FALSE
 || strpos($url_chk, 'tr.im') === FALSE) {
    echo "Sorry, we do not shorten short URLs.";
} else {
    echo "We can shorten that!";
}

EDIT: I change the original url to lowercase to simplify the match, since the user might have submitted the url as tinyURL.com, for example.

SECOND EDIT To answer your followup: it seems the string with the shortened url is created in the line

$shorten_url = "{$config['host']}/$code";

which means that $config['host'] should contain the relevant portion of the URL. But it's not clear where that comes from. At a guess, it's created in config.php.

Our suggestions using die() or echo() are just suggestions, don't embed this directly into your sprintf or your code without actually adapting them.

Community
  • 1
  • 1
Adriano Varoli Piazza
  • 7,297
  • 5
  • 39
  • 50
  • Two issues: if strpos() returns 0, your code will fail to match. Also, it will falsely match http://example.com/some/path/tr.im/woo – Frank Farmer Aug 03 '09 at 17:51
  • Corrected the first issue, but not the second. It's a drawback of the solution, yes, but I think it's arguable whether it's worth it to use the preg_s or not. A more realistic example could be 'www.getattr.im', for that matter. Other people argued that the number of url shorteners is so big enumerating them is useless. Doing more than -say- 5 leaves you with an unsightly regex, too. – Adriano Varoli Piazza Aug 03 '09 at 18:08