ArizonaJohn Posted June 11, 2009 Share Posted June 11, 2009 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); Quote Link to comment Share on other sites More sharing options...
J.Daniels Posted June 11, 2009 Share Posted June 11, 2009 URLs can be checked with a regex expression. Google 'php regex url verification' for code examples. Quote Link to comment Share on other sites More sharing options...
ted_chou12 Posted June 11, 2009 Share Posted June 11, 2009 this smells like a regex question // 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 Quote Link to comment Share on other sites More sharing options...
ArizonaJohn Posted June 12, 2009 Author Share Posted June 12, 2009 OK, thanks. I have a really simple question: how do I make a new variable $site1 that equals "http://$site"? Quote Link to comment Share on other sites More sharing options...
haku Posted June 12, 2009 Share Posted June 12, 2009 $site1 = 'http://' . $site; Quote Link to comment Share on other sites More sharing options...
ArizonaJohn Posted June 12, 2009 Author Share Posted June 12, 2009 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."; ?> Quote Link to comment Share on other sites More sharing options...
darkfreaks Posted June 12, 2009 Share Posted June 12, 2009 you are missing a close paren ) after the query Quote Link to comment Share on other sites More sharing options...
ArizonaJohn Posted June 12, 2009 Author Share Posted June 12, 2009 Thanks, man. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.