Welcome to WebmasterWorld Guest from 174.129.135.89

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Get The IP

   
4:17 pm on Jun 6, 2013 (gmt 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 Jun 7, 2013 (gmt 0)

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



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 Jun 8, 2013 (gmt 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 Jun 8, 2013 (gmt 0)

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



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 Jun 8, 2013 (gmt 0)

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



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 Jun 8, 2013 (gmt 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 Jun 8, 2013 (gmt 0)

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



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 Jun 9, 2013 (gmt 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 Jun 11, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



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...]
 

Featured Threads

Hot Threads This Week

Hot Threads This Month