Results 1 to 7 of 7

Thread: PHP REGEX to Validate Domain Name

  1. #1
    Join Date
    Jul 2009
    Posts
    127

    PHP REGEX to Validate Domain Name

    I would like to validate that a URL is valid. It does not have to be iron-clad but it should restrict big faults.

    For example.. These should validate:

    example.com
    domain.extension
    www.webaddress.ab.cd.ef
    my-domain.name
    domain123.name

    This should not validate:
    email@example.com
    web address com
    my_domain.name
    example.123

    How can this be performed withing the PHP programming environment ?

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: PHP REGEX to Validate Domain Name

    If you are developing yourself in web programming environment then you need to look for every chnages that are getting committed every day and every moments. Some new chnages and terms are being applied every time.

    But some of the rules never change, in order to this discussion :What about trying to have your script connect to the domain with fopen() if in case, it returns an error then it's mostly switched in the invalid zone. These are some of the rules that is needed to consider in such situation..

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Validating an URL

    Validating an URL is a job that appears so frequent. You may have a form where visitors (users) have to enter a valid URL. You might desired to check that a referrer that you will echo on a page is valid to restrict injection of wrong code and linking to faulty sites.

    While PHP facilitates with the parse_url function to parse a URL and return its components it still lacks some activities that will come in handy when validating a URL.

    Properties that my is_url function gives:

    1. Lets you mention which components are needed
    2. Reports which components are missing
    3. Cleans up the parts to comply with RFC2396 and return them as an array
    4. Returns true if entire needed components are present

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    How to use is_url function:

    For the impatient between you hereĀ“s a whole example first. It checks whether the HTTP_REFERER is valid and converts it into an truly URI before using it on our page.

    Code:
    $cleaned = array(); 
    $error = 0; 
    if (is_url($_SERVER['HTTP_REFERER'], PATH, $error, $cleaned)) { 
     if ($error & (SCHEME + AUTHORITY)) { 
      $_SERVER['HTTP_REFERER'] = make_abs($_SERVER['HTTP_REFERER'], 
    $_SERVER['SCRIPT_URI']); 
     } elseif ($cleaned['authority']!= $_SERVER['SERVER_NAME']) { 
      echo "Referer is from other domain. We do not include it."; 
     } else { 
      $_SERVER['HTTP_REFERER'] = make_uri($cleaned); 
      echo "<br>$_SERVER[HTTP_REFERER]"; 
     } 
    } else { 
     echo "errors: $error<br>"; 
    }

  5. #5
    Join Date
    May 2008
    Posts
    2,297

    PHP URL Validation Script

    I am going to show you a script which validates the format of a URL text string. It might be used to test the URL submitted from a form. The format of the URL is assumed to be of the form: domain-name.co.in for example. There are mainly two scripts. One would be used to validate a URL and the other one perform the domain name validation. They make use of regular expression pattern matching.

    <?php

    $url = 'teSTgjhgj.co.in';
    if (preg_match ("/^[a-z0-9][a-z0-9\-]+[a-z0-9](\.[a-z]{2,4})+$/i", $url)) {
    print "$url url OK.";
    } else {
    print "$url url invalid!";
    }
    ?>

    <?php
    // For domain validation
    $domain = 'teSTgjhgj';
    if (preg_match ("/^[a-z0-9][a-z0-9\-]+[a-z0-9]$/i", $domain)) {
    print "$domain Domain OK.";
    } else {
    print "$domain Domain invalid!";
    }
    ?>

  6. #6
    Join Date
    May 2008
    Posts
    2,389

    Re: PHP REGEX to Validate Domain Name

    Hey buddy, you also can use this to do what you need to perform and I will suggest you this function to validating URL.

    Code:
          function checkLink ($url)
          {
              if(!@fopen($url, "r"))
                 return false;
              else
                 return true;  
          }
           
         
          checkLink("http://php.net");
          //returns true

  7. #7
    Join Date
    Feb 2008
    Posts
    1,852

    Re: PHP REGEX to Validate Domain Name

    I don't think, the validation is very easy task and can be done by every common visitor. This is done by the use having proper privilege to edit and manage all this.A domain possible to be validated properly through the DNS-lookups and not through a fopen($domain). You function will fail for some domains that are valid but are not prefixed with www . Please consider about my words ...

    Thanks you so much ...

Similar Threads

  1. Regex to validate a date
    By Logan 2 in forum Software Development
    Replies: 5
    Last Post: 13-02-2010, 12:41 AM
  2. How to Validate Email by using PHP?
    By NGV BalaKrishna in forum Software Development
    Replies: 5
    Last Post: 07-02-2010, 02:43 AM
  3. How to Validate XML Against a DTD?
    By PsYcHo 1 in forum Software Development
    Replies: 5
    Last Post: 02-02-2010, 12:33 AM
  4. How to validate windows XP
    By austin26 in forum Operating Systems
    Replies: 3
    Last Post: 18-11-2009, 05:47 PM
  5. Replies: 1
    Last Post: 16-09-2009, 10:11 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,885,535.61499 seconds with 17 queries