Welcome to WebmasterWorld Guest from 23.22.220.37

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Get The IP

     
4:17 pm on Jun 6, 2013 (gmt 0)

New User

joined:June 6, 2013
posts: 4
votes: 0


I'm currently trying to log ips in a MYSQL database but it doesn't seem to be working. Im testing on localhost. Currently its just logging 0 as the converted ip.

 $data = mysql_query("SELECT ip,points FROM information") 
or die(mysql_error());

$ip = $_SERVER['REMOTE_ADDR'];

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {

$ip = $_SERVER['HTTP_CLIENT_IP'];

} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {

$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];

}

return $ip;
}
$ip = getIp();

while($info = mysql_fetch_array( $data ))
{
if($info['ip'] != null && $info['ip'] == ip2long($ip)){
Print "You have $info[points] points!";
break;
}
if($info['ip'] != null && $info['ip'] != ip2long($ip)){
mysql_query("INSERT INTO information VALUES ('INET6_ATON($ip)','0')");
Print "You have 0 points.";
break;
}
}
4:50 pm on June 7, 2013 (gmt 0)

Moderator from GB 

WebmasterWorld Administrator brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:4842
votes: 1


Welcome to the forums iWareWolf,

I take it you were testing within Apache and not the command line?

You seem to be calling $ip = getIp(); after evaluating $_SERVER['REMOTE_ADDR'] and the other variables. Does that function exist and is it returning the data you expect?

Try
$ip = getIp();
die("|$ip|");
after you've populated the variable to ensure the data is as expected.

Also alter the last query to
mysql_query("INSERT INTO information VALUES ('INET6_ATON($ip)','0')") or die(mysql_error());
and you'll see if it's the query causing the problem. Also, the function INET6_ATON shouldn't have quotes around it.
12:28 am on June 8, 2013 (gmt 0)

New User

joined:June 6, 2013
posts: 4
votes: 0


Yes I am currently using Apache to test out my code. Here is my new altered code. It currently is printing out
|::1|

 $data = mysql_query("SELECT ip,points FROM information") 
or die(mysql_error());

function getIp() {

$ip = $_SERVER['REMOTE_ADDR'];

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {

$ip = $_SERVER['HTTP_CLIENT_IP'];

} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {

$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];

}

return $ip;
}
$ip = getIp();
die("|$ip|");

while($info = mysql_fetch_array( $data ))
{
if($info['ip'] != null && $info['ip'] == ip2long($ip)){
Print "You have $info[points] points!";
break;
}
if($info['ip'] != null && $info['ip'] != ip2long($ip)){
mysql_query("INSERT INTO information VALUES (INET6_ATON($ip),'0')") or die(mysql_error());
Print "You have 0 points.";
break;
}
}
1:31 am on June 8, 2013 (gmt 0)

Moderator from GB 

WebmasterWorld Administrator brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:4842
votes: 1


At least you know the variable is present.

Aftering removing the die() statement, if you're getting no output then obviously your conditions are not being met.

Also, ip2long is for IPv4 addresses...
2:30 am on June 8, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

joined:Apr 9, 2011
posts:12696
votes: 244


Also, ip2long is for IPv4 addresses...

Isn't that the purpose of the second "if" loop? The one that says $info['ip'] != ip2long($ip) and then goes into the INET6_ATON business

Is there a reason for the overall logical structure?

if($info['ip'] != null && $info['ip'] == ip2long($ip))
and then, separately,
if($info['ip'] != null && $info['ip'] != ip2long($ip))

I'd expect an outer loop
if($info['ip'] != null)
with an inner
if($info['ip'] == ip2long($ip))
...
else

?

When my fingers typed 'loop' my brain meant something else, but I've gone blank on the word I meant.
4:21 pm on June 8, 2013 (gmt 0)

New User

joined:June 6, 2013
posts: 4
votes: 0


This is giving the ip NULL in MYSQL.


function getIp() {

$ip = $_SERVER['REMOTE_ADDR'];

if (!empty($_SERVER['HTTP_CLIENT_IP'])) {

$ip = $_SERVER['HTTP_CLIENT_IP'];

} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {

$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];

}

return $ip;
}
$ip = getIp();

while($info = mysql_fetch_array( $data ))
{
if($info['ip'] != null){
if($info['ip'] == ip2long($ip)){
Print "You have $info[points] points!";
break;
}
if($info['ip'] != ip2long($ip)){
mysql_query("INSERT INTO information VALUES (INET_ATON('$ip'),'0')") or die(mysql_error());
Print "You have 0 points.";
break;
}
}
10:17 pm on June 8, 2013 (gmt 0)

Moderator from GB 

WebmasterWorld Administrator brotherhood_of_lan is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:Jan 30, 2002
posts:4842
votes: 1


It'd help to see what the query is that's being inserted (including the variable $ip) by echo'ing the statement.... and knowing the table structure of the table being inserted into.
1:14 am on June 9, 2013 (gmt 0)

New User

joined:June 6, 2013
posts: 4
votes: 0


The result of the echo is this:
::1

And the query is this:
INSERT INTO information VALUES (INET_ATON('$ip'),'0')
5:11 pm on June 11, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member 5+ Year Member

joined:Dec 13, 2009
posts:943
votes: 0


I know this comment doesn't really pertain to the issue at hand, but...
$info['ip'] != null

Just jumping in here to say you should ideally be using the !== (not identical to) operator here, as all the following are considered equal to null by PHP:
false
0
''
array()

Get in the habit of doing this and it can save you a lot of headaches further down the line :)

Further reading:
[docs.php.net...]