0

I am updating IP location in database using third party free service by a small php script. It was working fine but suddenly it stopped working. I tried to figure out what happened but I could not. Here is my script

<?
$sno = 0;
$query = "SELECT date,ac_no,ip,country FROM visitors where $mainfilter order by ac_no,date desc limit 0,$maximumips;";

$result = db_query($query,"&nbsp;");
while($line=db_fetch_array($result))
{

$country = "";
$updatestatus = "";
$ip = $line[ip];
$sno = $sno + 1;

if($updateip=="Yes")
{
    $ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$ip));
    if($ip_data && $ip_data->geoplugin_countryName != null)
    {
        $country = $ip_data->geoplugin_countryName;
    }

    if($country!="")
    {
        $q_det = "update visitors set country='$country' where ip='$line[ip]'";
        db_exec($q_det);

        $q_det = "update ips set country='$country' where ip='$line[ip]'";
        db_exec($q_det);
        $updatestatus = "Yes - External";
    } else {
        $updatestatus = "No Trace";
    }
    sleep($waitseconds);        
}

echo "$sno | $line[date] | $line[ac_no] | $ip | $country | $updatestatus";?><br><?
}
?>

The problem is following command $ip = $line[ip];

In cpanel errorlog it says "Use of undefined constant ip - assumed 'ip' in /home/"

However when I use "echo $ip" it prints the ip, it means ip is stored in this variable but on other side it gives above mention error in cpanel.

Earlier I was using $line[ip] variable directly to get country name in following command and it was working perfectly but suddenly it stopped.

        $ip_data = @json_decode(file_get_contents("http://www.geoplugin.net/json.gp?ip=".$line[ip]));

Then I thought to store IP in $ip variable and then use there but still it does not work.

Please help me to know why cpanel givers error and script stopd although it was working fine and also data is stored in $ip variable which shows when "echo $ip" is used.

Thanks for help

2 Answers 2

2

Enclosing array key in quotes '' will fix it. Without quotes PHP assumes it a CONSTANT and hence throws an error.

$ip = $line['ip'];

Also change variable name in your UPDATE queries.

$q_det = "update visitors set country='$country' where ip='$ip'";
$q_det = "update ips set country='$country' where ip='$ip'";
Sign up to request clarification or add additional context in comments.

1 Comment

his query part is still wrong, he can't just add single qoute around ip in there, correct the query as well please
1

Try changing $ip = $line[ip] to $ip = $line['ip'] using ip without quotes php treats it like it's a constant

And to fix your queries

$q_det = "update visitors set country='$country' where ip=$ip";
db_exec($q_det);

$q_det = "update ips set country='$country' where ip=$ip";
db_exec($q_det);

2 Comments

his query part is still wrong, he can't just add single qoute around ip in there, correct the query as well please
Done. I think both me and Samir assumed he was using the variable instead of $line[ip] because he said "Then I thought to store IP in $ip variable and then use there but still it does not work."

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.