PHP如何在没有system()或exec()的情况下ping服务器 [英] PHP how to ping a server without system() or exec()

查看:278
本文介绍了PHP如何在没有system()或exec()的情况下ping服务器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试ping服务器,但我的主机已禁用 exec() system()因为安全原因。
有没有其他选择可以让它工作或我必须要求我的主机启用它们?

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?

我得到的错误:


警告:出于安全原因已禁用system()

警告:exec()已被禁用安全原因

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


推荐答案

你可以使用 PHP套接字函数

一个函数从PHP网站上的笔记中ping:

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.

这篇关于PHP如何在没有system()或exec()的情况下ping服务器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆