943,931 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 27641
  • PHP RSS
Dec 28th, 2006
0

URL Validation

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
fernandodonster is offline Offline
12 posts
since Sep 2006
Dec 28th, 2006
0

Re: URL Validation

php Syntax (Toggle Plain Text)
  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. }
Reputation Points: 13
Solved Threads: 2
Junior Poster
php_daemon is offline Offline
138 posts
since Aug 2006
Dec 29th, 2006
0

Re: URL Validation

Click to Expand / Collapse  Quote originally posted by php_daemon ...
php Syntax (Toggle Plain Text)
  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.
PHP Syntax (Toggle Plain Text)
  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.
Reputation Points: 13
Solved Threads: 4
Posting Whiz
paradox814 is offline Offline
351 posts
since Oct 2004
Dec 29th, 2006
0

Re: URL Validation

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.
Reputation Points: 13
Solved Threads: 2
Junior Poster
php_daemon is offline Offline
138 posts
since Aug 2006
Nov 18th, 2009
0
Re: URL Validation
check this out

PHP Syntax (Toggle Plain Text)
  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; Nov 18th, 2009 at 7:51 am. Reason: previous code is getting failed
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ym_chaitu is offline Offline
18 posts
since Jun 2009
Sep 23rd, 2010
0

Website URL Validation

<?php
$websiteUrl = $_REQUEST['url'];

if (!preg_match("#^http(s)?://[a-z0-9-_.]+\.[a-z]{2,4}#i",$websiteUrl)) {
echo "wrong url";
} else {
echo "ok";
}

?>

Check this one. This is perfect.
Last edited by Somnathpawar; Sep 23rd, 2010 at 7:15 am.
Reputation Points: 15
Solved Threads: 0
Newbie Poster
Somnathpawar is offline Offline
2 posts
since Sep 2010
Oct 7th, 2011
0
Re: URL Validation
Gotta complement, this works like magic. tried it out and it's fabulous.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
prof php is offline Offline
13 posts
since Jan 2011

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: while loop
Next Thread in PHP Forum Timeline: include ranged files in php





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC