bluefrog Posted December 11, 2010 Share Posted December 11, 2010 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. Link to comment https://forums.phpfreaks.com/topic/221323-geoip-image-script-not-functioning-correctly-shows-multiple-images/ Share on other sites More sharing options...
MMDE Posted December 11, 2010 Share Posted December 11, 2010 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. Link to comment https://forums.phpfreaks.com/topic/221323-geoip-image-script-not-functioning-correctly-shows-multiple-images/#findComment-1145765 Share on other sites More sharing options...
bluefrog Posted December 11, 2010 Author Share Posted December 11, 2010 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. Link to comment https://forums.phpfreaks.com/topic/221323-geoip-image-script-not-functioning-correctly-shows-multiple-images/#findComment-1145770 Share on other sites More sharing options...
bluefrog Posted December 11, 2010 Author Share Posted December 11, 2010 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? Link to comment https://forums.phpfreaks.com/topic/221323-geoip-image-script-not-functioning-correctly-shows-multiple-images/#findComment-1145772 Share on other sites More sharing options...
MMDE Posted December 11, 2010 Share Posted December 11, 2010 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">'; } Link to comment https://forums.phpfreaks.com/topic/221323-geoip-image-script-not-functioning-correctly-shows-multiple-images/#findComment-1145775 Share on other sites More sharing options...
bluefrog Posted December 11, 2010 Author Share Posted December 11, 2010 Thank you so much for the information - it's all valuable when you're learning!! I found out why the default wasn't working - wrong image name lol, all working now. Thank again. Link to comment https://forums.phpfreaks.com/topic/221323-geoip-image-script-not-functioning-correctly-shows-multiple-images/#findComment-1145780 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.