Support forums : Quickies

$Session or cookie help

A home for really quick one-liner thoughts about what we need to fix or do or whatever. If they spark discussion we move it, otherwise this is like post-it notes.

Moderator: Dracones

$Session or cookie help

Postby leeturner » Fri Sep 16, 2011 2:47 pm

Not sure if this is the right forum for this - maybe we need a 'Plugin Coding Help' forum or something.

Anyway, i am in the middle of coding a plugin that does a jquery style popup when a visitor displays a blog. In the back end there is a setting to say how long to wait before the popup is displayed again. The plugin does the popup by including all the bits of JS and CSS in the TemplateBeginHtmlHead function so my initial thought was to just check for a cookie in there, if it didn't exist display the popup and set the cookie. I quickly realised that that wasn't going to work as the headers have already been sent by the time the TemplateBeginHtmlHead is called.

So, I do a little bit more digging and think that the BeforeBlogDisplay might be a cool place to do it but while digging around I find a post by Yabs on the evo forums saying that using the $Session is the best way to do this. So, I give this a try and it seems to work fine - kind of like this in the TemplateBeginHtmlHead function:

Code: Select all
$cookie = $this->session_get( "popupmonster" );
if ( empty( $cookie ) )
   //include my stuff
   $this->session_set( "popupmonster", "leewozere", 60, true );

The above code is set to 60 seconds so a visitor loads the blog, gets the popup, cancels it and after 60 seconds it will display again if they reload a page. All seems to work fine.

Obviously 60 seconds is going to be a pain in the ass and most users will want to set a number of days as the expiry. My question is, given I have never used $Session before, is this the best way to do this? Does the $Session setting persist over time in the same way as cookies do. If I set the expiry to a month, a user visits my site, gets the popup and then doesn't come back again till 2 weeks later, will the $Session still be remembered and the popup not be displayed?

Or should I use cookies for this?

User avatar
Posts: 730
Joined: Fri Dec 11, 2009 11:55 am
Location: Brighton, UK

Return to Quickies

Who is online

Users browsing this forum: No registered users and 2 guests