Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 17

Thread: Test a domain

  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts

    Test a domain

    What's the best way (in PHP) to test if a given domain name is valid? By valid I mean that it connects to a website. Is there a PHP function for this?
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #2
    Regular Coder
    Join Date
    Feb 2005
    Posts
    663
    Thanks
    5
    Thanked 14 Times in 14 Posts
    I think the best you can do is verify if its a valid web address/link with .xxx extension. Or maybe be able to limit to strictly index.xxx pages. You could use preg_match for it.

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    To check the life of a site (and limit you're handling), I'd use curl in conjunction with a head request. Almost no data to handle (so little work to do) and will tell you if the site is actually responding. Good for checking uptime status. This is how you'd test verification.

    If you just need to check the validity of a domain, simple pattern matching is all you need.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    New Coder
    Join Date
    Dec 2008
    Posts
    10
    Thanks
    0
    Thanked 1 Time in 1 Post

    Validate Domain Name Using PHP

    Hi rfresh,

    This can be easily done using PHP regular expression. There are a couple of regular expressions, ereg functions or POSIX & preg functions (perl compatible) to make this script run perfect.

    --Dave

  • #5
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    @Fou-Lu

    This is what I'm looking for (and I'm not trying to write an uptime checker) - what would be in the headers?
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Been awhile.
    It should return some server information (apache address for example), the modification date, lessee... connection type, maybe some cookies, and other defined stuff.
    I'm trying to remember what curl returns if there is an error that occures, I think its just false, but it may still populate the results as an empty array. That would take some playing around.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #7
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    Here is a simple URL test. Obviously this domain does not exist yet when I run this script it returns True - huh?

    PHP Code:
    $ch curl_init();
    curl_setopt($chCURLOPT_URL"http://sfhsfhshfjsdhf.com");
    curl_setopt($chCURLOPT_POST0);
    curl_setopt($chCURLOPT_RETURNTRANSFER1);
    curl_setopt($chCURLOPT_TIMEOUT0);
    $status curl_exec($ch);
    curl_close($ch);
    if (
    $status)
       {
         print 
    "true";
       }
    else
       {
         print 
    "false";
       } 
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #8
    Regular Coder kokjj87's Avatar
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    279
    Thanks
    1
    Thanked 55 Times in 54 Posts
    I think the one below would be faster, as you are just getting the header only.

    you can add some regular expression for checking before connecting to the site.
    The test will only pass if it is a 200, 301, 302(live, ok, and no error).

    If there is a 404Error(File not found) or something, the function will return it as a invalid site.

    PHP Code:
    function isValidUrl($url)
    {
        
    //connect to the website and get the header information
        
    $header = @get_headers($url);
        
    //if header status is 200, 301 or 302, the website is live and valid
        
    return preg_match('/^HTTP(.*)(200|301|302)(.*)$/i'$header[0], $status);

    Last edited by kokjj87; 12-11-2008 at 09:20 AM.

  • Users who have thanked kokjj87 for this post:

    uncleroxk (12-11-2008)

  • #9
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,994
    Thanks
    4
    Thanked 2,662 Times in 2,631 Posts
    Quote Originally Posted by kokjj87 View Post
    I think the one below would be faster, as you are just getting the header only.

    you can add some regular expression for checking before connecting to the site.
    The test will only pass if it is a 200, 301, 302(live, ok, and no error).

    If there is a 404Error(File not found) or something, the function will return it as a invalid site.

    PHP Code:
    function isValidUrl($url)
    {
        
    //connect to the website and get the header information
        
    $header = @get_headers($url);
        
    //if header status is 200, 301 or 302, the website is live and valid
        
    return preg_match('/^HTTP(.*)(200|301|302)(.*)$/i'$header[0], $status);

    get_headers is a better idea. I completely forgot that they added these in PHP5. Save yourself some work and make use of this one (unless you've got PHP4 of course).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • Users who have thanked Fou-Lu for this post:

    rfresh (12-12-2008)

  • #10
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    No matter what URL I use, the code below always returns 0

    PHP Code:
    function isValidUrl($url)
    {
        
    //connect to the website and get the header information
        
    $header = @get_headers($url);
        
    //if header status is 200, 301 or 302, the website is live and valid
        
    return preg_match('/^HTTP(.*)(200|301|302)(.*)$/i'$header[0], $status);
    }  
    print 
    isValidUrl("sfsfsdfsdfsdf5.com"); 
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #11
    Regular Coder kokjj87's Avatar
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    279
    Thanks
    1
    Thanked 55 Times in 54 Posts
    PHP Code:
    function isValidUrl($url)
    {
      
    //if no protocol define, use http as default.
       
    if(!preg_match('/(((http(s?))|(ftp))\:\/\/)/i'$url))
       {
        
    $url 'http://'.$url;
       }
        
    //connect to the website and get the header information
        
    $header = @get_headers($url);
        
    //if header status is 200, 301 or 302, the website is live and valid
        
    return preg_match('/^HTTP(.*)(200|301|302)(.*)$/i'$header[0], $status);
    }  
    print 
    isValidUrl("google.com"); 
    That is because you didn't pass in the PROTOCOL..
    anyway i have added a function inside which will check whether did you supply th protocol or not, if not it would be treated as a http.

  • Users who have thanked kokjj87 for this post:

    rfresh (12-12-2008)

  • #12
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    Both of these still return the same results: 1

    print isValidUrl("google.com");
    print isValidUrl("googleslkfjslkjfkslfjks.com");
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #13
    Regular Coder kokjj87's Avatar
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    279
    Thanks
    1
    Thanked 55 Times in 54 Posts
    It is working on my computer..

    print isValidUrl("google.com"); //return 1
    print isValidUrl("googleslkfjslkjfkslfjks.com"); //return 0

  • #14
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts
    Well, your results is what I'm after but I can't get the same results as you. I cleared my IE7 cache and tried it using Firefox...both return 1 for me - I'm doomed...
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #15
    Regular Coder kokjj87's Avatar
    Join Date
    Sep 2008
    Location
    Singapore
    Posts
    279
    Thanks
    1
    Thanked 55 Times in 54 Posts
    can you run this 2 line on your computer and post the result here?..

    PHP Code:
    print_r(get_headers("http://google.com"));
    echo 
    "<br/><br/><br/><br/><br/><br/><br/><br/><br/>";
    print_r(get_headers("http://googleslkfjslkjfkslfjks.com")); 


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •