views:

57

answers:

3

I'm using PHP to write a using socket library application. How could I get the IP of the client? THanks for any help

A: 

$_SERVER['REMOTE_ADDR']; ?

Elliott
I tried, but it's useless.
coolkid
Did you check it against $_SERVER["HTTP_X_FORWARDED_FOR"] ?
Elliott
Those variables are not available if he's using ordinary sockets.
nos
+2  A: 

Use socket_getpeername

nos
A: 
<?php
if ($_SERVER['HTTP_CLIENT_IP'])
    $visitorIP = $_SERVER['HTTP_CLIENT_IP'];
elseif ($_SERVER['HTTP_X_FORWARDED'])
    $visitorIP = $_SERVER['HTTP_X_FORWARDED'];
elseif ($_SERVER['HTTP_X_FORWARDED_FOR'])
    $visitorIP = $_SERVER['HTTP_X_FORWARDED_FOR'];
else
    $visitorIP = $_SERVER['REMOTE_ADDR'];

?>

For more/discussion - getting visitor's real IP address

Satya Prakash
man your article is terrible. this variable can NOT hold the private LAN IP address. While other variables can contain NASDAQ quotes. And all these variables has nothing to do with this question.
Col. Shrapnel