PHP how to ping a server without system() or exec()

折月煮酒 提交于 2019-12-10 21:55:33

问题


I'm trying to ping a server but my host disabled exec() and system() because of security reasons.
Are there any other options to let it work or do I have to ask my host to enable them?

The error I get:

Warning: system() has been disabled for security reasons
Warning: exec() has been disabled for security reasons


回答1:


You can do that by using the PHP socket functions

A function for pinging from the notes on the PHP website:

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;
    }

But chances are good that if you are not allowed to do system calls the socket functions are also disabled.



来源:https://stackoverflow.com/questions/20467432/php-how-to-ping-a-server-without-system-or-exec

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!