0

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??

Javier
  • 99
  • 1
  • 9

3 Answers3

1

$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.

Community
  • 1
  • 1
Celada
  • 21,627
  • 4
  • 64
  • 78
  • Is the observation with respect to the solution of Laxus. Seek information about your solution, because by the time getpeername do not know. if I can lend a help regarding getpeername thank you – Javier Jul 13 '12 at 12:40
  • I have this: `socket_getsockname ($socket, $IPX, $PORT);` but $ IPX is Null, that I have wrong? – Javier Jul 13 '12 at 13:40
  • I correct is: `socket_getpeername ($socket, $IPX, $port); ` but do not get the remote ip. – Javier Jul 13 '12 at 14:09
  • When I run this command: `socket_getpeername ($socket, $IPX, $port);` I get this error: `Warning: socket_getpeername () Expects parameter 1 to be resource` – Javier Jul 15 '12 at 16:26
  • `socket_geetpeername` only works for actual sockets. xinetd binds the TCP socket to PHP's stdin/stdout, so `socket_getpeername` will simply error. – Xkeeper May 21 '14 at 17:36
1

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

Javier
  • 99
  • 1
  • 9
1

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

Leiyi.China
  • 157
  • 1
  • 4