Jump to content

Function to Alert me when someone outside our IP address accesses a page


GeekInAsia

Recommended Posts

I am trying to create a simple function that I can call from the start of each page. It is simply checking where a users is in the UK or trying to access whilst overseas.  I have the detection working fine, however it alerts me every time they reload or change a page. What i want is just to be alerted once. My code it below if someone can point out my mistakes I would appreciate it.

 

<?php 
if (getenv(@HTTP_X_FORWARDED_FOR))
{
	$tmp_SurferIP = @getenv(HTTP_X_FORWARDED_FOR);
} else {
	$tmp_SurferIP = @getenv(REMOTE_ADDR);
}
$tmp_SurferCountry = geoip_country_code_by_addr($gi, $tmp_SurferIP);

if ($tmp_SurferCountry != "GB")
{	
	if (@$_SESSION['SurferCountryDenied'] != TRUE)
	{
		$tmp_SentToEmail = "[email protected]";
		$tmp_MessageSubject = "Site Access Denied Notice - ".$tmp_SurferCountry." - ".$tmp_SurferIP;
		SendEmail ($tmp_SentToEmail,$tmp_MessageSubject,$tmp_MessageBody);
		@$_SESSION['SurferCountryDenied'] = TRUE;
	}
	echo "Sorry you are not in the UK, so access to this site has been denied";
	exit;
}
else
{
	if (@$_SESSION['SurferCountryGranted'] != TRUE)
	{
		$tmp_SentToEmail = "[email protected]";
		$tmp_MessageSubject = "Site Access Granted Notice - ".$tmp_SurferCountry." - ".$tmp_SurferIP;
		$tmp_MessageBody = "The IP address <strong>".$tmp_SurferIP."</strong> has been granted access to the site due to being detected within the United Kingdom";
		@$_SESSION['SurferCountryGranted'] = TRUE;
		SendEmail ($tmp_SentToEmail,$tmp_MessageSubject,$tmp_MessageBody);
	}		
}
?>

You'll likely want to set up a database table to store the ip addresses and a timestamp in, then only insert the address if it doesn't already exist in the table (or update the timestamp if it does). You could also use the timestamp so the mail script would be able to run every xx minutes or hours on a cron job and send any new entries rather than a separate mail for each one.

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.