ip address of client [message #173236] |
Tue, 29 March 2011 05:46 |
suresh
Messages: 5 Registered: March 2011
Karma: 0
|
Junior Member |
|
|
Hi
I was using the following script to get ip address of client, but I
get Welcom ::1 from the following script. How to get the real ip
address?
<?php
function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share
internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is
pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
$ip = getRealIpAddr();
echo "Welcome "; echo $ip;
thanks
suresh
|
|
|
|
Re: ip address of client [message #173238 is a reply to message #173236] |
Tue, 29 March 2011 06:51 |
gordonb.oas0f
Messages: 1 Registered: March 2011
Karma: 0
|
Junior Member |
|
|
> I was using the following script to get ip address of client, but I
> get Welcom ::1 from the following script. How to get the real ip
> address?
If someone is using a proxy on their local machine (perhaps to speed
up network access), that may *BE* the real IP (v6) address (localhost)
as defined by your function. If more than one of
$_SERVER['HTTP_CLIENT_IP']
$_SERVER['HTTP_X_FORWARDED_FOR']
and $_SERVER['REMOTE_ADDR']
are defined, it is not always obvious which one is "real". The
first two can be IP addresses in someone's local network that are
not reachable from the outside. The first two can also be faked.
I recommend logging all three.
> <?php
> function getRealIpAddr()
> {
> if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share
> internet
> {
> $ip=$_SERVER['HTTP_CLIENT_IP'];
> }
> elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is
> pass from proxy
> {
> $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
> }
> else
> {
> $ip=$_SERVER['REMOTE_ADDR'];
> }
> return $ip;
> }
> $ip = getRealIpAddr();
> echo "Welcome "; echo $ip;
|
|
|
Re: ip address of client [message #173241 is a reply to message #173237] |
Tue, 29 March 2011 10:47 |
Beauregard T. Shagnas
Messages: 154 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
richard the sto0pid wrote:
> On Mon, 28 Mar 2011 22:46:48 -0700 (PDT), suresh wrote:
>
>> Hi
>> I was using the following script to get ip address of client, but I
>> get Welcom ::1 from the following script. How to get the real ip
>> address?
>>
>> <?php
>> [snip code to only what RtS is replying to]
>> echo "Welcome "; echo $ip;
>
> echo "welcome " $ip; <--- wrong
> echo "welcome ".$ip; <--- will work, as is concatenated
>
> only one echo per physical line.
Incorrect and not necessary. His line: echo "Welcome "; echo $ip;
is perfectly legitimate, having the proper terminator ";" after the
first echo. A newline is not required between commands.
--
-bts
-In a broadband world, you are just a dialup
|
|
|
|
Re: ip address of client [message #173248 is a reply to message #173247] |
Tue, 29 March 2011 21:12 |
Michael Fesser
Messages: 215 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
.oO(richard)
> On Tue, 29 Mar 2011 06:47:45 -0400, Beauregard T. Shagnasty wrote:
>
>> Incorrect and not necessary. His line: echo "Welcome "; echo $ip;
>> is perfectly legitimate, having the proper terminator ";" after the
>> first echo. A newline is not required between commands.
>
> while it may be correct, it serves no purpose.
> Proper use of string manipulation will not confuse the server.
This is not about string manipulation at all, but simply about code
formatting preferences.
Micha
|
|
|
Re: ip address of client [message #173250 is a reply to message #173247] |
Tue, 29 March 2011 23:42 |
Beauregard T. Shagnas
Messages: 154 Registered: September 2010
Karma: 0
|
Senior Member |
|
|
richard the sto0pid wrote:
> Beauregard T. Shagnasty wrote:
>> richard the sto0pid wrote:
>>> suresh wrote:
>>>> I was using the following script to get ip address of client, but I
>>>> get Welcom ::1 from the following script. How to get the real ip
>>>> address?
>>>>
>>>> <?php
>>>> [snip code to only what RtS is replying to]
>>>> echo "Welcome "; echo $ip;
>>>
>>> echo "welcome " $ip; <--- wrong
>>> echo "welcome ".$ip; <--- will work, as is concatenated
>>>
>>> only one echo per physical line.
>>
>> Incorrect and not necessary. His line: echo "Welcome "; echo $ip; is
>> perfectly legitimate, having the proper terminator ";" after the
>> first echo. A newline is not required between commands.
>
> while it may be correct, it serves no purpose.
> Proper use of string manipulation will not confuse the server.
What kind of a backpedaling answer is that? Did you not understand my
response?
--
-bts
-In a broadband world, you are just a dialup
|
|
|