Had this old perl program to set and log cookies and id’s but now we needed it to run from wordpress.
Figuring out how to set custom cookies (before the html begins) from inside thesis / wordpress was harder than it should have been!
So finally figured out that inside custom_functions.php you can add, for example,
add_action('init', 'myCookieMonster');
and this shows up before any headers / output are sent.
So now it’s a plugin, here is the code:
/*
Plugin Name: myCookieMonster
Plugin URI: n/a
Description: Sets and updates custom cookie "s"
Version: 1.0
Author: Joe
Author URI:
License:
*/
## Note: To move this Cookie functionality, change below in 2 NOTED places!
#-----Create the log file - for myCookieMonster ---------#
function writelog($tracking_id = '',$session_id = '') {
# Log visit
$timestamp=time();
$human_readable_timestamp=date("D M d h:m:s Y", $timestamp);
$log_file_line =
$session_id ."\n".
getenv("HTTP_REFERER") ."\n".
getenv("SCRIPT_URI") ."\n".
getenv("REDIRECT_SCRIPT_URI") ."\n".
getenv("REDIRECT_QUERY_STRING") ."\n".
$timestamp ."\n".
$human_readable_timestamp ."\n";
$log_file_line = str_replace("\n","\t",$log_file_line) . "\n";
// need to use absolute path on server, data dir needs 777 permission
## NOTE: CHANGE FOLLOWING LINE, & CHECK 'data' DIR PERMISSIONS = 777
$filename = '/home/##NOTE SET THIS PATH##/data/'.$tracking_id;
$handle = fopen($filename, 'a');
fwrite($handle, $log_file_line);
fclose($handle);
}
#-----Create the random numbers - for myCookieMonster ---------#
function CreateTrackingId() {
// create a 15 digit number
$myfile=0;
while ($myfile<1){
$newfile = rand(10000,99999).rand(10000,99999).rand(10000,99999);
$testthis = '/home/##NOTE SET THIS PATH##/data/'.$newfile;
if(!file_exists($testthis)){
$myfile=1;
}
}
return $newfile;
}
function myCookieMonster () {
# Cookie params
## NOTE: CHANGE FOLLOWING LINE
$cookie_domain='##NOTE SET THIS DOMAIN##';
$cookie_path='/';
$cookie_expiration='';
$cookie_name='s';
$tracking_id="";
$tracking_time = "";
$session_id="";
$cookie_life=60*60*24*365; # 1 year
$session_life = 1800; # 1/2 hour
# cookies are seperated by a semicolon and a space, this will split
# them and return a hash of cookies
$rawCookies = split ("; ",getenv("HTTP_COOKIE"));
foreach($rawCookies as $thisCookie){
$mysplit = split ("=",$thisCookie);
$key = $mysplit[0];
$val = $mysplit[1];
$current_cookies[$key] = $val;
//echo $key." : ".$val."
\n";
}
# Already has cookie set
if (isset($current_cookies["s"])) {
$message= "yes s"; // for debugging
// Lesson learned, from certain browsers, the cookie stores the '|' as '%7C'
$cooksplit = split("%7C", $current_cookies["s"]);
if (count($cooksplit)<3){
$cooksplit = split("|", $current_cookies["s"]);
}
$tracking_id = $cooksplit[0];
$session_id = $cooksplit[1];
$tracking_time = $cooksplit[2];
#Check previous tracking time???
if ((time() - $tracking_time) > $session_life) {
$session_id=CreateTrackingId();
}
#Reset tracking time
$tracking_time=time();
$current_cookies["s"] = $tracking_id . '|' . $session_id . '|' . $tracking_time;
$cookie_expiration=($tracking_time + $cookie_life);
//echo "
YES s, ".$tracking_id." , ".$session_id;
writelog($tracking_id,$session_id);
}
# No existing cookie
else {
$message= "no s
\n";
# Pick a random tracking id
$tracking_id=CreateTrackingId();
//echo $tracking_id."
\n";
$session_id=CreateTrackingId();
$tracking_time=time();
$current_cookies["s"] = $tracking_id . '|' . $session_id . '|' . $tracking_time;
$cookie_expiration=($tracking_time + $cookie_life);
//echo "
NO s, ".$tracking_id." , ".$session_id;
writelog($tracking_id,$session_id);
}
$cookie_value = $current_cookies["s"];
# Set cookie
//setcookie($cookie_name,$cookie_value,$cookie_expiration,$cookie_path);
setcookie($cookie_name,$cookie_value,$cookie_expiration,$cookie_path,$cookie_domain);
//print "cookie details: ".$cookie_name." , ".$cookie_value." , ".date("m-d-Y h:m:s",$cookie_expiration)." , ".$cookie_path ." , ".$cookie_domain;
//print $message; // for debugging, tells if the cookie was found or not
// End of Cookiemonster function
}
add_action('init', 'myCookieMonster');