Jump to content

GeoIP image script not functioning correctly - shows multiple images


bluefrog

Recommended Posts

Hi I'm using an IP database to correctly (+/-) serve related ads based on country. This seems to work fine BUT when tested on a USA proxy (I'm in the UK) I am getting TWO ads served, one for USA (correctly) and one for the UK (proxy error?). When surfing via proxy from the Netherlands I get no image at all.

 

Looking for somebody to see what I've done wrong here:

 

$server   = 'localhost'; // MySQL hostname
$username = '***'; // MySQL username
$password = '***'; // MySQL password
$dbname   = '***'; // MySQL db name


$db = mysql_connect($server, $username, $password) or die(mysql_error());
      mysql_select_db($dbname) or die(mysql_error());

$sql = 'SELECT
            country
        FROM
            ip2nation
        WHERE
            ip < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'")
        ORDER BY
            ip DESC
        LIMIT 0,1';

list($country) = mysql_fetch_row(mysql_query($sql));

switch ($country) {
	case 'us':
		// Image or USA
		echo '<img src="impact/samsungN210PLUS_usa.jpg">';

	case 'uk':
		// Image for UK
		echo '<img src="impact/samsungN210PLUS_uk.jpg">';

	default:
	// Image for everybody else (europe)
		echo '<img src="impact/samsungN210PLUS_europe.jpg">';
	end;
}


echo '<div width="420" class="loc"><span  class="loc" align="center">Not from ';


	$sql = 'SELECT
            c.country
        FROM
            ip2nationCountries c,
            ip2nation i
        WHERE
            i.ip < INET_ATON("'.$_SERVER['REMOTE_ADDR'].'")
            AND
            c.code = i.country
        ORDER BY
            i.ip DESC
        LIMIT 0,1';

list($countryName) = mysql_fetch_row(mysql_query($sql));

// Output full country name
echo $countryName;
$domain = str_replace("www.","", $_SERVER['HTTP_HOST']);
echo '? Choose your flag: <a href="http://uk.' .$domain. '"><img src="images/flags/ukflag.gif"></a> <a href="http://usa.' .$domain. '"><img src="images/flags/usaflag.gif"></a> <a href="http://europe.' .$domain. '"><img  src="images/flags/euflag.gif"></a></span></div>';



 

All help appreciated and thanks in advance.

 

PS. The script is working here: http://darrenbassett.com/trial if somebody in the USA wishes to check and let me kow if it is a proxy surfing error (seeing BOTH IP's). Not a real website just a 'play' page to get the bones of the script working.

I know why now.

You forgot to use break; after each case!

 

your code:

	switch ($country) {
	case 'us':
		// Image or USA
		echo '<img src="impact/samsungN210PLUS_usa.jpg">';

	case 'uk':
		// Image for UK
		echo '<img src="impact/samsungN210PLUS_uk.jpg">';

	default:
	// Image for everybody else (europe)
		echo '<img src="impact/samsungN210PLUS_europe.jpg">';
	end;
}

it will run USA, then UK, then default.

 

How it should be done:

	switch ($country) {
	case 'us':
		// Image or USA
		echo '<img src="impact/samsungN210PLUS_usa.jpg">';
		break;
	case 'uk':
		// Image for UK
		echo '<img src="impact/samsungN210PLUS_uk.jpg">';
		break;
	default:
	// Image for everybody else (europe)
		echo '<img src="impact/samsungN210PLUS_europe.jpg">';
		break;
}

Here it stops the switch when the case is done.

Hi, if you mean echo the supposed country (based on IP) then this is already doing it under the image script, which when surfing via proxy is echoed correctly, USA and Netherlands, but the images are not representing that data. The European one gets nothing served and the USA gets UK and USA served lol.

Great stuff that has stopped the multiple images showing, but now I still have a problem with the 'default' image not showing if the IP isn;t from the list in the code.

 

If I browse via proxy from Canada or Netherlands, no image shows up BUT the IP is correctly seen and displayed in text.

 

Any idea's?

Ah, as easy as that. Is this so the script is stopped  - ie the script is stopped after the correct value is shown?

Sorry for editing so many times, I should start to use the review button before I post, but I so often remember something I want to add some minutes after I've posted.

 

To the point, you can use this in for example loops too.

 

 

example:

while(1<2){

}

will run forever you might say, but the truth is, you can stop it.

$i=0;
while(1<2){
$i++;
if($i==50){
	break;
}else{
	echo $i;
}
}

will echo 50 numbers, from 0 to 49 and then "break" the loop. =P

 

 

oh and you shouldn't break the default.. my bad!

 

	switch ($country) {
	case 'us':
		// Image or USA
		echo '<img src="impact/samsungN210PLUS_usa.jpg">';
		break;
	case 'uk':
		// Image for UK
		echo '<img src="impact/samsungN210PLUS_uk.jpg">';
		break;
	default:
		// Image for everybody else (europe)
		echo '<img src="impact/samsungN210PLUS_europe.jpg">';
}

Archived

This topic is now archived and is closed to further replies.

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