SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Enthusiast
    Join Date
    Jan 2007
    Posts
    25
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    checking URL existence

    hi,
    i need to check the exsistence of any URL, how do i do that?
    i tried using fopen, but that is not working
    here's what i did
    Code:
    <?php
    $fp=fopen("http://www.google.com","r");
    if($fp==null)
     echo "invalid";
    else
     echo "url exists";
    ?>
    any suggestions? thanks a lot!

  2. #2
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    use file() instead and it should work.
    PHP Code:
    <?php

    $target_url 
    'http://www.sitepoint.com';

    if(@
    file($target_url) == false) { // @ surpresses the php error
        
    echo 'not available';
    } else {
        echo 
    'Its there';
    }
    ?>
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  3. #3
    SitePoint Member
    Join Date
    Sep 2006
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Will it prevent errors that will be displayed if the file does not exist!

  4. #4
    dooby dooby doo silver trophybronze trophy
    spikeZ's Avatar
    Join Date
    Aug 2004
    Location
    Manchester UK
    Posts
    13,788
    Mentioned
    151 Post(s)
    Tagged
    3 Thread(s)
    yes, the @ sign surprsses the php error message so you can use your own option.

    without it you would get:
    Warning: file(): php_network_getaddresses: gethostbyname failed in C:\apache2triad\htdocs\sp_file.php on line 5 Warning: file(http://www.sitepointsss.com): failed to open stream: No error in C:\apache2triad\htdocs\sp_file.php on line 5 not available
    Mike Swiffin - Community Team Advisor
    Only a woman can read between the lines of a one word answer.....

  5. #5
    SitePoint Wizard
    Join Date
    Jan 2001
    Location
    Grand Rapids, MI
    Posts
    1,284
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I believe you can also do this with cURL, which if I remember correctly is a number of times faster.


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
  •