Jump to content

Archived

This topic is now archived and is closed to further replies.

yana

Problem capturing and displaying IP Address

Recommended Posts

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

[code]$ip_address = $_SERVER['REMOTE_ADDR'];

$user_browser = $_SERVER['HTTP_USER_AGENT'];[/code]

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?
[code]
<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>
[/code]

Share this post


Link to post
Share on other sites
Did you get the blank email all the time, or sometime?

Share this post


Link to post
Share on other sites
Both those variable are unreliable because they are sent by the client.

Share this post


Link to post
Share on other sites
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 [code]$ip_address = $HTTP_SERVER_VARS['REMOTE_ADDR'];[/code] instead of [code]$ip_address = $_SERVER['REMOTE_ADDR'];[/code]

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

Share this post


Link to post
Share on other sites
So what is a good method to use to capture the IP address then???????? If the ones I am using are not reliable...............  :o

Share this post


Link to post
Share on other sites
Ip addresses are unreliable. What do you want them for?

Share this post


Link to post
Share on other sites
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');
   }

Share this post


Link to post
Share on other sites
My boss wants to capture the IP address and I just do what I am told ... unfortunately...

Share this post


Link to post
Share on other sites
This is the code I use to do a similar function. It works about 90% of the time:
[code]<?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"]);
    }
?>[/code]

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

Ken

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Wow thanks!  :-*

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

[code]if (getenv('HTTP_X_FORWARDED_FOR')) {
       $ip_address = getenv('HTTP_X_FORWARDED_FOR');
   } else {
       $ip_address = getenv('REMOTE_ADDR');
   }[/code]

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

Share this post


Link to post
Share on other sites
[quote author=thorpe link=topic=100191.msg395130#msg395130 date=1152626082]
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.
[/quote]

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! :)

Share this post


Link to post
Share on other sites
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???????????

Share this post


Link to post
Share on other sites
[quote author=yana link=topic=100191.msg395140#msg395140 date=1152626512]
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)...
[/quote]
Do you have a dynamic IP. I've read that at some ISPs the IP change at every request.

[quote author=yana link=topic=100191.msg395140#msg395140 date=1152626512]
Yeah apparently the Live Server has an older version of PHP (3 or so) and the Test Server uses the newer version 4...
[/quote]
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.

Share this post


Link to post
Share on other sites
10.243.102.136 appears to be your internal NIC address, while the other is your public IP.

Share this post


Link to post
Share on other sites
[quote author=Daniel0 link=topic=100191.msg395144#msg395144 date=1152626744]
[quote author=yana link=topic=100191.msg395140#msg395140 date=1152626512]
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)...
[/quote]
Do you have a dynamic IP. I've read that at some ISPs the IP change at every request.

[quote author=yana link=topic=100191.msg395140#msg395140 date=1152626512]
Yeah apparently the Live Server has an older version of PHP (3 or so) and the Test Server uses the newer version 4...
[/quote]
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.
[/quote]

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

Share this post


Link to post
Share on other sites
[quote author=yana link=topic=100191.msg395148#msg395148 date=1152626924]
[quote author=Daniel0 link=topic=100191.msg395144#msg395144 date=1152626744]
[quote author=yana link=topic=100191.msg395140#msg395140 date=1152626512]
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)...
[/quote]
Do you have a dynamic IP. I've read that at some ISPs the IP change at every request.

[quote author=yana link=topic=100191.msg395140#msg395140 date=1152626512]
Yeah apparently the Live Server has an older version of PHP (3 or so) and the Test Server uses the newer version 4...
[/quote]
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.
[/quote]

It's not my server - it's the company's.
[/quote]
Tell them to upgrade, it's an old version.

[quote author=thorpe link=topic=100191.msg395146#msg395146 date=1152626878]
10.243.102.136 appears to be your internal NIC address, while the other is your public IP.
[/quote]
Ah, yes of course, he will see his internal IP address when he runs the script on localhost.

Share this post


Link to post
Share on other sites
[quote author=yana link=topic=100191.msg395148#msg395148 date=1152626924]
[quote author=Daniel0 link=topic=100191.msg395144#msg395144 date=1152626744]
[quote author=yana link=topic=100191.msg395140#msg395140 date=1152626512]
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)...
[/quote]
Do you have a dynamic IP. I've read that at some ISPs the IP change at every request.

[quote author=yana link=topic=100191.msg395140#msg395140 date=1152626512]
Yeah apparently the Live Server has an older version of PHP (3 or so) and the Test Server uses the newer version 4...
[/quote]
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.
[/quote]

It's not my server - it's the company's.
[/quote]

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
[quote author=yana link=topic=100191.msg395151#msg395151 date=1152627093]
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.
[/quote]

I meant you, but see above for the explanation.

Share this post


Link to post
Share on other sites
Actually what I meant is that now my code works on the Test AND Live Server. I've used the following code to capture the IP Address:

[code]if (getenv('HTTP_X_FORWARDED_FOR')) {
        $ip_address = getenv('HTTP_X_FORWARDED_FOR');
    } else {
        $ip_address = getenv('REMOTE_ADDR');
    }[/code]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.