homepage Welcome to WebmasterWorld Guest from 54.196.24.103
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Accredited PayPal World Seller

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Get The IP
iWareWolf




msg:4581735
 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;
}
}

 

brotherhood of LAN




msg:4582150
 4:50 pm on Jun 7, 2013 (gmt 0)

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.

iWareWolf




msg:4582231
 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;
}
}

brotherhood of LAN




msg:4582241
 1:31 am on Jun 8, 2013 (gmt 0)

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

lucy24




msg:4582259
 2:30 am on Jun 8, 2013 (gmt 0)

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.

iWareWolf




msg:4582336
 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;
}
}

brotherhood of LAN




msg:4582397
 10:17 pm on Jun 8, 2013 (gmt 0)

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.

iWareWolf




msg:4582424
 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')

Readie




msg:4583185
 5:11 pm on Jun 11, 2013 (gmt 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...]

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved