简体   繁体   English

当 php 页面部署到 Internet 时,php shell_exec() function 不工作

[英]php shell_exec() function not working when the php page is deployed on to the internet

I have created a very basic php page, which contains the following codes:我创建了一个非常基本的 php 页面,其中包含以下代码:

<?php
$out = shell_exec('ipconfig');
echo $out
?>

This code successfully prints my ip-addresses on the web page.此代码在 web 页面上成功打印了我的 IP 地址。 But when this page is made live on the internet using any hosting platforms, it prints nothing on the web page and the value of the variable out( $out ) is also null.但是,当使用任何托管平台在 Internet 上制作此页面时,它不会在 web 页面上打印任何内容,并且变量 out( $out ) 的值也是 null。

How to fix this issue?如何解决这个问题?

It return ip on Shell which will always return null on Server.它在 Shell 上返回 ip,这将始终在服务器上返回 null。

To Get ip on server:要在服务器上获取 ip:

Server IP You can get the server IP address from $_SERVER['SERVER_ADDR'].服务器 IP 您可以从 $_SERVER['SERVER_ADDR'] 获取服务器 IP 地址。

Client IP address客户端IP地址

You can get the client IP from $_SERVER['REMOTE_ADDR']您可以从 $_SERVER['REMOTE_ADDR'] 获取客户端 IP


$ipAddress=$_SERVER['REMOTE_ADDR'];
$macAddr=false;

#run the external command, break output into lines
$arp=`arp -a $ipAddress`;
$lines=explode("\n", $arp);

#look for the output line describing our IP address
foreach($lines as $line)
{
   $cols=preg_split('/\s+/', trim($line));
   if ($cols[0]==$ipAddress)
   {
       $macAddr=$cols[1];
   }
}

if you just want to dump it's result straight to the user you can cut to the chase and use passthru():如果您只想将结果直接转储给用户,您可以切入正题并使用 passthru():

$cmd = shell_exec('ipconfig');
echo '<pre>';
passthru($cmd);
echo '</pre>';

If you want to display the output at run time as the program goes, you can do this:如果你想在程序运行时显示 output,你可以这样做:


while (@ ob_end_flush()); // end all output buffers if any

$proc = popen($cmd, 'r');
echo '<pre>';
while (!feof($proc))
{
    echo fread($proc, 4096);
    @ flush();
}
echo '</pre>';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM