gpt4 book ai didi

Php 套接字、Apache 和 CentOS 6.3 给出权限被拒绝

转载 作者:可可西里 更新时间:2023-10-31 23:52:58 25 4
gpt4 key购买 nike

我想做的是运行一个简单的 PHP 脚本来检查游戏服务器是否在线并从中获取一些信息。我在带有 WAMP 服务器的本地机器上运行完全相同的脚本,我只是取消注释 php_openssl.dll 和 php_sockets.dll 并且 - 瞧 - 它按预期工作。

但是接下来是我们的生产环境!我通常使用 Debian,但我们的主机决定在我们的专用服务器上安装 CentOS,因为 NIC 在 Debian 中出现故障,从那以后就一直很麻烦。

我克服了一些问题,但仍面临这个问题:如何修复 PHP 套接字?我读到我需要 php-common,所以我安装了它:

# yum install php-common

然后我检查了 phpinfo(),我得到了这个

'./configure'  '--with-openssl' '--enable-sockets' ...

因此,如果您问我,这看起来不错,openssl 和套接字都已安装并且应该可以正常工作,但事实并非如此。我在 Stack Overflow 上找到了这个脚本:

<?php
//just in case
if (!extension_loaded('sockets')) {
die('The sockets extension is not loaded.');
}
echo '<p><strong>Establishing connection...</strong></p>';
$socket = socket_create(AF_INET,SOCK_STREAM,0);
if (!socket_connect($socket, "stackoverflow.com", 80))
{
die('Socket error : '.socket_strerror(socket_last_error()));
}

echo '<p><strong>Connection successful!</strong></p>';

$request = join("\n",array(
"GET / HTTP/1.1",
"Connection: close",
"Host: stackoverflow.com",
"User-Agent: Mozilla/5.0 (Windows NT 6.1)",
"Accept: text/html,*/*;q=0.8",
""));
socket_write($socket,$request,strlen($request));

$response = socket_read($socket,2048);


echo "<p><strong>This is the received data : </strong></p>";
echo '<pre>'.htmlentities($response).'</pre>';

socket_close($socket);

这个输出完全让我吃惊:

Establishing connection...

Connection successful!

This is the received data :

HTTP/1.0 408 Request Time-out
Cache-Control: no-cache
Connection: close
Content-Type: text/html

<html><body><h1>408 Request Time-out</h1>
Your browser didn't send a complete request in time.
</body></html>

但是,我的脚本仍然无法运行? :(我已经禁用了 iptables

# service iptables stop

只是为了确定这不是防火墙问题。当我打开页面而不是游戏服务器名称时,会发生以下情况:

Warning: socket_connect(): unable to connect [13]: Permission denied in /var/www/test.php on line 9 <<<

这是我正在使用的代码,它是 xPaw 的开源代码,所以我不相信它:

<?php
function queryserver( $IP, $Port = 25565, $Timeout = 2 )
{
$Socket = Socket_Create( AF_INET, SOCK_STREAM, SOL_TCP );
Socket_Set_Option( $Socket, SOL_SOCKET, SO_SNDTIMEO, array( 'sec' => (int)$Timeout, 'usec' => 0 ) );
Socket_Set_Option( $Socket, SOL_SOCKET, SO_RCVTIMEO, array( 'sec' => (int)$Timeout, 'usec' => 0 ) );
if( $Socket === FALSE || Socket_Connect( $Socket, $IP, (int)$Port ) === FALSE )
{
return FALSE;
}
Socket_Send( $Socket, "\xFE\x01", 2, 0 );
$Len = @Socket_Recv( $Socket, $Data, 256, 0 );
Socket_Close( $Socket );
if( $Len < 4 || $Data[ 0 ] !== "\xFF" )
{
return FALSE;
}
$Data = SubStr( $Data, 3 );
$Data = iconv( 'UTF-16BE', 'UTF-8', $Data );

if( $Data[ 1 ] === "\xA7" && $Data[ 2 ] === "\x31" )
{
$Data = Explode( "\x00", $Data );
return Array(
'HostName' => $Data[ 3 ],
'Players' => IntVal( $Data[ 4 ] ),
'MaxPlayers' => IntVal( $Data[ 5 ] ),
'Protocol' => IntVal( $Data[ 1 ] ),
'Version' => $Data[ 2 ]
);
}
$Data = Explode( "\xA7", $Data );
return Array(
'HostName' => SubStr( $Data[ 0 ], 0, -1 ),
'Players' => isset( $Data[ 1 ] ) ? IntVal( $Data[ 1 ] ) : 0,
'MaxPlayers' => isset( $Data[ 2 ] ) ? IntVal( $Data[ 2 ] ) : 0,
'Protocol' => 0,
'Version' => '1.3'
);
}

$test = queryserver('SERVERADDRESSHERE'); // (i tried multiple, which are all working @wamp)
$echo = $test['HostName'] . "<<< <br />";
echo $echo;

最佳答案

在你的 linuxbox 中尝试这个命令(作为 root)。

setsebool -P httpd_can_network_connect 1

关于Php 套接字、Apache 和 CentOS 6.3 给出权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14798907/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com