Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

brianellisrules

macrumors regular
Original poster
Oct 17, 2003
229
0
OK, I have a counter setup for my site. It reads the IP of the user, checks it against a database and if the IP address doesn't exist in the db, it'll add it.

Here's my function to check the IP:
PHP:
function ipcheck() {

global $username, $password, $database, $table, $ip, $ipstatus;

if(getenv("HTTP_CLIENT_IP")) {
  $ip = getenv("HTTP_CLIENT_IP");
  }
elseif(getenv("HTTP_X_FORWARDED_FOR")) {
  $ip = getenv("HTTP_X_FORWARDED_FOR");
  }
else {
  $ip = getenv("REMOTE_ADDR");
  }

$db = mysql_connect("localhost", "$username", "$password");
mysql_select_db($database, $db) or die("Invalid server or user.");

//check to see if the IP exists
$grabip="select * from $table where ipaddy='$ip'";
$ipresult=mysql_query($grabip) or die("select  fails");
$ipstatus=mysql_num_rows($ipresult);
}
I was using just "$ip = getenv("REMOTE_ADDR");" to grab the IP, but someone from this forum (sonofslim??) suggested I use this series of if/elseif/else statements because it was better.

I'm getting some odd results though. When I check the mysql db, some odd looking IPs include:
10.77.45.179, 210.11.178.71 (saved in one cell of the table)
172.17.246.191, 193.
202.0.43.53, 203.97.
205.138.21.143, unknown
207.44.248.42, 200.1

etc, etc, etc...

It looks like it's grabbing 2 IP addresses when it does the check. So I ask... what's the dilly, yo??!

Thanks.


EDIT

Here's my insert function:
PHP:
if ($ipstatus==0)
{
$ipinfo= "insert into $table (ipaddy) values ('$ip')";
$insertip = mysql_query($ipinfo);
}
 
I'm interested.. can't see any glaring errors popping out at me, would like to see who comes up with the solution.

The SQL looks good.. to me.

Any chance you checked $ip with a print or echo before putting them in the Database?
 
brianellisrules said:
elseif(getenv("HTTP_X_FORWARDED_FOR")) {
$ip = getenv("HTTP_X_FORWARDED_FOR");
}
some odd looking IPs include:
10.77.45.179, 210.11.178.71
172.17.246.191, 193.
etc, etc, etc...

you probably don't want ton include this forwarded-thing here. you know those 10.something and 172.something addresses are private addresses used to computers behind nat, and the latter ip would probably be the address of its gateway. i think your script will do fine with only getting the REMOTE_ADDR.

when someone suggests some code being "better", you have to know what does it do and how it is better FOR YOU and your application.
 
Jeez, it took forever and a day just to get the reply window to load. Anyhoo.......
JFreak said:
you probably don't want ton include this forwarded-thing here. you know those 10.something and 172.something addresses are private addresses used to computers behind nat, and the latter ip would probably be the address of its gateway. i think your script will do fine with only getting the REMOTE_ADDR.

when someone suggests some code being "better", you have to know what does it do and how it is better FOR YOU and your application.
True, true.

One of the guys over at devshed filled me in on what could be going wrong. I've since scaled back and am now just using the last line, "getenv("REMOTE_ADDR")". Hopefully it'll be smooth sailing now.
 
hmm, not my code back there... unless my evil twin is handing out php advice again.

is your revised code working? i'd be interested to see if that solves the problem.
 
sonofslim said:
hmm, not my code back there... unless my evil twin is handing out php advice again.

is your revised code working? i'd be interested to see if that solves the problem.
Oops, sorry about that. Didn't mean to go around throwing out false accusations. :D
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.