Link to home
Start Free TrialLog in
Avatar of KraLord
KraLord

asked on

IP and User's Host

Hello there,
Why i am getting this in just one line?
I want it in 3 different lines

----code-----
<?php
$ip = $_SERVER['REMOTE_ADDR'];
$fullhost = gethostbyaddr($ip);
$host = preg_replace("/^[^.]+./", "*.", $fullhost);
echo $ip;
echo $fullhost;
echo $host;
?>
----end-----
ASKER CERTIFIED SOLUTION
Avatar of lozloz
lozloz

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of KraLord
KraLord

ASKER

I think this code here should work fine

<?php
$ip = $_SERVER['REMOTE_ADDR'];
$fullhost = gethostbyaddr($ip);
$host = preg_replace("/^[^.]+./", "*.", $fullhost);
 echo $ip . "<br>";
echo $fullhost . "<br>";
echo $host . "<br>";
?>

=)
yep that's the same thing pretty much, without the formatting for the source code and swapping <br> for the xml compliant line breaks i put in

cheers,

loz
actually that won't work fine ... it depends on your users ... there are users in private networks etc ... in those cases you'll come up with the proxy in that soluction.

As you should be aware lots of users have a private IP and they contact the proxy and the request comes with the proxy's IP ... therefore two or more users in the same private network come out as the same user in that soluction above ...

You should use this that takes in account the proxy problem:

<?
  if ($_SERVER["HTTP_X_FORWARDED_FOR"] != ""){
       $ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
       $proxy = $_SERVER["REMOTE_ADDR"];
       $fullhost = @gethostbyaddr($_SERVER["HTTP_X_FORWARDED_FOR"]);
   }else{
       $ip = $_SERVER["REMOTE_ADDR"];
       $fullhost = @gethostbyaddr($_SERVER["REMOTE_ADDR"]);
   }

$host = preg_replace("/^[^.]+./", "*.", $fullhost);
echo $ip . "<br />\n";
echo $fullhost . "<br />\n";
echo $host . "<br />\n";
?>


just my two cents! :)

Hope it helps...
you do understand that your code ain't gonna work well with all the users that go to you site right? or are you ok with that?
Avatar of KraLord

ASKER

yes Drift3r thanks a lot!