-2

I have a database where i have stored all IP address. Now i want to know these IP address is connected/normal or disconnected. I have tried:

$add     = "example.com";
$result = checkdnsrr($add, "MX"); 
var_dump($result);

Its return boolean true or false. But i have IP address which is not connected dns. But how can i know the IP is active/normal/connected or disconnected?

Chonchol Mahmud
  • 2,717
  • 7
  • 39
  • 72
  • checkdnsrr will get domain information where available. This is not the same as a user being connected. To detect this you would need to implement some sort of a background list where IP addresses are stored against a date/last visited. It could also be archived with the use of js – atoms Dec 27 '18 at 09:30
  • @atoms so how can i achieve my goal? Can you give me any idea in PHP or JS? – Chonchol Mahmud Dec 27 '18 at 09:35
  • not really. I've pointed you in the right direction. Abdul has given some good information too. Think about having a list in memory. Each user has a record in the list. Just maintain the list's data and you will have what you want – atoms Dec 27 '18 at 09:38

2 Answers2

1

LONG POLLING is BAD

As far as I have understood your question, you just want to check if the particular client is connected or not.

You will have to setup a cron job in PHP with a continuous loop which will be long polled by XHR (AJAX with Jquery etc) setting a status = true. So, when a user disconnects, the XHR will be broken and a status will be set = false. Thus you can check if a user is connected or not. However, please note that Long Polling is really resource intensive and is not appreciated.

I would highly suggest you to use go with Node and Websockets etc.

I could write a code for PHP Cron job and settle your problem but I don't appreciate Long Polling + Cron Job for it.

Airy
  • 5,484
  • 7
  • 53
  • 78
  • Please could you write up PHP cron Job or give me some useful links for node and websockets. Appreciated. – Chonchol Mahmud Dec 27 '18 at 09:37
  • @ChoncholMahmud https://itnext.io/building-a-node-js-websocket-chat-app-with-socket-io-and-react-473a0686d1e1 for more, you can Google for `Node + Websockets` and it will bring a list of tons of websites teaching it. – Airy Dec 27 '18 at 09:42
  • 1
    @ChoncholMahmud for more robust system, use Socket.io with Node. As this will cover a wide area of old browsers as well. Off Course, if you are newbie to Node, you will have to learn it but trust me it's really easy and worth learning it. – Airy Dec 27 '18 at 09:46
  • Thank you very much for you good suggestion. Yes, I am newbie in Node. That's why i want solve this problem with PHP. – Chonchol Mahmud Dec 27 '18 at 09:48
0

if you want to check if the website is alive then you can do it like this:

$add     = "example.com";
$result = false;
if($fp = fsockopen($add, 80, $errno, $errstr, 10)){
    fclose($fp);
    $result = true;
}
var_dump($result);

// edit: so you want to know if the IP is present in your local network ? if so you can use ping like:

function ping($host, $timeout = 1) {
            /* ICMP ping packet with a pre-calculated checksum */
            $package = "\x08\x00\x7d\x4b\x00\x00\x00\x00PingHost";
            $socket  = socket_create(AF_INET, SOCK_RAW, 1);
            socket_set_option($socket, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $timeout, 'usec' => 0));
            socket_connect($socket, $host, null);

            $ts = microtime(true);
            socket_send($socket, $package, strLen($package), 0);
            if (socket_read($socket, 255))
                    $result = microtime(true) - $ts;
            else    $result = false;
            socket_close($socket);

            return $result;
}

$present = ping('192.168.0.100');
Kazz
  • 1,030
  • 8
  • 16