PHP函数错误

编程入门 行业动态 更新时间:2024-10-27 04:25:03
本文介绍了PHP函数错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这个小脚本,可以对$hosts_to_ping数组中的IP进行ping操作.在index.html中使用JavaScript调用此PHP.

I have this little script, which should ping the IPs in the $hosts_to_ping array. This PHP is called with JavaScript in the index.html.

但是出了点问题,因为$rval始终为1(这意味着主机不可访问).但是我知道前两个主机还活着.

But something is wrong, because the $rval is always 1 (which mean the host is unreachable). But I know that the first two host are alive.

所以我打印$res变量,然后看到消息:Need to give the IP.我不明白为什么它不能将$host变量替换为函数中的实际IP地址.

So I print the $res variable, and I see the message: Need to give the IP. I don't understand why it doesn't replace the $host variable to the actual IP address in the function.

<?php function ping($host) { exec(sprintf('ping -n 4', escapeshellarg($host)), $res, $rval); print_r($res); return $rval === 0; } $hosts_to_ping = array('10.54.23.254', '10.22.23.254', '10.23.66.134'); ?> <ul> <?php foreach ($hosts_to_ping as $host): ?> <li> <?php echo $host; ?> <?php $up = ping($host); ?> (<img src="<?php echo $up ? 'on' : 'off'; ?>" alt="<?php echo $up ? 'up' : 'down'; ?>">) </li> <?php endforeach; ?> </ul>

推荐答案

在这一行:

exec(sprintf('ping -n 4', escapeshellarg($host)), $res, $rval);

sprintf 不会在字符串中插入escapeshellarg($host),因为您错过了%s .将该行替换为:

sprintf won't interpolate escapeshellarg($host) in the string because you missed the %s. Replace that line with:

exec(sprintf('ping -n 4 %s', escapeshellarg($host)), $res, $rval);

尝试一下,看看是否可行.

Try this and see if it works.

更多推荐

PHP函数错误

本文发布于:2023-10-17 22:11:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1502222.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数   错误   PHP

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!