yana Posted July 11, 2006 Share Posted July 11, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/ Share on other sites More sharing options...
hvle Posted July 11, 2006 Share Posted July 11, 2006 Did you get the blank email all the time, or sometime? Quote Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/#findComment-56168 Share on other sites More sharing options...
trq Posted July 11, 2006 Share Posted July 11, 2006 Both those variable are unreliable because they are sent by the client. Quote Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/#findComment-56174 Share on other sites More sharing options...
yana Posted July 11, 2006 Author Share Posted July 11, 2006 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........ ::) Quote Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/#findComment-56176 Share on other sites More sharing options...
yana Posted July 11, 2006 Author Share Posted July 11, 2006 So what is a good method to use to capture the IP address then???????? If the ones I am using are not reliable............... :o Quote Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/#findComment-56178 Share on other sites More sharing options...
trq Posted July 11, 2006 Share Posted July 11, 2006 Ip addresses are unreliable. What do you want them for? Quote Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/#findComment-56181 Share on other sites More sharing options...
hvle Posted July 11, 2006 Share Posted July 11, 2006 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'); } Quote Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/#findComment-56182 Share on other sites More sharing options...
yana Posted July 11, 2006 Author Share Posted July 11, 2006 My boss wants to capture the IP address and I just do what I am told ... unfortunately... Quote Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/#findComment-56190 Share on other sites More sharing options...
kenrbnsn Posted July 11, 2006 Share Posted July 11, 2006 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 Quote Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/#findComment-56194 Share on other sites More sharing options...
trq Posted July 11, 2006 Share Posted July 11, 2006 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 Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/#findComment-56196 Share on other sites More sharing options...
yana Posted July 11, 2006 Author Share Posted July 11, 2006 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????????? Quote Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/#findComment-56198 Share on other sites More sharing options...
yana Posted July 11, 2006 Author Share Posted July 11, 2006 [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! :) Quote Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/#findComment-56199 Share on other sites More sharing options...
Daniel0 Posted July 11, 2006 Share Posted July 11, 2006 Are you using different versions of PHP? Quote Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/#findComment-56200 Share on other sites More sharing options...
yana Posted July 11, 2006 Author Share Posted July 11, 2006 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??????????? Quote Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/#findComment-56204 Share on other sites More sharing options...
Daniel0 Posted July 11, 2006 Share Posted July 11, 2006 [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 Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/#findComment-56206 Share on other sites More sharing options...
trq Posted July 11, 2006 Share Posted July 11, 2006 10.243.102.136 appears to be your internal NIC address, while the other is your public IP. Quote Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/#findComment-56208 Share on other sites More sharing options...
yana Posted July 11, 2006 Author Share Posted July 11, 2006 [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 Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/#findComment-56210 Share on other sites More sharing options...
Daniel0 Posted July 11, 2006 Share Posted July 11, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/#findComment-56212 Share on other sites More sharing options...
yana Posted July 11, 2006 Author Share Posted July 11, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/#findComment-56213 Share on other sites More sharing options...
hvle Posted July 11, 2006 Share Posted July 11, 2006 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. Quote Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/#findComment-56215 Share on other sites More sharing options...
Daniel0 Posted July 11, 2006 Share Posted July 11, 2006 [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. Quote Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/#findComment-56216 Share on other sites More sharing options...
yana Posted July 11, 2006 Author Share Posted July 11, 2006 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] Quote Link to comment https://forums.phpfreaks.com/topic/14290-problem-capturing-and-displaying-ip-address/#findComment-56223 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.