the url dose not exists?

Discussion in 'PHP' started by free-designer, Feb 26, 2010.

  1. #1
    hello,

    i want anything to check to see if this domain exists for example.

    www.google.com
    PHP:
    this is good url but this one

    www.hjifodszhgfiodshgfo.com
    PHP:
    ops there are none site like that

    so is there any way to validate this with php and echo a message back

    thanks in advance
     
    free-designer, Feb 26, 2010 IP
  2. n3r0x

    n3r0x Well-Known Member

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #2
    
    function is_valid_dns($dns) {
    if(gethostbyname($dns) == $dns)
      return false;
    return true;
    }
    
    PHP:
    haven´t tested it but it should work...

    More information on Gethostbyname
     
    n3r0x, Feb 26, 2010 IP
  3. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    no it didn't worked, this is not what im looking for.

    i just want something to tell whether if the url is right and have content or wrong

    the if condition is not working, if you test book.com it's going to say false and actually book.com exists :)

    thanks for help
     
    free-designer, Feb 26, 2010 IP
  4. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #4
    
    if(file('http://www.book.com')):
    
    	echo 'EXISTS';
    
    else:
    
    	echo 'DOESN\'T EXIST';
    
    endif;
    
    PHP:
    that could be a control..
     
    mehmetm, Feb 26, 2010 IP
  5. n3r0x

    n3r0x Well-Known Member

    Messages:
    257
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    120
    #5
    then there´s something wrong with your server since that works on mine using


    
    <?php
    
    function is_valid_dns($dns) {
    if(gethostbyname($dns) == $dns)
      return false;
    return true;
    }
    
    if(is_valid_dns("book.com")) {
     print "true";
    } else {
    print "false";
    }
    
    ?>
    
    PHP:
    you can see it at => http://www.qsdchublist.com/gethost.php

    :p
     
    n3r0x, Feb 26, 2010 IP
  6. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    it works but the problem that when the domain is false it get me a php error like that

    
    Warning: file() [function.file]: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\mysite\index.php on line 23
    
    Warning: file(http://www.bhfdshfdshfdsagook.com) [function.file]: failed to open stream: php_network_getaddresses: getaddrinfo failed: No such host is known. in C:\xampp\htdocs\mysite\index.php on line 23
    DOESN'T EXIST
    
    HTML:
    so do you got any idea to only make it show DOESN'T EXIST
     
    Last edited: Feb 26, 2010
    free-designer, Feb 26, 2010 IP
  7. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    hmm, not working well it only get me true even if the domain is
    www.boogbdsfgsk.com
    HTML:
     
    free-designer, Feb 26, 2010 IP
  8. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #8
    see the output of this php function, and you can use the data to see if website exists or not:

    
    echo '<pre>'; print_r(get_headers('http://www.book.com')); exit;
    
    Code (markup):
    The reason other functions are returning an error is because the domain book.com is a 301 redirect to another url. You can see this info using this function and your script can decide if it actually exists or not.
    Thanks :)
     
    JEET, Feb 26, 2010 IP
  9. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #9
    yep

    
    if(@file('http://www.xcvaqsse.com')):
    
    	echo 'EXISTS';
    
    else:
    
    	echo 'DOESN\'T EXIST';
    
    endif;
    
    PHP:
     
    Last edited: Feb 26, 2010
    mehmetm, Feb 26, 2010 IP
  10. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    thank you so mush you are the best ;-)

    thanks for everone that helped me out here
     
    free-designer, Feb 26, 2010 IP
  11. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #11
    yea, i guess you are right, but #mehmetm give me exactly what i need thanks
     
    free-designer, Feb 26, 2010 IP
  12. mehmetm

    mehmetm Well-Known Member

    Messages:
    134
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #12
    your welcome :)
     
    mehmetm, Feb 26, 2010 IP
  13. HostingProvider

    HostingProvider Active Member

    Messages:
    1,480
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    95
    #13
    This is one of the most resource-consuming alternatives that I have ever seen to do what the OP asks for...

    My suggestion:
    http://php.net/manual/en/function.curl-getinfo.php
    (I think there's a way to use it without executing curl_exec before)
     
    HostingProvider, Feb 26, 2010 IP
  14. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14

    i don't have that big of knowlade with php, but i think the code above works pretty well, and i tested it a lot of times and it gives me exactly what i need.

    so if you do have a way to use your code and gives me the same result, tell me how can i use it, cuz on the page you gave me on php.net none people talking about it.
     
    free-designer, Feb 27, 2010 IP
  15. HostingProvider

    HostingProvider Active Member

    Messages:
    1,480
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    95
    #15
    I can't afford to do your work. I just pointed you in the right direction - "teach them to fish instead of giving them the fish"
     
    HostingProvider, Feb 27, 2010 IP
  16. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #16
    haha, well okay sorry for asking, now i guess im going to fish by my own hand.

    :) thanks for help
     
    free-designer, Feb 27, 2010 IP
  17. danx10

    danx10 Peon

    Messages:
    1,179
    Likes Received:
    44
    Best Answers:
    2
    Trophy Points:
    0
    #17
    <?php
    $headers = get_headers("http://www.google.com", 1);
    if ($headers[0] == "HTTP/1.1 200 OK" || $headers[0] == "HTTP/1.0 200 OK"){
    echo "Exists";
    } else {
    echo "Doesn't exist";
    }
    ?>
    PHP:
     
    danx10, Feb 27, 2010 IP
  18. free-designer

    free-designer Peon

    Messages:
    79
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    this works but it's give me a php error if the url is wrong one try to type

    www.ijngshgdslnhifds.com
    HTML:
    :)

    and you have to make this $headers[0] like that $headers[1]
     
    free-designer, Feb 27, 2010 IP