Xinetd obtain remote IP with PHP

2019-08-28 09:31发布

Hello I have this service with xinetd:

service MyService 
{
port = 8881
socket_type = stream
wait = no
user = nobody
server = /usr/local/bin/php
server_args = /home/file/public_html/php/port/test1.php
log_on_success + = USERID
log_on_failure + = USERID
disable = no
}

My File test1.php:

<? php
$handle = fopen ('php :/ / stdin', 'r');
$input = fgets ($ handle);
fclose ($ handle);
$ip = $_SERVER['REMOTE_ADDR']
echo "Hello {$ input} your IP: $ip";
?>

I can not get the remote ip:

$ip = $ _SERVER['REMOTE_ADDR']

As I can get the remote ip??

标签: php tcp xinetd
3条回答
Fickle 薄情
2楼-- · 2019-08-28 09:56

I tested and found that:

Apache httpd passes "REMOTE_ADDR" and "REMOTE_PORT" and much more other environments to the CGI program.

But xinetd passes only "REMOTE_HOST" environment to my program defined by "server" in xinetd.d/xxxx 。and more, "REMOTE_HOST" looks like "::ffff:192.168.1.23" , not a pure IP address.(BTW: what does this mean ?)

So : xinetd + program can only acts a tiny httpd moudle

查看更多
Deceive 欺骗
3楼-- · 2019-08-28 10:07

The solution is modifying the PHP with :

$IpX = $_SERVER['REMOTE_HOST'] ? $_SERVER['REMOTE_HOST'] : $_SERVER['HOST'];

<? php
$IpX = $_SERVER['REMOTE_HOST'] ? $_SERVER['REMOTE_HOST'] : $_SERVER['HOST'];
$handle = fopen ('php :/ / stdin', 'r');
$input = fgets ($ handle);
fclose ($ handle);
echo "Hello {$ input} your IP: $IpX";
?>

Thanks to: Gonzalo Ayuzo

查看更多
别忘想泡老子
4楼-- · 2019-08-28 10:08

$REMOTE_ADDR is an environment variable that is set by a web server as part of the CGI protocol. You're running straight on a socket. There is no web server here. It is not surprising that this variable (or indeed the ones suggested by Laxus in a comment) aren't set.

If you want the remote address, you will have to get it yourself using getpeername.

查看更多
登录 后发表回答