Jump to content

[SOLVED] php ip script bugged?


darkfreaks

Recommended Posts

okay so i have an ip script that checks a text file for known country adresses and returns the name of the country however if i try to implement this into a block script it always returns the IP as united states and i am uncertain why i am using $_SERVER[REMOTE_ADDR] for the IP in my script. i assume it is a bug in the script yes ???

Link to comment
Share on other sites

From what I've read all of the items in the "$_SERVER" array can be fairly easily changed so I don't know that you necessarily can trust that...

 

SECURITY RISK !

 

Never ever trust the values that comes from $_SERVER.

 

HTTP_X_FORWARDED, HTTP_X_FORWARDED_FOR, HTTP_FORWARDED_FOR, HTTP_FORWARDED, etc.. can be spoofed !

 

To get the ip of user, use only $_SERVER['REMOTE_ADDR'], otherwise the 'ip' of user can be easily changed by sending a HTTP_X_* header, so user can escape a ban or spoof a trusted ip.

 

Are you always testing it from home?  Are you in the US?  Can you hard code a foreign IP into your page that you can use to test it out to give the page the impression you are in Europe or whatever country it is that you are trying to block?  (hope that makes sense)

Link to comment
Share on other sites

also here is the PHP IP class

countryfromip.inc.php:

<?php 
/** 
* This class generates the country name and its flag from its IP address 
* 
* 
* @author Rochak Chauhan 
*/ 

class CountryFromIP { 

     private $CountryIPDatabase = 'CountryIPDatabase.txt'; 
     private $ip = ''; 

    /** 
     * Function to validate IP ( please modify it according to your needs) 
     * 
     * @param $ip - string 
     * 
     * @return boolean 
     */ 
    public function ValdateIP($ip) { 
        $ipArray = explode(',',$ip); 

        if(count($ipArray) != 4) { 
            echo "<font color='red' size='3'> <b>ERROR: </b> Invalid IP</font>"; 
            return false; 
        } 
        else { 
            return true; 
        } 
    } 

    /** 
     * Function to return Country name from the IPDatabase 
     * 
     * @param $ip string 
     *  
     * @return string - name of the country, false otherwise 
     */ 
    public function GetCountryName($ip) { 
        $this->ip = $ip; 
        $ip = sprintf("%u", ip2long($ip)); 
     
        $csvArray = file($this->CountryIPDatabase); 
     
        for($i=0; $i<count($csvArray); $i++) { 
            $arrayOfLine = explode(',', $csvArray[$i]); 
            if($ip >= $arrayOfLine[0] && $ip <= $arrayOfLine[1] ) { 
                return $countryName = $arrayOfLine[2]; 
            } 
        } 
        return false; 
    } ?>

 

Main.php:

<?php
$ip="81.111.61.96";
require_once('CountryFromIP.inc.php');
//$ip = $_SERVER['REMOTE_ADDR'];
$object = new CountryFromIP(); 
$countryName =  $object->GetCountryName($ip);//always returns united states
?>

Link to comment
Share on other sites

this is the function that pulls the info and matches it to the $ip variable

 

public function GetCountryName($ip) { 
        $this->ip = $ip; 
        $ip = sprintf("%u", ip2long($ip)); 
     
        $csvArray = file($this->CountryIPDatabase); 
     
        for($i=0; $i<count($csvArray); $i++) { 
            $arrayOfLine = explode(',', $csvArray[$i]); 
            if($ip >= $arrayOfLine[0] && $ip <= $arrayOfLine[1] ) { 
                return $countryName = $arrayOfLine[2]; 
            } 
        } 
        return false; 
    } 

 

something is off in this function because its just printing united states as the country even if the IP is british  ???

Link to comment
Share on other sites

So what have you done to troubleshoot what is in $ip, $arrayOfLine[0], and $arrayOfLine[1] to find out why it is not working, since you obviously are going to ignore the suggestion to post the actual data involved that someone else would need to be able to help you.

Link to comment
Share on other sites

That code works for me using an IP of 212.63.161.221, which falls into the corresponding range of 3560939996,3560939999.

 

Your code likely has a problem reading the file in so nothing is being compared. So, I'll ask again, what have you done to troubleshoot what it is actually doing?

Link to comment
Share on other sites

ok this is what i have on main.php

 

<?php
require_once('CountryFromIP.inc.php');
$ip=str_replace(",",".",$_SERVER["REMOTE_ADDR"]);
$object = new CountryFromIP(); 
$countryName =  $object->GetCountryName($ip); 
if($countryName ="UNITED STATES"){
?>
html code here
<?php } ?>
<?php if($countryName!=="UNITED STATES"){?>
more html code here
<?php }?>

Link to comment
Share on other sites

In your code...

<?php
if($countryName ="UNITED STATES"){
?>
html code here
<?php } ?>
<?php if($countryName!=="UNITED STATES"){?>
?>

 

That first "if" will ALWAYS be true cause its just checking to see if united states was assigned to the variable of $countryName.  Add a second "=" and go from there.

Link to comment
Share on other sites

Well that's basically telling me that the variable is NOT equal to "UNITED STATES" then.  If that were the case then the first one would work.  Is there possibly a white space at the beginning or end?

 

Just to prove me wrong please try the following code...

 

<?php
if(trim(strtolower($countryName)) == "united states"){
?>
html code here
<?php } ?>
<?php if(trim(strtolower($countryName)) != "united states"){?>

Link to comment
Share on other sites

If my post does not work then print the value to the screen and view the page source.  See if there is like a "\ " escape character or something in between the words.  I had a major issue with an IF statement awhile back and it was all because of that.  If that is the case then try escaping slashes and comparing again.

Link to comment
Share on other sites

Are you sure there is nothing else in the middle there that is somehow changing the value?  Perhaps a function call or something along those lines?

 

There HAS to be something because basically what we are looking at here is an actual "Bug" with an "IF" statement and I'm betting that there is NOT one like this.  It has to be your code somewhere.

 

Try this code on a temp page or something just to see...

<?php
require_once('CountryFromIP.inc.php');
$ip=str_replace(",",".",$_SERVER["REMOTE_ADDR"]);
$object = new CountryFromIP(); 
$countryName =  $object->GetCountryName($ip); 

echo "<b>Country name info: </b>:<br />\n";
echo "Length: ".strlen($countryName)."<br />\n";
echo "Value:--".$countryName."--<br />\n";

if($countryName == "UNITED STATES")
{
   $echo "IS United States<br />\n";
} 

if($countryName != "UNITED STATES")
{
  $echo "NOT United States<br />\n";
}
?>

 

What are the exact results that it shows on the screen?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.