dombrorj Posted November 11, 2011 Share Posted November 11, 2011 I'm trying to block access to a particular page if the visitor is not from a specific country (using Maxmind Geoip Country database), but not quite sure how to code this properly: I tried this and many variations of this inside the head tags, bud doesn't work. Any suggestions on the best way to go about this? <?php require_once("geoip.inc"); $gi = geoip_open("/home/username/GeoIP.dat",GEOIP_STANDARD); $country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']); geoip_close($gi); if($country_code !== 'US') header("HTTP/1.0 404 Not Found"); exit; ?> Quote Link to comment https://forums.phpfreaks.com/topic/250954-if-country-is-us-then-load-page-otherwise-force-404/ Share on other sites More sharing options...
Pikachu2000 Posted November 11, 2011 Share Posted November 11, 2011 What do you mean you tried it "inside the head tags"? Does that mean there is more code before the code posted above? Quote Link to comment https://forums.phpfreaks.com/topic/250954-if-country-is-us-then-load-page-otherwise-force-404/#findComment-1287471 Share on other sites More sharing options...
dombrorj Posted November 11, 2011 Author Share Posted November 11, 2011 Initially, yea. I placed it just above the <body> tags. I moved it to the very top, but still having trouble. I'm getting a "Cannot modify header information - headers already sent" warning. I checked for whitespace, but there isn't any. I forgot to add a couple brackets in the code above, so this is what I'm working with - without success. <?php require_once("geoip.inc"); $gi = geoip_open("/home/username/GeoIP.dat",GEOIP_STANDARD); $country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']); geoip_close($gi); if($country_code !== 'US') { header("HTTP/1.0 404 Not Found"); exit(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/250954-if-country-is-us-then-load-page-otherwise-force-404/#findComment-1287473 Share on other sites More sharing options...
markjoe Posted November 11, 2011 Share Posted November 11, 2011 if you're getting "Cannot modify header information - headers already sent", you positively have output being sent before your header() call. check "geoip.inc" also, if that has anything outside the <?php ?> tags, it will cause it too. If you still cannot find the offending characters, use a hex editor to check the actual bytes in the file in case there is some non-printing char that you text editor doesn't understand. Quote Link to comment https://forums.phpfreaks.com/topic/250954-if-country-is-us-then-load-page-otherwise-force-404/#findComment-1287476 Share on other sites More sharing options...
Pikachu2000 Posted November 11, 2011 Share Posted November 11, 2011 Did you read the sticky topic, HEADER ERRORS - READ HERE BEFORE POSTING THEM? Quote Link to comment https://forums.phpfreaks.com/topic/250954-if-country-is-us-then-load-page-otherwise-force-404/#findComment-1287477 Share on other sites More sharing options...
dombrorj Posted November 11, 2011 Author Share Posted November 11, 2011 Yes I did, but thanks for pointing me there... that info is very helpful. There is no html output though that I can see. I even tried removing everything else on the page and just using that small code. Still receiving the error. Quote Link to comment https://forums.phpfreaks.com/topic/250954-if-country-is-us-then-load-page-otherwise-force-404/#findComment-1287483 Share on other sites More sharing options...
Pikachu2000 Posted November 11, 2011 Share Posted November 11, 2011 Something is being output before the header if you're getting that error. The error message will tell you exactly where the output is. Quote Link to comment https://forums.phpfreaks.com/topic/250954-if-country-is-us-then-load-page-otherwise-force-404/#findComment-1287486 Share on other sites More sharing options...
markjoe Posted November 11, 2011 Share Posted November 11, 2011 If you haven't found the characters being output, keep looking, you missed it/them. Did you try viewing the files with a hex editor? (such as XVI32 if you're on Windows) This also includes blank lines after the ?> in "geoip.inc". (which is why i recommend not using closing tags at all) Saying you're looking for html output is not promising, you need to look for any and all bytes that may be output to the browser. Quote Link to comment https://forums.phpfreaks.com/topic/250954-if-country-is-us-then-load-page-otherwise-force-404/#findComment-1287488 Share on other sites More sharing options...
dombrorj Posted November 12, 2011 Author Share Posted November 12, 2011 Hmmm... this is strange. I tried deleting everything and entering just a basic php redirect as the only thing in the file and still received the error... making sure there was nothing else, no white spaces, etc. I'm using dreamweaver and also Notepad++. <?php header('Location: http://www.example.com/'); ?> So I tried copy/pasting that code above into a new file, overwriting the existing file, and it worked. So then I tried the same with the geo script, and it worked. Then I added the HTML and ran into problems. If the HTML code with the geo script is saved in Dreamweaver, I get the "headers already sent error." If I copy/paste everything into Notepad++ and save it there, it works but the page loads goofy. Where there are special characters on the page and apostrophe's, I get the question mark symbol � Quote Link to comment https://forums.phpfreaks.com/topic/250954-if-country-is-us-then-load-page-otherwise-force-404/#findComment-1287498 Share on other sites More sharing options...
dombrorj Posted November 12, 2011 Author Share Posted November 12, 2011 Changed the encoding from UTF-8 to ANSI in Notepad++ and everything works fine now. Thanks for all the help with this. So weird, this has never happened to me. Quote Link to comment https://forums.phpfreaks.com/topic/250954-if-country-is-us-then-load-page-otherwise-force-404/#findComment-1287500 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.