PHP套接字错误Linux上的权限被拒绝

时间:2013-06-11 06:18:25

标签: php linux sockets

我在Linux 2.6.34上安装了php 5.4.13。

我使用套接字创建简单的客户端/服务器页面,但它无法正常工作。

提供权限被拒绝错误

以下是我的PHP代码

if (false == ($socket = socket_create(AF_INET, SOCK_STREAM, 0)))  // create socket 
{
   $stringData= date("D M d, Y g:i A"). " socket_create() failed: reason: " . socket_strerror(socket_last_error()) . "<br>";
   echo $stringData;
}
else
{
    $timeout = array('sec'=>5,'usec'=>500000);
    socket_set_option($socket,SOL_SOCKET,SO_RCVTIMEO,$timeout);

    if(false==($result = socket_connect($socket, $host, $port)))
    {
        $stringData= date("D M d, Y g:i A"). " socket_connect() failed: reason: " . socket_strerror(socket_last_error()) . "<br>";
        echo $stringData;
        socket_close($socket);
    }
    else
    {
        $stringData= date("D M d, Y g:i A"). " Socket connected succefully <br>";
        echo $stringData;

        if(false==(socket_write($socket, $command, strlen($command))))
        {
            $stringData= date("D M d, Y g:i A"). " socket_write() failed: reason: " . socket_strerror(socket_last_error()) . "<br>";
            echo $stringData;
            socket_close($socket);
        }
        else
        {
            if(false===($cmd = socket_read ($socket, 65536)))
            {
                //10060 for windows and 11 for linux

              if(10060!=socket_last_error() && 11!=socket_last_error())
              {
                $stringData= date("D M d, Y g:i A"). " socket_read() failed: reason: " . socket_strerror(socket_last_error()) . "<br>";
                echo $stringData;
                socket_close($socket);

              }
              switch(socket_select($r = array($socket), $w = array($socket), $f = array($socket), 5))
              {
                       case 2:
                               $refused=1;  
                               break;
              }
              if($refused==1)
              {
                $stringData= date("D M d, Y g:i A"). " socket_read() failed: reason: Connection Refused <br>";
                $ourFileHandle = fopen(SOCKET_LOG, 'a');
                echo $stringData;
                socket_close($socket);

              }
            }
            else
            {
                echo "<pre>".html_entity_decode(print_r($cmd,true))."</pre>";
            }
        }
    }
}

上面的代码在命令提示符下工作正常,但是当从任何浏览器打开页面时,它会给出错误Permission denied。

从终端运行php的命令:/usr/local/rootfs/php5/bin/php /www/socket_client.php

2 个答案:

答案 0 :(得分:12)

检查 php.ini 设置中的参数“ safe_mode ”。它应该是“关闭”。

另一个问题可能是由于“selinux”阻止你的mod_php(apache进程)通过socket连接。 在这种情况下:

# check if it's enabled:
/usr/sbin/sestatus -v
to add the connecting rule:
setsebool httpd_can_network_connect=1

如果要完全禁用它:

setenforce 0

此外,出于调试原因,请禁用apache / PHP上的任何安全模块。例如,如果正在运行,请尝试禁用Suhosin

答案 1 :(得分:0)

我遇到了类似的问题:

   <?php
   fsockopen('udp://192.168.1.255', 7, $errno, $errstr, 2);
   ?>

警告: fsockopen():无法连接到第 1 行 /var/www/html/udp.php 中的 udp://192.168.1.255:7(权限被拒绝)

致命错误:未捕获的异常:无法打开 UDP 套接字:权限被拒绝

php.ini 如果服务器让你改变这个配置:

allow_url_fopen = On      // Was On
allow_url_include = Off   // On Didn't change the problem

终端检查防火墙:

ufw status
Status: Inaktiv

/usr/sbin/sestatus -v
SELinux status:        disabled

解决方案:

不允许广播到192.168.1.255,我把它改成对应的IP地址,即192.168.1.24

默认情况下,袜子的广播是禁用的。 socket_set_option 命令启用广播消息。

PHP 官方网站上的以下脚本:

<?php
# Wake on LAN - (c) HotKey (at SPR dot AT), upgraded by Murzik <tomurzik@inbox.ru>

flush();

function WakeOnLan($addr, $mac)
{
$addr_byte = explode(':', $mac);
$hw_addr = '';

for ($a=0; $a < 6; $a++) $hw_addr .= chr(hexdec($addr_byte[$a]));

$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);

for ($a = 1; $a <= 16; $a++) $msg .= $hw_addr;

// send it to the broadcast address using UDP
// SQL_BROADCAST option isn't help!!
$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
if ($s == false)
{
echo "Error creating socket!\n";
echo "Error code is '".socket_last_error($s)."' - " . socket_strerror(socket_last_error($s));
}
else
{
// setting a broadcast option to socket:
$opt_ret = socket_set_option($s, 1, 6, TRUE);
if($opt_ret < 0)
{
echo "setsockopt() failed, error: " . strerror($opt_ret) . "\n";
}
$e = socket_sendto($s, $msg, strlen($msg), 0, $addr, 2050);
echo $e;
socket_close($s);
echo "Magic Packet sent (".$e.") to ".$addr.", MAC=".$mac;
}
}

#WakeOnLan('yourIPorDomain.dyndns.org', 'your:MAC:address');
#WakeOnLan('192.168.0.2', '00:30:84:2A:90:42');
#WakeOnLan('192.168.1.2', '00:05:1C:10:04:05');

//if you have switch or other routing devices in LAN, sendign to
// the local IP isn't helps! you need send to the broadcast address like this:
WakeOnLan('192.168.0.255', 'xx:xx:xx:xx:xx:xx'); 

某人

附加信息:

https://www.php.net/manual/de/function.fsockopen.php

fsockopen() enabling it in PHP.ini

https://serverfault.com/questions/371000/how-to-enable-fsockopen-function-in-php

https://ubuntuforums.org/showthread.php?t=919407

https://forum.qnap.com/viewtopic.php?t=39154