tags:

views:

175

answers:

3

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!

+3  A: 
if(preg_match('!^(?:http://)?(?:www\.)?(?:bit\.ly|tinyurl\.com|tr\.im)/!i', $url))
    die("Sorry, we do not shorten short URLs.");
chaos
I think you need one of these ^ to make that complete ;)
Peter Bailey
@Peter Bailey: Yeah, that'd be good. :)
chaos
+2  A: 

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
+1  A: 

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.

Adriano Varoli Piazza
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
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