#1
  1. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0

    Question Validating Domains In Different Ways ?


    Folks,

    When a user inputs his domain name (just the domain name. Eg. devshed.com) into my form field, I want the form to check if the domain is valid.

    1. I don't care if the inserted domain is registered or not. Nor care if it is functional or not. Aslong as it is a valid domain then that should be accepted.

    2. This time, I need to make sure the domain is registered and functional.


    I am spoilt for choice as too many code samples presented here:

    https://stackoverflow.com/questions/...in-name-in-php

    Which code sample from the above link would you use to satisfy my question number 1 and why particularly that one and not the others ?
    And, which one would you use to satisfy my question number 2 and why particularly that one over the others ?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0
    No ideas, anyone ?
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Location
    Lithuania
    Posts
    48
    Rep Power
    47
    If you need to check just if a domain is valid, you can use regular expressions, as per your link with example.

    If you need to check if a domain is registered, use dns_get_record() function. It will return an array with DNS records if domain is OK, or false on error.

    Comments on this post

    • UniqueIdeaMan agrees
    Do you license and update your PHP scripts?
  6. #4
  7. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,472
    Rep Power
    2105
    For validation (of a whole lot of stuff including URL's and email addresses) you can't go past PHP's built in filter_var() function. Use that and it will make your life a whole lot easier.
  8. #5
  9. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Location
    Lithuania
    Posts
    48
    Rep Power
    47
    Yes, except that using built-in validation will not work in this case.

    FILTER_VALIDATE_URL will validate URL
    Code:
    http://localhost
    (which is NOT a domain), and will fail (which is a valid and working domain).

    Comments on this post

    • UniqueIdeaMan agrees
    Do you license and update your PHP scripts?
  10. #6
  11. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,472
    Rep Power
    2105
    Originally Posted by phpmillion
    Yes, except that using built-in validation will not work in this case.
    Except that it does...
    PHP Code:
    $url 'http://localhost';
    $is_valid filter_var ($urlFILTER_VALIDATE_URL);
    echo 
    '<p>Validation result for "'.$url.'" is "'.$is_valid.'"</p>';

    $url 'http://not a url';
    $is_valid filter_var ($urlFILTER_VALIDATE_URL);
    echo 
    '<p>Validation result for "'.$url.'" is "'.$is_valid.'"</p>'
    Run that. For 301 Moved Permanently the result is the proper filtered URL, which is correct. For the second invalid URL, it returns false.

    So sorry, but the filter_var() option works for your example too, and is still the best option.
  12. #7
  13. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Location
    Lithuania
    Posts
    48
    Rep Power
    47
    Hm, did you actually read my post and the original OP's request?
    Do you license and update your PHP scripts?
  14. #8
  15. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,472
    Rep Power
    2105
    Originally Posted by phpmillion
    Hm, did you actually read my post and the original OP's request?
    Yes I did. I also read where you said:

    Originally Posted by phpmillion
    If you need to check just if a domain is valid, you can use regular expressions, as per your link with example.
    Regex's are overkill and almost impossible to get completely accurate for this case. Emails are a similar situation too. You're much better off using the built-in PHP functions rather then trying to roll your own in this case.
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Location
    Lithuania
    Posts
    48
    Rep Power
    47
    So you say, that if I try to validate domain voveraitė.lt (which is valid and works), built-in PHP validation will succeed?

    And then, if someone enters 301 Moved Permanently as his "domain", built-in PHP validation will mark it as invalid domain (as author expects)?
    Do you license and update your PHP scripts?
  18. #10
  19. Code Monkey V. 0.9
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Mar 2005
    Location
    A Land Down Under
    Posts
    2,472
    Rep Power
    2105
    Originally Posted by phpmillion
    So you say, that if I try to validate domain voveraitė.lt (which is valid and works), built-in PHP validation will succeed?
    That will depend on your system and how you save/transfer the text. If your system handles the encoding correctly for those special characters, then as far as the documentation goes, it should be valid.

    Originally Posted by phpmillion
    And then, if someone enters 301 Moved Permanently as his "domain", built-in PHP validation will mark it as invalid domain (as author expects)?
    No it won't, because that's a valid URL and a valid hostname, so why should it be flagged as invalid? (and you're the one that's mentioned that as invalid, not the OP)

    I'd be interested in seeing what regex you can provide that would handle all of these conditions. I'm sure it's possible, but I'm also sure that it would be hard to develop correctly, and would be almost impossible to understand for anyone that's not a regex guru. It would also be a whole lot more processing then the built-in PHP functions.

    While I do understand your points, there's always other ways to do things, and in this case it's also a whole lot easier and will give the same results. If you need to filter out specific things (like localhost) then it would be better using separate filters for those so you can see exactly why they are being rejected, instead of one big catch-all "Sorry, that's wrong" message with no other explanation.
  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2017
    Location
    Lithuania
    Posts
    48
    Rep Power
    47
    Just for interest - would you mind showing a filter_var based code that validates domain voveraitė.lt? You claim If your system handles the encoding correctly for those special characters, then as far as the documentation goes, it should be valid, but PHP documentation says - internationalized domain names (containing non-ASCII characters) will fail

    No it won't, because that's a valid URL and a valid hostname, so why should it be flagged as invalid?
    Unfortunately, there's a small problem in this. OP stated he needs to validate domain, not URL or hostname, and he even provided an example - When a user inputs his domain name (just the domain name. Eg. devshed.com) into my form field, I want the form to check if the domain is valid

    In other words, if URL/hostname is valid, it doesn't mean it's a valid domain. I'm pretty sure it's the main reason why this filter is named FILTER_VALIDATE_URL instead of FILTER_VALIDATE_DOMAIN. Hence, while filter_var is surely the easiest way to validate URLs, I don't think it will work for OP's needs this time.
    Do you license and update your PHP scripts?
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0
    @phpmillion & catacaustic,

    I missed this thread for nearly a month and surprised to find you 2 kept it alive. Thank you both.
    Looking at both of you two's arguments, I realize phpmillion understood my intentions when I opened this thread. Saying all this, if a php built-in function can do the job then I'll give that credit over regex as I hate regex. I'll only opt for regex if the php function is not foolproof.
    Anyway, I'm willing to learn both of you two's methods of doing things. Best if you both provide code samples how you'd code it to solve this issue. That way, I and the newbies to this thread learn both tactics from 2 seniors. I hope I'm not asking for too much.

    Ok, I've got your answer phpmillion to use dns_get_record() function if I want to check if a domain is functional. No need to provide any code sample for this one unless you want to.
    I got Catacaustic code sample at post 6:
    Validating Domains In Different Ways ?

    Now, I'd appreciate a regex code sample from you phpmillion (if that is not too much to ask) to check if the domain is valid or not regardless of whether the url is valid or not excluding the domain. And then, I guess I can say my learning on this issue is complete. Can close thread then.
    Thread will be handy to all future php newcomers.

    @everyone, now and then I mention Kicken (this forum) and Mlukac89 (from sitepoint.com forum) and Benanamen (this forum and codingforums.com) and Tango Force (codingforums.com) contributed some code samples for my learning.
    But, I forgot, phpmillion also contributed code sample once or twice on some forum. Good thing he is prowling this forum because it jogged my memory.
    I remember now, I was adding my own code (link click logging) onto Mini Proxy and it was not logging "http" or "https" and phpmillion told me where to add what code sample and it did the job! I owe him my "web app->Android app" copy.
    I now owe Catacaustic too for his code sample contribution on post 6.
    These people would get a copy of my app for sure since their code samples taught me how to build the app features:
    Mlukac89
    Kicken
    PhpMillion
    Catacaustic

    These people might get a copy. They did contribute code samples to my php learning but not sure if what I learnt from them got added onto the app or not. Have to check:
    Benanamen
    Tango Force
    Requinix (at another forum)

    I think some at other forums contributed code samples too for my learning purpose. Will have to check these forums one day by retracing my footsteps.
    Just mentioning all this so you guys understand, I don't forget someone who contributes code samples for my php learning.
    And, once you start seeing how my app helps bankrupts, unemployed, house wives, single parents, bankrupt, homeless, etc. then you'd appreciate that you were part of my learning that helped so many classes of people out of poverty. I'm not kidding.
    Thanks for reading everyone.

    Cheers folks!
    Last edited by UniqueIdeaMan; March 11th, 2018 at 11:49 AM.
  24. #13
  25. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0
    Originally Posted by phpmillion
    If you need to check just if a domain is valid, you can use regular expressions, as per your link with example.

    If you need to check if a domain is registered, use dns_get_record() function. It will return an array with DNS records if domain is OK, or false on error.
    I can only see, unless I'm falling asleep (nearly 5am here) or would need glasses, there is only 1 regex sample from the op and he mentions it's not working.
    Which one would you use yourself out of all of them ?
    https://stackoverflow.com/questions/...in-name-in-php

    Same question goes to others reading this.

    Mmm.
    Last edited by UniqueIdeaMan; March 26th, 2018 at 06:59 AM.
  26. #14
  27. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0
    Originally Posted by Catacaustic
    Except that it does...
    PHP Code:
    $url 'http://localhost';
    $is_valid filter_var ($urlFILTER_VALIDATE_URL);
    echo 
    '<p>Validation result for "'.$url.'" is "'.$is_valid.'"</p>';

    $url 'http://not a url';
    $is_valid filter_var ($urlFILTER_VALIDATE_URL);
    echo 
    '<p>Validation result for "'.$url.'" is "'.$is_valid.'"</p>'
    Run that. For 301 Moved Permanently the result is the proper filtered URL, which is correct. For the second invalid URL, it returns false.

    So sorry, but the filter_var() option works for your example too, and is still the best option.
    Yeah, I just tested it. I get:

    Validation result for "http://localhost" is "http://localhost"

    Validation result for "http://not a url" is ""



    Even though this thread is about domain validation, your url validation has become a bonus for me. Thanks. Made a note of it. Will become handy whilst building my searchengine crawler. Another incomplete project that is still on hold.
    Now, I need a domain filter and so do you mind replying to my post just before this one ?
    Last edited by UniqueIdeaMan; March 26th, 2018 at 07:06 AM.
  28. #15
  29. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2017
    Posts
    845
    Rep Power
    0
    I need an answer to my previous post 13.

    Anyone can respond!

    Cheers!
    Last edited by UniqueIdeaMan; April 2nd, 2018 at 05:19 AM.

IMN logo majestic logo threadwatch logo seochat tools logo