Jump to content

[SOLVED] Checking to see if a variable is a valid URL


ArizonaJohn

Recommended Posts

Hi,

 

For the code below, my intention is that the user enters in a valid URL for the variable $site.  But I have nothing to stop the user from entering in non-valid URLs.  Is there a way I could check to see if $site is a valid URL, and then reject it if it's not?

 

Thanks in advance,

 

John

 

$remove_array = array('http://www.', 'http://', 'www.');
$site = str_replace($remove_array, "", $_POST['site']);
$site = strtolower($site);

Link to comment
Share on other sites

this smells like a regex question  :P

// SCHEME 
$urlregex = "^(https?|ftp)\:\/\/"; 

// USER AND PASS (optional) 
$urlregex .= "([a-z0-9+!*(),;?&=\$_.-]+(\:[a-z0-9+!*(),;?&=\$_.-]+)?@)?"; 

// HOSTNAME OR IP 
$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)*";  // http://x = allowed (ex. http://localhost, http://routerlogin) 
//$urlregex .= "[a-z0-9+\$_-]+(\.[a-z0-9+\$_-]+)+";  // http://x.x = minimum 
//$urlregex .= "([a-z0-9+\$_-]+\.)*[a-z0-9+\$_-]{2,3}";  // http://x.xx(x) = minimum 
//use only one of the above 

// PORT (optional) 
$urlregex .= "(\:[0-9]{2,5})?"; 
// PATH  (optional) 
$urlregex .= "(\/([a-z0-9+\$_-]\.?)+)*\/?"; 
// GET Query (optional) 
$urlregex .= "(\?[a-z+&\$_.-][a-z0-9;:@/&%=+\$_.-]*)?"; 
// ANCHOR (optional) 
$urlregex .= "(#[a-z_.-][a-z0-9+\$_.-]*)?\$"; 

// check 
if (eregi($urlregex, $url)) {echo "good";} else {echo "bad";}  

source: http://www.phpcentral.com/208-url-validation-php.html

Link to comment
Share on other sites

Hi,

 

OK, I found a regex function that someone wrote that looks pretty good.  I'm trying to adapt it to my code, and I'm getting an error message that says "Parse error: syntax error, unexpected ';'" on the second-to-last line in the code below.  I think my last if/else statement is not working correctly or something.

 

Can anyone see what I'm doing wrong?

 

Thanks

 


<?php

$site1 = 'http://' . $site;

// Checks if string is a URL
// @param string $url
// @return bool
function isURL($url = NULL) {
        if($url==NULL) return false;

        $protocol = '(http://|https://)';
        $allowed = '([a-z0-9]([-a-z0-9]*[a-z0-9]+)?)';

        $regex = "^". $protocol . // must include the protocol
                         '(' . $allowed . '{1,63}\.)+'. // 1 or several sub domains with a max of 63 chars
                         '[a-z]' . '{2,6}'; // followed by a TLD
        if(eregi($regex, $url)==true) return true;
        else return false;
}


if(isURL($site1)==true)
mysql_query("INSERT INTO `$find` VALUES (NULL, '$site',1,0)";
else
echo "Not a valid URL.";
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.