1

I have an input box that tells uers to enter a link from imgur.com I want a script to check the link is for the specified site but I'm not sue how to do it?

The links are as follows: https://i.sstatic.net/RYnCi.jpg Please note that after the /, the text may vary e.g. not be a jpg but the main domain is always http://i.imgur.com/.

Any help appreciated. Thanks, Josh.(Novice)

6 Answers 6

3

Try parse_url()

try {
    if (!preg_match('/^(https?|ftp)://', $_POST['url']) AND !substr_count($_POST['url'], '://')) {
        // Handle URLs that do not have a scheme
        $url = sprintf("%s://%s", 'http', $_POST['url']);
    } else {
        $url = $_POST['url'];
    }

    $input = parse_url($url);

    if (!$input OR !isset($input['host'])) {
        // Either the parsing has failed, or the URL was not absolute
        throw new Exception("Invalid URL");
    } elseif ($input['host'] != 'i.imgur.com') {
        // The host does not match
        throw new Exception("Invalid domain");
    }

    // Prepend URL with scheme, e.g. http://domain.tld
    $host = sprintf("%s://%s", $input['scheme'], $input['host']);
} catch (Exception $e) {
    // Handle error
}
Sign up to request clarification or add additional context in comments.

7 Comments

parse_url() can fail, therefore you need to make sure it !== false before checking $input['host']
@simshaun That is true, but not in the "quick" answer. I've enhanced the answer for you
??? Quick answer or not, it's necessary or else you've introduced a potential bug in your code.
Is there anyway to get this to insert http:// dependent on whether its been provided or not?
@JoshLukeBlease parse_url will only work in full if the URL is absolute, but I have added a sanity check for http/https/ftp
|
2
substr($input, 0, strlen('http://i.imgur.com/')) === 'http://i.imgur.com/'

Comments

2

Check this, using stripos

if(stripos(trim($url), "http://i.imgur.com")===0){
 // the link is from imgur.com
}

Comments

1

Try this:

<?php
if(preg_match('#^http\:\/\/i\.imgur.com\/#', $_POST['url']))
    echo 'Valid img!';
else
    echo 'Img not valid...';
?>

Where $_POST['url'] is the user input.

I haven't tested this code.

Comments

1
$url_input = $_POST['input_box_name'];
if ( strpos($url_input, 'http://i.imgur.com/') !== 0 )

...

3 Comments

Needs to be !== FALSE or == 0. I'd opt for the latter since you would expect it to be act the beginning of the string.
!== FALSE won't give us the position check, just the presence one. ) And ==0 is just not right, as it'd the same response for FALSE as well.
I hinted the position check, and that the latter would be better. But yes you are right, it needs to be === 0. Edit: Just realized you probably meant the condition for throwing an error. Ignore me.
1

Several ways of doing it.. Here's one:

if ('http://i.imgur.com/' == substr($link, 0, 19)) {
    ...
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.