Jump to content


Photo

Problem capturing and displaying IP Address


  • Please log in to reply
21 replies to this topic

#1 yana

yana
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts
  • LocationToronto, Canada

Posted 11 July 2006 - 01:24 PM

I am using the following two methods now to capture above mentioned info:

$ip_address = $_SERVER['REMOTE_ADDR'];

$user_browser = $_SERVER['HTTP_USER_AGENT'];

This user info is captured and sent in an email to me and works perfectly on our Test Server. However, on the Live Server this does not get captured (I get an email with a blank).

What could be the cause of this?
<html>
<body>
<?

$ip_address = $_SERVER['REMOTE_ADDR'];

$user_browser = $_SERVER['HTTP_USER_AGENT'];

//add From: header 
$headers = "From: \"Online Survey\" webserver@localhost\r\n";

//specify MIME version 1.0 
$headers .= "MIME-Version: 1.0\r\n"; 

//unique boundary 
$boundary = uniqid("HTMLDEMO"); 

//tell e-mail client this e-mail contains//alternate versions 
$headers .= "Content-Type: multipart/alternative" . 
   "; boundary = $boundary\r\n\r\n"; 

//message to people with clients who don't understand MIME 
$headers .= "This is a MIME encoded message.\r\n\r\n"; 

//HTML version of message 
$headers .= "--$boundary\r\n" . 
   "Content-Type: text/html; charset=ISO-8859-1\r\n" . 
   "Content-Transfer-Encoding: base64\r\n\r\n"; 
$headers .= chunk_split(base64_encode("IP Address: $ip_address Browser: $user_browser"))

//send message 
mail("yana@greatgulfhomes.com,yanochka_@hotmail.com", "Online Survey", "", $headers); 

?> 
</body>
</html> 


Yana

#2 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 11 July 2006 - 01:33 PM

Did you get the blank email all the time, or sometime?
Life's too short for arguing.

#3 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 11 July 2006 - 01:37 PM

Both those variable are unreliable because they are sent by the client.

#4 yana

yana
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts
  • LocationToronto, Canada

Posted 11 July 2006 - 01:38 PM

On the live server no values are returned for IP address and has been the case all the time. On the test server it works fine.

My manager just gave me a tip that it might have to do with different versions of PHP that we have installed on Test and Live Servers.

And told me to use
$ip_address = $HTTP_SERVER_VARS['REMOTE_ADDR'];
instead of
$ip_address = $_SERVER['REMOTE_ADDR'];

However, it still does not work on the Live Server........  ::)
Yana

#5 yana

yana
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts
  • LocationToronto, Canada

Posted 11 July 2006 - 01:39 PM

So what is a good method to use to capture the IP address then???????? If the ones I am using are not reliable...............  :o
Yana

#6 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 11 July 2006 - 01:41 PM

Ip addresses are unreliable. What do you want them for?

#7 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 11 July 2006 - 01:42 PM

user using proxy may not get detected right.

try this, i'm not sure it's work but try anyway.

if (getenv('HTTP_X_FORWARDED_FOR')) {
       $ip_address = getenv('HTTP_X_FORWARDED_FOR');
   } else {
       $ip_address = getenv('REMOTE_ADDR');
   }
Life's too short for arguing.

#8 yana

yana
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts
  • LocationToronto, Canada

Posted 11 July 2006 - 01:49 PM

My boss wants to capture the IP address and I just do what I am told ... unfortunately...
Yana

#9 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 July 2006 - 01:53 PM

This is the code I use to do a similar function. It works about 90% of the time:
<?php
    if ($_SERVER["HTTP_X_FORWARDED_FOR"] != "")
	{
        $IP = $_SERVER["HTTP_X_FORWARDED_FOR"];
        $proxy = $_SERVER["REMOTE_ADDR"];
        $host = @gethostbyaddr($_SERVER["HTTP_X_FORWARDED_FOR"]);
    }
	else
	{
        $IP = $_SERVER["REMOTE_ADDR"];
        $host = @gethostbyaddr($_SERVER["REMOTE_ADDR"]);
    }
?>

I believe I got this code from a different forum about 4 or 5 years ago...

Ken

#10 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 11 July 2006 - 01:54 PM

The method hvle posted is probably the best your going to get. Just remember though that some clients will not show up or will have fake ip. Also be aware that just because your getting hits from a certain ip doesn't mean it is the same client. I have 8 computers in my house, all of which share 1 ip.

#11 yana

yana
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts
  • LocationToronto, Canada

Posted 11 July 2006 - 01:55 PM

Wow thanks!  :-*

This code works for me on the Test and Live server!!!!!!!!!!

if (getenv('HTTP_X_FORWARDED_FOR')) {
        $ip_address = getenv('HTTP_X_FORWARDED_FOR');
    } else {
        $ip_address = getenv('REMOTE_ADDR');
    }

Could anyone explain to me what is the difference between this code and the one I used?????????
Yana

#12 yana

yana
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts
  • LocationToronto, Canada

Posted 11 July 2006 - 01:57 PM

The method hvle posted is probably the best your going to get. Just remember though that some clients will not show up or will have fake ip. Also be aware that just because your getting hits from a certain ip doesn't mean it is the same client. I have 8 computers in my house, all of which share 1 ip.


I am not really concerned how useful this IP address will be! My task was to create an online survey...which was supposed to be anonymous. So anonymous means to me that nothing should be captured......at all! :)
Yana

#13 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 11 July 2006 - 01:58 PM

Are you using different versions of PHP?

#14 yana

yana
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts
  • LocationToronto, Canada

Posted 11 July 2006 - 02:01 PM

Yeah apparently the Live Server has an older version of PHP (3 or so) and the Test Server uses the newer version 4...

Now that this is working for me, I wonder how useful this is at all???????

The test server returns one IP address for me (10.243.102.136) while the Live Server returns a different IP address (216.191.52.74)...
What is the reason for that???????????
Yana

#15 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 11 July 2006 - 02:05 PM

The test server returns one IP address for me (10.243.102.136) while the Live Server returns a different IP address (216.191.52.74)...

Do you have a dynamic IP. I've read that at some ISPs the IP change at every request.

Yeah apparently the Live Server has an older version of PHP (3 or so) and the Test Server uses the newer version 4...

If it's your server I would upgrade PHP to version 4 (or 5 if you wish). The reason why you need to use $HTTP_SERVER_VARS instead of $_SERVER is that $_SERVER was first implemented in PHP4.

#16 trq

trq
  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 11 July 2006 - 02:07 PM

10.243.102.136 appears to be your internal NIC address, while the other is your public IP.

#17 yana

yana
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts
  • LocationToronto, Canada

Posted 11 July 2006 - 02:08 PM

The test server returns one IP address for me (10.243.102.136) while the Live Server returns a different IP address (216.191.52.74)...

Do you have a dynamic IP. I've read that at some ISPs the IP change at every request.

Yeah apparently the Live Server has an older version of PHP (3 or so) and the Test Server uses the newer version 4...

If it's your server I would upgrade PHP to version 4 (or 5 if you wish). The reason why you need to use $HTTP_SERVER_VARS instead of $_SERVER is that $_SERVER was first implemented in PHP4.


It's not my server - it's the company's.
Yana

#18 Daniel0

Daniel0
  • Staff Alumni
  • Advanced Member
  • 11,956 posts

Posted 11 July 2006 - 02:10 PM

The test server returns one IP address for me (10.243.102.136) while the Live Server returns a different IP address (216.191.52.74)...

Do you have a dynamic IP. I've read that at some ISPs the IP change at every request.

Yeah apparently the Live Server has an older version of PHP (3 or so) and the Test Server uses the newer version 4...

If it's your server I would upgrade PHP to version 4 (or 5 if you wish). The reason why you need to use $HTTP_SERVER_VARS instead of $_SERVER is that $_SERVER was first implemented in PHP4.


It's not my server - it's the company's.

Tell them to upgrade, it's an old version.

10.243.102.136 appears to be your internal NIC address, while the other is your public IP.

Ah, yes of course, he will see his internal IP address when he runs the script on localhost.

#19 yana

yana
  • Members
  • PipPipPip
  • Advanced Member
  • 45 posts
  • LocationToronto, Canada

Posted 11 July 2006 - 02:11 PM

The test server returns one IP address for me (10.243.102.136) while the Live Server returns a different IP address (216.191.52.74)...

Do you have a dynamic IP. I've read that at some ISPs the IP change at every request.

Yeah apparently the Live Server has an older version of PHP (3 or so) and the Test Server uses the newer version 4...

If it's your server I would upgrade PHP to version 4 (or 5 if you wish). The reason why you need to use $HTTP_SERVER_VARS instead of $_SERVER is that $_SERVER was first implemented in PHP4.


It's not my server - it's the company's.


And neither Live or Test server appears to use a dynamic IP address.....because I've tested it now a few times and every time I get the exact same value for IP Address on Live and Test server.

Well, anyhow. It works now.
Yana

#20 hvle

hvle
  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 11 July 2006 - 02:13 PM

it worked for you cuz you used an old version, and it wasn't understand $_SERVER.

Personally you should modify kensbrn's code because it's more error proof.

replace $_SERVER with getevn() and it should work on your live server.


Life's too short for arguing.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users