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 DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post

    Test if internet connection exists

    Hey everyone,

    Well here I sit typing on a LAMP server that I use for a little bit of dev.

    Some of my apps need the internet to communicate with other apps that I have written online.

    The problem is that my internet comes and goes as it pleases.

    Is there a way to quickly check if there is an internet connection, perhaps with a function that returns true or false?

    I was looking at pinging with PHP but that looks WAAAAAY to complex for my uses.

    Thanks a lot!
    -DR.Wong

    Wheres the food at?

  • #2
    New Coder
    Join Date
    Mar 2009
    Location
    Chicago, IL
    Posts
    69
    Thanks
    0
    Thanked 15 Times in 15 Posts
    It's not that hard to ping domain, add this function in your script or one of the include files:

    PHP Code:
    function ping_domain($domain)
    {
        
    $file = @fsockopen ($domain80$errno$errstr10);
        
        return (!
    $file) ? FALSE TRUE;

    Then to ping domain use

    PHP Code:
    $domain 'google.com';

    if (
    ping_domain($domain))
    {
        echo 
    "{$domain} is online."
    }
    else
    {
        echo 
    "{$domain} is offline."

    Last edited by steelaz; 03-22-2009 at 05:51 PM.

  • Users who have thanked steelaz for this post:

    DR.Wong (03-22-2009)

  • #3
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    Not sure you maybe could use file_get_contents("http://www.example.com");
    that would return false if it cant connect to it for some reason. There's probably a better way around it tbh.

    You could use
    PHP Code:
    <?PHP
    $str
    =exec("ping -c 1 -w 1 192.168.1.216",$a,$a1);
    if(
    strlen($str)>1){
         
    //present 
        
      
    }else{
         
    //not present   

      
    }
    ?>
    Not sure if this works i nicked it off of exec(). And this is a windows PC so can't test it atm.
    You can not say you know how to do something, until you can teach it to someone else.

  • #4
    Regular Coder DR.Wong's Avatar
    Join Date
    Jan 2005
    Posts
    360
    Thanks
    23
    Thanked 1 Time in 1 Post
    Thanks steelaz, that works like a charm.

    Tim, that code seems to say offline all the time. I didn't change the IP in the example you gave so that's probably why. Thanks anyway!
    -DR.Wong

    Wheres the food at?

  • #5
    Senior Coder timgolding's Avatar
    Join Date
    Aug 2006
    Location
    Southampton
    Posts
    1,519
    Thanks
    114
    Thanked 110 Times in 109 Posts
    lol it's probably for the best. steelaz method is far superior.
    You can not say you know how to do something, until you can teach it to someone else.


  •  

    Posting Permissions

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