Cookie Monster

Hey no one ever needed to invite Cookie Monster to eat a cookie right, so why not use cookies to invite your visitors.

I can’t say that I’ve had considerable need to entice a visitor to do something on my site using a cookie but recently I did.  Just to make it clear I decided to use the PHP setcookie() Function not a JavaScript Cookie, no real reason other than I likes my PHP!  Let’s get all crazy and invite all our visitors to Sesame Street, sound good?  Great!

First we’ll need to create a tempting Call to Action (CTA) – I know “Want to find  Sesame Street? Friendly Neighbors Click Here!“, awesome…

Understand that cookies are sent along with the rest of the HTTP headers.  Once the cookies have been set, they can be accessed on the next page load with the $_COOKIE array.  Our CTA is going to help us set the cookie so that we can present our visitor with the invitation (this is great opportunity to use “Geo Targeting“,  click here).

Next we’ll need to bake, I mean create our cookie script – yummy – I’m gonna get lazy and just “paste” it for you.

function moorescode_Function() {
<!--?php $cookie_name = "Friendly Neighbor"; $cookie_value = "123 Sesame Street (located right next door to Oscar's trash can and Big Bird's nest)"; setcookie($cookie_name, $cookie_value, strtotime('2017-05-18 12:00:00'), "/"); if(!isset($_COOKIE[$cookie_name])) { echo "Hey you're invited to Sesame Street?&amp;nbsp; &lt;a onclick='moorescode_Function()' title='Click here!'&gt;Click here!&lt;/a&gt;"; } else { echo "We welcome you as our &lt;u&gt;" . $cookie_name . "&lt;/u&gt; to come and visit us!&lt;br&gt;&lt;br&gt;"; echo "Join us at " . $cookie_value . " for fun, food and laughter from 10:00am to 2:00pm&lt;br&gt;&lt;br&gt;"; $target = mktime(12, 0, 0, 5, 18, 2017) ; $today = time () ; $difference =($target-$today); $days =(int) ($difference/3600); echo "So when someone asks you'll be able to tell how to get to Sesame Street, the fun starts in $days hour(s).&lt;br&gt;&lt;br&gt;&lt;i&gt;See you there!&lt;/i&gt;"; } ?-->

I’ve always wanted to go to Sesame Street and now that I’ve been invited I can’t wait!

So the script above serves a few purposes…

  1. We set the cookie.
  2. We eat the cookie (that’s just silly).
  3. The cookie will only expire when we determine – like so strtotime(‘2017-05-18 12:00:00’).
  4. We display how much time is left for our “Friendly Neighbor” to act on this invitation.

Really cool if you’re looking for something engaging while keeping your marketing efforts in-mind.   Just to be clear I’m not stating anything new here, the links above will attest to that.

Sunny Day
Sweepin' the clouds away
On my way to where the "code" is sweet...


David Moores

Advanced knowledge of web server deployment, database management systems, and server-side scripting languages. Strengths lie in digital analytics, metric components and the implementation of data tracking methods for mining interpretation. The body of my work has been a balance of problem solving and technical know-how. I love a problem and love solving them even more!

Leave a Reply