Detect if URL is a URL shorter

-1

I have a text input in an HTML form where the system user inserts a link. This link is copied from the URL. I need to accept any kind of link, except for those 'shortened' URLs like 'goo.gl.'. How can I create a function to validate and verify this? Is it possible?

    
asked by anonymous 18.04.2016 / 00:19

2 answers

2

One suggestion is to make a request in the URL. If it detects redirection, it may be a "shortening", but it may not be and would then be preventing legitimate URLs.

From this point on, compare the number of characters. If it is smaller than X it increases the likelihood of being a shorter and discards larger URLs that redirect, but are not shortened.

It looks good so far, but in practice, it does not work.

Let's take a real example:

tinyurl.com < - is a URL shortener for globo.com is smaller than tinyurl.com but is not a shortcutser.
r7.com < - is smaller yet.

So the logic in comparing the URL or domain size is not valid.

Maybe you should ask yourself, what's the point of detecting if it's a URL shorter?

If it's because they redirect a URL, then they could only validate the redirect, regardless of whether it's a shorter or not.

    
18.04.2016 / 00:53
2

What you can do is to insert into a array or MySQL (depending on the type of language you will use) some of the URL's that are shortened. For example goo.gl , tinyurl.com , etc.

A code made with PHP for example:

    <?php
    $urls = array('goo.gl', 'tinyurl.com'); //Insere as URL's que são encurtadoras

    $url_input = trim(strtolower($_POST['url'])); //Pega a URL que o usuário informou

$filtra_url = parse_url($url_input);

$url = $filtra_url['host'];


    if(in_array($url, $urls)){
    echo 'Você informou uma URL encurtada. Não permitimos isso!';
    }else{
    echo 'URL autorizada';
    }
    ?>
    
18.04.2016 / 06:11