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); Link to comment https://forums.phpfreaks.com/topic/161891-solved-checking-to-see-if-a-variable-is-a-valid-url/ 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. Link to comment https://forums.phpfreaks.com/topic/161891-solved-checking-to-see-if-a-variable-is-a-valid-url/#findComment-854140 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 Link to comment https://forums.phpfreaks.com/topic/161891-solved-checking-to-see-if-a-variable-is-a-valid-url/#findComment-854141 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"? Link to comment https://forums.phpfreaks.com/topic/161891-solved-checking-to-see-if-a-variable-is-a-valid-url/#findComment-854173 Share on other sites More sharing options...
haku Posted June 12, 2009 Share Posted June 12, 2009 $site1 = 'http://' . $site; Link to comment https://forums.phpfreaks.com/topic/161891-solved-checking-to-see-if-a-variable-is-a-valid-url/#findComment-854202 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."; ?> Link to comment https://forums.phpfreaks.com/topic/161891-solved-checking-to-see-if-a-variable-is-a-valid-url/#findComment-854680 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 Link to comment https://forums.phpfreaks.com/topic/161891-solved-checking-to-see-if-a-variable-is-a-valid-url/#findComment-854686 Share on other sites More sharing options...
ArizonaJohn Posted June 12, 2009 Author Share Posted June 12, 2009 Thanks, man. Link to comment https://forums.phpfreaks.com/topic/161891-solved-checking-to-see-if-a-variable-is-a-valid-url/#findComment-854690 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.