{"id":186,"date":"2011-03-07T01:18:23","date_gmt":"2011-03-07T01:18:23","guid":{"rendered":"http:\/\/www.omniweb.com\/wordpress\/?p=186"},"modified":"2011-03-07T02:30:20","modified_gmt":"2011-03-07T02:30:20","slug":"mycookiemonster","status":"publish","type":"post","link":"https:\/\/www.omniweb.com\/wordpress\/?p=186","title":{"rendered":"myCookieMonster"},"content":{"rendered":"<p>Had this old perl program to set and log cookies and id&#8217;s but now we needed it to run from wordpress.<br \/>\nFiguring out how to set custom cookies (before the html begins) from inside thesis \/ wordpress was harder than it should have been!<\/p>\n<p>So finally figured out that inside custom_functions.php you can add, for example,<\/p>\n<p><code>add_action('init', 'myCookieMonster');<\/code><\/p>\n<p>and this shows up before any headers \/ output are sent.<\/p>\n<p>So now it&#8217;s a plugin, here is the code:<\/p>\n<p><code>\/*<br \/>\nPlugin Name: myCookieMonster<br \/>\nPlugin URI: n\/a<br \/>\nDescription: Sets and updates custom cookie \"s\"<br \/>\nVersion: 1.0<br \/>\nAuthor: Joe<br \/>\nAuthor URI:<br \/>\nLicense:<br \/>\n*\/<\/p>\n<p>## Note: To move this Cookie functionality, change below in 2 NOTED places!<br \/>\n#-----Create the log file - for myCookieMonster ---------#<br \/>\nfunction writelog($tracking_id = '',$session_id = '') {<br \/>\n        # Log visit<br \/>\n        $timestamp=time();<br \/>\n        $human_readable_timestamp=date(\"D M d h:m:s Y\", $timestamp);<br \/>\n        $log_file_line =<br \/>\n\t$session_id .\"\\n\".<br \/>\n\tgetenv(\"HTTP_REFERER\") .\"\\n\".<br \/>\n\tgetenv(\"SCRIPT_URI\") .\"\\n\".<br \/>\n\tgetenv(\"REDIRECT_SCRIPT_URI\") .\"\\n\".<br \/>\n\tgetenv(\"REDIRECT_QUERY_STRING\") .\"\\n\".<br \/>\n\t$timestamp .\"\\n\".<br \/>\n\t$human_readable_timestamp .\"\\n\";<\/p>\n<p>  $log_file_line = str_replace(\"\\n\",\"\\t\",$log_file_line) . \"\\n\";<\/p>\n<p>  \/\/ need to use absolute path on server, data dir needs 777 permission<br \/>\n  ## NOTE: CHANGE FOLLOWING LINE, & CHECK 'data' DIR PERMISSIONS = 777<br \/>\n  $filename = '\/home\/##NOTE SET THIS PATH##\/data\/'.$tracking_id;<br \/>\n  $handle = fopen($filename, 'a');<br \/>\n  fwrite($handle, $log_file_line);<br \/>\n  fclose($handle);<br \/>\n}<\/p>\n<p>#-----Create the random numbers - for myCookieMonster ---------#<br \/>\nfunction CreateTrackingId() {<br \/>\n        \/\/ create a 15 digit number <\/p>\n<p>        $myfile=0;<br \/>\n        while ($myfile<1){\n                $newfile = rand(10000,99999).rand(10000,99999).rand(10000,99999);\n                $testthis = '\/home\/##NOTE SET THIS PATH##\/data\/'.$newfile;\n                if(!file_exists($testthis)){\n                        $myfile=1;              \n                }\n        }\n        return $newfile;\n}\n\t\nfunction myCookieMonster () { \n\t# Cookie params\n\t## NOTE: CHANGE FOLLOWING LINE\n\t$cookie_domain='##NOTE SET THIS DOMAIN##';\n\t$cookie_path='\/';\n\t$cookie_expiration='';\n\t$cookie_name='s';\n\t$tracking_id=\"\";\n\t$tracking_time = \"\";\n\t$session_id=\"\";\n\t$cookie_life=60*60*24*365; # 1 year\n\t$session_life = 1800; # 1\/2 hour\n\n  # cookies are seperated by a semicolon and a space, this will split\n  # them and return a hash of cookies\n  $rawCookies = split (\"; \",getenv(\"HTTP_COOKIE\"));\n  foreach($rawCookies as $thisCookie){\n     $mysplit = split (\"=\",$thisCookie);\n     $key = $mysplit[0];\n     $val = $mysplit[1];\n     $current_cookies[$key] = $val;\n     \/\/echo $key.\" : \".$val.\"<br \/>\\n\";<br \/>\n  }<\/p>\n<p>\t# Already has cookie set<br \/>\n\tif (isset($current_cookies[\"s\"])) {<br \/>\n     $message= \"yes s\";  \/\/ for debugging<br \/>\n     \/\/ Lesson learned, from certain browsers, the cookie stores the '|' as '%7C'<\/p>\n<p>     $cooksplit = split(\"%7C\", $current_cookies[\"s\"]);<br \/>\n  \t if (count($cooksplit)<3){\n\t\t\t\t $cooksplit = split(\"|\", $current_cookies[\"s\"]);\n\t\t }\n\n     $tracking_id = $cooksplit[0];\n     $session_id = $cooksplit[1];\n     $tracking_time = $cooksplit[2];\n        \t\t\n     #Check previous tracking time???\n     if ((time() - $tracking_time) > $session_life) {<br \/>\n         $session_id=CreateTrackingId();<br \/>\n     }<\/p>\n<p>     #Reset tracking time<br \/>\n     $tracking_time=time();<br \/>\n     $current_cookies[\"s\"] = $tracking_id . '|' .  $session_id . '|' . $tracking_time;<br \/>\n     $cookie_expiration=($tracking_time + $cookie_life);<br \/>\n     \/\/echo \"<br \/> YES s, \".$tracking_id.\" , \".$session_id;<br \/>\n     writelog($tracking_id,$session_id);<br \/>\n\t}<br \/>\n\t# No existing cookie<br \/>\n\telse {<br \/>\n     $message= \"no s <br \/>\\n\";<br \/>\n     # Pick a random tracking id<br \/>\n     $tracking_id=CreateTrackingId();<br \/>\n     \/\/echo $tracking_id.\" <br \/>\\n\";<br \/>\n     $session_id=CreateTrackingId();<br \/>\n     $tracking_time=time();<br \/>\n     $current_cookies[\"s\"] = $tracking_id . '|' .  $session_id . '|' . $tracking_time;<br \/>\n     $cookie_expiration=($tracking_time + $cookie_life);<br \/>\n     \/\/echo \"<br \/> NO s, \".$tracking_id.\" , \".$session_id;<br \/>\n     writelog($tracking_id,$session_id);<br \/>\n\t}<\/p>\n<p>\t$cookie_value = $current_cookies[\"s\"];<\/p>\n<p>\t# Set cookie<br \/>\n\t\/\/setcookie($cookie_name,$cookie_value,$cookie_expiration,$cookie_path);<br \/>\n\tsetcookie($cookie_name,$cookie_value,$cookie_expiration,$cookie_path,$cookie_domain);<\/p>\n<p>\t\/\/print \"cookie details: \".$cookie_name.\" , \".$cookie_value.\" , \".date(\"m-d-Y h:m:s\",$cookie_expiration).\" , \".$cookie_path .\" , \".$cookie_domain;<br \/>\n\t\/\/print $message; \/\/ for debugging, tells if the cookie was found or not<\/p>\n<p>\t\/\/ End of Cookiemonster function<br \/>\n}<\/p>\n<p>add_action('init', 'myCookieMonster');<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Had this old perl program to set and log cookies and id&#8217;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 &hellip; <a href=\"https:\/\/www.omniweb.com\/wordpress\/?p=186\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[],"_links":{"self":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/186"}],"collection":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=186"}],"version-history":[{"count":7,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/186\/revisions"}],"predecessor-version":[{"id":193,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/186\/revisions\/193"}],"wp:attachment":[{"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=186"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=186"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.omniweb.com\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=186"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}