the-sexy-slug Posted August 3, 2010 Share Posted August 3, 2010 Hello I am trying to show/geo target content for certian countries that my visitors to my site are from. I have doing a little research on google and have found a few script but for some reason they do not work. I know about the geoip database from maxmind but I am looking for a remote solution as the geoip database is said to be a heavy strain on servers if you get a lot of visitors. I am hosted on a shared server so I do not want to get my account suspended for over working the server. Also I have a few sites that will use this idea. This is a script I found whilst googling and all it does is give me an error. <?php $ip = $_SERVER [ 'REMOTE_ADDR' ]; $country = file_get_contents ( 'http://api.hostip.info/country.php?ip=' . $ip ); if ( $country == "US" ) { echo "<American-English Ringtone Ad Tag>" ; } elseif ( $country == "AU" OR $country == "UK" OR $country == "IE" ) { echo "<British-English Ringtone Ad Tag>" ; } else { echo "<YPN Ad Tag>" ; } ?> This is the error it gives me:- Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/username/public_html/webaddress.co.uk/test/test.php on line 7 What I am trying to do is to geo target Javascript ads for different countries. When I add the javascript ad it gives me that error. If I just put in normal text then it works ok. Thank you all for your help in advance. Quote Link to comment https://forums.phpfreaks.com/topic/209641-php-to-geo-target-content-for-country/ Share on other sites More sharing options...
Yesideez Posted August 3, 2010 Share Posted August 3, 2010 <?php $ip = $_SERVER['REMOTE_ADDR']; $country = file_get_contents('http://api.hostip.info/country.php?ip='.$ip); if ($country=="US") { echo "<American-English Ringtone Ad Tag>" ; } elseif ($country=="AU" || $country=="UK" || $country=="IE") { echo "<British-English Ringtone Ad Tag>"; } else { echo "<YPN Ad Tag>"; } ?> Couple of things... 1. PHP uses || instead of OR 2. If displaying < or > on a web page you need to use the codes otherwise the browser will try and interpret the contents as HTML and nothing will be displayed Quote Link to comment https://forums.phpfreaks.com/topic/209641-php-to-geo-target-content-for-country/#findComment-1094502 Share on other sites More sharing options...
Yesideez Posted August 3, 2010 Share Posted August 3, 2010 As an extra, if you're choosing multiple country codes a switch() conditional would be better. switch ($country) { case "US": echo "<American-English Ringtone Ad Tag>"; break; case "UK": case "IE": case "AU": echo "<British-English Ringtone Ad Tag>"; break; default: echo "<YPN Ad Tag>"; } Quote Link to comment https://forums.phpfreaks.com/topic/209641-php-to-geo-target-content-for-country/#findComment-1094506 Share on other sites More sharing options...
the-sexy-slug Posted August 3, 2010 Author Share Posted August 3, 2010 Thank you for yout reply. That seams to work with text but when I put in a javascript ad code it gives me an error:- Parse error: syntax error, unexpected T_STRING, expecting ',' or ';' in /home/username/public_html/webaddress.co.uk/test/test.php on line 9 Any advice to help me show javascript ads for different locations would be very much appreciated. I have been trying to solve this problem now for months. Thanks all in advance for your advice. Quote Link to comment https://forums.phpfreaks.com/topic/209641-php-to-geo-target-content-for-country/#findComment-1094509 Share on other sites More sharing options...
Yesideez Posted August 3, 2010 Share Posted August 3, 2010 You can post your Javascript and I'll have a quick look but you're better off posting that in the Javascript section - also because if I can't sort it someone else might. As for the above script, if hostip.info ever goes down or has difficulties your web page will have until file_get_contents() times out. I just knocked this up using cURL that will wait a maximum of 2 seconds - you can change this to however long you want your script to wait, replace the file_get_contents line with the following: $ch=curl_init('http://api.hostip.info/country.php?ip='.$ip); curl_setopt($ch,CURLOPT_RETURNTRANSFER,true); curl_setopt($ch,CURLOPT_HEADER,false); curl_setopt($ch,CURLOPT_TIMEOUT,2); $country=curl_exec($ch); if (curl_errno($ch)) { echo 'ERROR'; } curl_close($ch); Quote Link to comment https://forums.phpfreaks.com/topic/209641-php-to-geo-target-content-for-country/#findComment-1094511 Share on other sites More sharing options...
Yesideez Posted August 3, 2010 Share Posted August 3, 2010 Just thought - if your PHP routine above is outputing straight into a Javascript routine change the > and < for the > and < respectively and try again. Quote Link to comment https://forums.phpfreaks.com/topic/209641-php-to-geo-target-content-for-country/#findComment-1094513 Share on other sites More sharing options...
the-sexy-slug Posted August 3, 2010 Author Share Posted August 3, 2010 Thanks for the reply. I have changed the > and < with the < etc as you suggested and I still get the same error. What I am trying to do is to post a ebay affiliate link that I created using there ebay tool kit program. It produces a javascript code that you paste in your website. And for some reason it is not working when I paste into the php. Quote Link to comment https://forums.phpfreaks.com/topic/209641-php-to-geo-target-content-for-country/#findComment-1094515 Share on other sites More sharing options...
KevinM1 Posted August 3, 2010 Share Posted August 3, 2010 1. PHP uses || instead of OR Just for education's sake, PHP actually uses both, but they have different operator precedences. See: http://www.php.net/manual/en/language.operators.comparison.php http://www.php.net/manual/en/language.operators.logical.php Quote Link to comment https://forums.phpfreaks.com/topic/209641-php-to-geo-target-content-for-country/#findComment-1094519 Share on other sites More sharing options...
Wolphie Posted August 3, 2010 Share Posted August 3, 2010 What is the JavaScript code you're actually trying to use? Quote Link to comment https://forums.phpfreaks.com/topic/209641-php-to-geo-target-content-for-country/#findComment-1094520 Share on other sites More sharing options...
the-sexy-slug Posted August 3, 2010 Author Share Posted August 3, 2010 What I am trying to do is to post a ebay affiliate link that I created using there ebay tool kit program. It is the code that the EPN or the Ebay partner Netword Editors tool kit creates. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/209641-php-to-geo-target-content-for-country/#findComment-1094533 Share on other sites More sharing options...
PradeepKr Posted August 8, 2010 Share Posted August 8, 2010 Try putting < and > only. And not &glt; etc. I think you are passing that only to javascript and not showing it on html. If problem persists....you need to put here (into the forum) the javascript. Then only somebody can help Quote Link to comment https://forums.phpfreaks.com/topic/209641-php-to-geo-target-content-for-country/#findComment-1096481 Share on other sites More sharing options...
PradeepKr Posted August 8, 2010 Share Posted August 8, 2010 One more thing, even file_get_contents() can be made to handle timeout. check http://www.expertsguide.info/2010/08/set-time-out-while-downloading-files/, for the same and other different ways to implement timeouts. Quote Link to comment https://forums.phpfreaks.com/topic/209641-php-to-geo-target-content-for-country/#findComment-1096482 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.