Jump to content

If Country is US then load page, otherwise force 404


dombrorj

Recommended Posts

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; 

?>

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();
}
?>

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.

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.

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 �

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.