4

I am trying to verify URLs. This is the code I have:

function isValidURL($url) 
{
    return preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}

This code is working but now I am trying to work out how to add http:// or https:// if the URL is correct but is just missing the beginning http://

Please point me in the right direction.

5 Answers 5

6

Use the filter functions, there's a FILTER_VALIDATE_URL for this.

$is_valid_url = filter_var($url, FILTER_VALIDATE_URL);

There's a bunch of options too, see here.

To detect if there's a missing http:// or not, just test your input without modifying it first, and try prepend it and test again if that fails.

Sign up to request clarification or add additional context in comments.

Comments

3

You can use parse_url() to verify your url http://php.net/manual/en/function.parse-url.php

<?php

$url = "https://twitter.com?id=3232";
$url_info = parse_url($url);

echo "<pre>";
    print_r($url_info);
echo "</pre>";


?>

Output

Array
(
    [scheme] => https
    [host] => twitter.com
    [query] => id=3232
)

I believe you can even work with this function parse_url you will get lot of parameters easily and understandable format.

so your code will be

<?php

function isValidURL($url) {
    $varr = parse_url($url);
    if ($varr['scheme'] == 'https') {
        return true;
    }
    return false;
}
?>

Note : Url used above is not valid, its for testing purspose

Comments

1

To fix the HTTP(s) issue with Regex, you may set http(s)?:// between parentheses and make it optional with ?:

var_dump(isValidURL("stackoverflow.com/questions/15453684/php-url-verification-and-if-not-valid-fix-it"));

function isValidURL($url) {
 return preg_match('|^(http(s)?://)?[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $url);
}

EDIT: Thanks to DCSoft, it seems that the above regex also validates something like fijsfsdufd, here's a quick fix, by adding a dot:

function isValidURL($url) {
 return preg_match('|^(http(s)?://)?[a-z0-9-]+\.(.[a-z0-9-]+)+(:[0-9]+)?(/.*)?$|i', $url);
}

This may be interesting.

5 Comments

The problem with this is it lets the user type in what ever they want e.g - asdsaasdsad and it would still count it as being a valid URL
Thank you, it works perfectly but would you say for time optimization, it would be better to run it as a JavaScript on the form and stop the user submitting it or would it be better to do it in the php script?
@DCSoft Do it on both, never trust users input, they may just send it as HTTP/POST|GET request or disable JS
If I was going to do it as a javascript would the code be like - pastebin.com/aRPi7WHb
If you're using HTML5, try also to look at the required attribute by using something like <input type="url" required="required">
1

You can use strpos as such:

if (strpos($url,'http://') === false){
    $url = 'http://'.$url;
}

Comments

1

Based on @seanwm, here is a function that checks and fixes the URL.

PHP function

function fixUrl($url, $forceSSL = false) {

    if (strpos($url,'http://') === false 
    && strpos($url,'https://') === false) {

        if($forceSSL) {
            return 'https://'.$url;
        }

        return 'http://'.$url;
    }

    return $url;
}

Use

For http:// :

$url = fixUrl($url);

force to prepend https:// :

$url = fixUrl($url, true);

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.