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.
Results 1 to 5 of 5
  1. #1
    Regular Coder
    Join Date
    Mar 2004
    Location
    Australia
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post

    Internet Connectivity

    Hey all. Just wondering if anyone knew of any inbuilt php boolean type functions that could check for inet connectivity. I tried something that i thought i could use for a shortcut but didnt exactly work

    PHP Code:
    function checkFile($pathToFile)
    {
        if(
    is_file($pathToFile))
        {
            return 
    true;
        }

            return 
    false;
    }

    $chkPath checkFile("http://www.google.com/index.html");

        if (
    $chkPath
        { 
            echo 
    "true"
        } 
        else 
        { 
            echo 
    "false"
        } 
    thought that if it could check the url for the index.html then there had to be some sort of connection. doesnt look like it works that easy though. I also tried using the is_link but there wasnt much information regarding it that i could find.

    any help would be great

    Edit:
    Will be using this script locally

  • #2
    Senior Coder
    Join Date
    Aug 2003
    Location
    One step ahead of you.
    Posts
    2,815
    Thanks
    0
    Thanked 3 Times in 3 Posts
    The function chkFile does exaxtly what is_file() does... and it returns bool so the last if/esle is pointless too.
    I'm not sure if this was any help, but I hope it didn't make you stupider.

    Experience is something you get just after you really need it.
    PHP Installation Guide Feedback welcome.

  • #3
    Regular Coder
    Join Date
    Mar 2004
    Location
    Australia
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post
    Ok i never actually looked into the is_file function... kinda posted before work and was kinda pseudo. anyway i say down and have looked at it and came up with teh following

    PHP Code:
    $chkPath "http://www.google.com/index.html";
    echo 
    "<strong>Internet: </strong>";
        if(
    is_file($chkPath))
        {
            echo 
    "connected";
        }
        else
        {
            echo 
    "disconnected";
        } 
    doesnt seem to be working so far, i keep getting "disconnected". however if i do
    PHP Code:
    $chkPath "http://www.google.com/index.html";
    echo 
    "<strong>Internet: </strong>";
        if(
    readfile($chkPath))
        {
            echo 
    "connected";
        }
        else
        {
            echo 
    "disconnected";
        } 
    it returns true, hence "connected" but it displays the url itself. so i will have a google page were i dont exactly want it. is there a way around displaying the actual contents?

  • #4
    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
    readfile() will return a result to your output buffer.
    You can eliminate this and retain it as a string:
    PHP Code:

    ob_start
    ();
    readfile($chkpath);
    $checkpath ob_get_contents();
    ob_end_clean(); 
    Now, this method isn't really nessessary. Instead of using readfile() use file_get_contents() instead, which can be loaded as a variable, and not directly outputted to the browser:
    PHP Code:

    if (!$file_read file_get_contents($chkpath))
    {
         echo 
    'disconnected';

    for instance.

    Edit:
    I'd thought I'd mention this as well, I don't know quite what it is your doing with it, but if your running via apache, there are several proprietary functions available for you to use which can look up information on a page. apache_lookup_uri for instance will return several values about the url requested -> including the returned 'status'. This can be used to your advantage as well, and I would assume it to probably be faster than using a filesystem command. I'd need to put that to the test though....
    Last edited by Fou-Lu; 06-30-2005 at 06:16 PM.

  • #5
    Regular Coder
    Join Date
    Mar 2004
    Location
    Australia
    Posts
    217
    Thanks
    0
    Thanked 1 Time in 1 Post
    Hey. Thanks for your reply Fou-Lu that helped me out a bit. I have been trying to get pear installed for the past while so i could start using some of the inbuilt functionality. Finally found a bundle that has it working

    so now i have
    PHP Code:
    <?php
    echo "<strong>Internet: </strong>";
    $chkPath "http://www.google.com";

    if (@!
    $file_read file_get_contents($chkPath))
    {
         echo 
    'disconnected';
    }  
    else
    {
         echo 
    'connected';
    }
    ?>
    originally displayed a nasty error if the url wasnt active so i had to escape that. only thing to work on now is a quicker way of doing it...

    thanks

    Edit:
    i looked into the apache_lookup_uri that you mentioned but dont think it could help me in any way. below you see the array that it produces. I dont get any varying information for valid/invalid url's that are passed in... well that i can see anyway

    Code:
    stdClass Object
    (
        [status] => 200
        [the_request] => GET /apache_uri.php HTTP/1.1
        [method] => GET
        [mtime] => 0
        [clength] => 0
        [chunked] => 0
        [no_cache] => 0
        [no_local_copy] => 1
        [unparsed_uri] => /www.google.com
        [uri] => /www.google.com
        [filename] => W:/www/www.google.com
        [path_info] => 
        [allowed] => 0
        [sent_bodyct] => 0
        [bytes_sent] => 0
        [request_time] => 1120196681
    )
    Last edited by Serex; 07-01-2005 at 07:48 AM.


  •  

    Posting Permissions

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