Recently, I was asked to create a script that sets a cookie in a visitors browser and does the following:
1. On page-load set visit, date, time.
2. If it’s set already, don’t reset.
3. Evaluate if it’s same day or after midnight.
4. Output “offer is on” or “offer is expired” variable.
5. Display content conditional in body.
After a lot of research, and not much luck finding something suitable online…I determined that the script needed to be written from scratch. To hopefully save you some time..I’ve included it here!
So, you’re going to need two files:
FILE ONE: script.php
include('ip2locationlite.class.php');
//start get timezone
$ipLite = new ip2location_lite;
$ipLite->setKey('a231dc748021a92a3937a7a35825de0199a5c5d38bef5566427db378257de3a9');
$locations = $ipLite->getCity($_SERVER['REMOTE_ADDR']);
$seconds = (int)$locations['timeZone'];
$seconds = $seconds * 3600;
//end get timezone
//current timestamp
$curtime = time();
//adjust for timezone
$curtime += $seconds;
//variables you can use later in your page to determine whether or not to show it
$display_offer = 0;
$display_expire = 0;
//for testing purposes, or if you need to use it
$done = 0;
//if there isn't an offer currently offered
if(!isset($_COOKIE['offer'])){
//make sure they already havent received the offer before
if(!isset($_COOKIE['visit'])){
//set a cookie based on timestamp
setcookie("offer", $curtime);
//show the offer page
$display_offer = 1;
}
//for testing purposes
else{
$done = 1;
}
}
//the offer is in progress
else{
//get the time that the script was offered
$offer_time = $_COOKIE['offer'];
//get the seconds
$offer_secs = $offer_time % 86400;
$cur_secs = $curtime % 86400;
//if it's the next day, too late
if($cur_secs<$offer_secs){
//clear cookies and set cookies to make sure it doesnt happen again, display message that it has expired
setcookie("visit", 1);
setcookie('offer','',time()-3600);
$display_expire = 1;
}
}
//Example purposes:
//for when the offer expires
if($display_expire){
echo "Sorry, your offer expired $cur_secs $offer_secs";
}
//first visiting
else if($display_offer){
echo "Register before midnight!!!!!";
}
//incase you want to show something after the offer is over
else if($done){
echo "No more offers.";
}
//incase you want to show something while the offer is available
else{
echo "Offer is in progress $cur_secs $offer_secs";
echo "
";
}
?>
FILE TWO: ip2locationlite.class.php
final class ip2location_lite{
protected $errors = array();
protected $service = 'api.ipinfodb.com';
protected $version = 'v3';
protected $apiKey = '';
public function __construct(){}
public function __destruct(){}
public function setKey($key){
if(!empty($key)) $this->apiKey = $key;
}
public function getError(){
return implode("\n", $this->errors);
}
public function getCountry($host){
return $this->getResult($host, 'ip-country');
}
public function getCity($host){
return $this->getResult($host, 'ip-city');
}
private function getResult($host, $name){
$ip = @gethostbyname($host);
if(preg_match('/^(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:[.](?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}$/', $ip)){
$xml = @file_get_contents('http://' . $this->service . '/' . $this->version . '/' . $name . '/?key=' . $this->apiKey . '&ip=' . $ip . '&format=xml');
try{
$response = @new SimpleXMLElement($xml);
foreach($response as $field=>$value){
$result[(string)$field] = (string)$value;
}
return $result;
}
catch(Exception $e){
$this->errors[] = $e->getMessage();
return;
}
}
$this->errors[] = '"' . $host . '" is not a valid IP address or hostname.';
return;
}
}
?>