URL Validation

Reply

Join Date: Sep 2006
Posts: 12
Reputation: fernandodonster is an unknown quantity at this point 
Solved Threads: 0
fernandodonster fernandodonster is offline Offline
Newbie Poster

URL Validation

 
0
  #1
Dec 28th, 2006
How do we validate URL either with PHP/javascript? could u help me?for example,
whether input text is in the format http://www.google.com or not?
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 138
Reputation: php_daemon is an unknown quantity at this point 
Solved Threads: 2
php_daemon php_daemon is offline Offline
Junior Poster

Re: URL Validation

 
0
  #2
Dec 28th, 2006
  1. if (!preg_match("#^http://www\.[a-z0-9-_.]+\.[a-z]{2,4}$#i",$url)) {
  2. echo "wrong url";
  3. } else {
  4. echo "ok";
  5. }
Reply With Quote Quick reply to this message  
Join Date: Oct 2004
Posts: 348
Reputation: paradox814 is an unknown quantity at this point 
Solved Threads: 4
paradox814's Avatar
paradox814 paradox814 is offline Offline
Posting Whiz

Re: URL Validation

 
0
  #3
Dec 29th, 2006
Originally Posted by php_daemon View Post
  1. if (!preg_match("#^http://www\.[a-z0-9-_.]+\.[a-z]{2,4}$#i",$url)) {
  2. echo "wrong url";
  3. } else {
  4. echo "ok";
  5. }
that looks good, except that only letters, numbers, and hypens are allowed in a domain name (underscores are not allowed), and due to ICANN limitation domain names are limited to 64 characters. This regular expression also limits a lot of cases, for instance none of the following special cases would work...

any website with a subdomain/or lack of one other than www, for instance
http://slashdot.org/ (has no www and no subdomain)
http://en.wikipedia.org/ (has a subdomain en, but no www)

subfolders
http://www.mozilla.com/en-US/firefox/

ip addresses...
http://64.233.167.99/
you will actually get google.com (that is its ip address as of this current moment).

specifying port numbers:
http://glcfapp.umiacs.umd.edu:8080

entering username/passwords directly into the url
http://username: password@example.com

i recommend going here: http://regexlib.com/Search.aspx
and performing a search for category "Uri" and see which regular expression you thing would work best for this example. here is a sample i got from that site, of course this one is not perfect either, but it's another sample.
  1. (http|ftp|https):\/\/[\w\-_]+(\.[\w\-_]+)+([\w\-\.,@?^=%&:/~\+#]*[\w\-\@?^=%&/~\+#])?

just know that it will probably be virtually impossible to find a perfect regex expression to match all possible URL's, one way would be to see if it starts with http:// and if it does, then see if you can make a connection to it with php, if the headers return a status code of 200, then you are good and you know it's a valid url, but that is a lot of work in itself.
Last edited by paradox814; Dec 29th, 2006 at 1:00 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2006
Posts: 138
Reputation: php_daemon is an unknown quantity at this point 
Solved Threads: 2
php_daemon php_daemon is offline Offline
Junior Poster

Re: URL Validation

 
0
  #4
Dec 29th, 2006
Yes, indeed. Though if the OP needs to check for the urls of specific format only, such as the one provided as an example, a simple regexp would do quite well.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 18
Reputation: ym_chaitu is an unknown quantity at this point 
Solved Threads: 0
ym_chaitu ym_chaitu is offline Offline
Newbie Poster
 
0
  #5
21 Days Ago
check this out

  1. <?php
  2. $subscr=$_REQUEST['subscr'];
  3.  
  4. //url validation
  5.  
  6. if (preg_match('|^http(s)?://[a-z0-9-]+(.[a-z0-9-]+)*(:[0-9]+)?(/.*)?$|i', $subscr))
  7. {
  8. print "$subscr url OK.";
  9. } else {
  10. print "$subscr url not valid!";
  11. }
  12. //echo("$subscr");
  13. ?>
Last edited by ym_chaitu; 21 Days Ago at 7:51 am. Reason: previous code is getting failed
Reply With Quote Quick reply to this message  
Reply

Message:



Similar Threads
Other Threads in the PHP Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC