Cookie Publishing: What’s New Since Your Last Visit


Published on June 23, 2005 at 6:07 PM EST
In the Tutorials category.

The idea is pretty simple: returning visitors get to see only new content, not the old stuff. After all, if they’re returning, that’s probably what they want.

Using a cookie, I can track when you’ve last visited the front page. If any content is newer than the cookie, it gets published. If not, a “no new entries” message gets displayed. And so I dubbed it “Cookie Publishing.”

You’ll need to be able to use PHP to implement this feature. Visit danandsherree.com’s front page to see it work—first a cookie will be set, then refresh to see it in action—or keep reading for details of how to set this up yourself.

2005/08/02 Update: I tried to publish an entry tonight but ran into a wacky problem where the new entry was listed as old. A few changes to the encoding took care of it easy enough. The $entrydate variable had to be updated, and the changes are bolded:

$entrydate = mktime(<MTEntryDate format="%H, %M, %S, intval('%m'), %e, %Y">);

Of course, the changes are also reflected below.

I’ve set up this process to work on a 30-day period because that’s a good amount of time for this site and its freqency of updates. You might want to change it to a longer or shorter time to better suit your site.

The below code all belongs in your Main Index template—your site’s front page.

Set a Cookie

Of course, the heart of Cookie Publishing is setting a cookie. You’ll need to set the cookie in the first line of your Main Index template:

<?php

// How long should something be considered new for? (In seconds.)
// seconds * minutes * hours * days
// Default is 72 hours (3 days).
$stillnew = 60*60*24*3;

setcookie('CookiePublishing', time()-$stillnew, time()+60*60*24*30, '/');

?>

Note the $stillnew variable. This is used to keep entries new for a specified amount of time. In other words, so an entry isn’t considered old as soon as it’s viewed. Shorten or lenghten the time to suit how often you update your site.

Also note that the cookie is set to expire in 30 days (time()+60*60*24*30). Increasing or decreasing this time will, in effect, “reset” the process for the user. I would guess most people visit at least once a month, so I thought this was a good number.

Display New Content

Displaying new content is a two-step process. First, decide if there’s any new content to display:

<MTEntries lastn="1">

<?php
$entrydate = mktime(<MTEntryDate format="%H, %M, %S, intval('%m'), %e, %Y">);

if ($_COOKIE['CookiePublishing'] >= $entrydate) {
  echo '<p>Sorry, no new entries since your last visit.</p>';
}
?>

</MTEntries>

The message above, “Sorry, no new entries since your last visit,” can be changed to suit you.

Below, the new entries are actually displayed. Note that days="30" key/value in the MTEntries tag; change it if you like. Change the entry display code to suit your needs; I’ve reproduced the default 3.x template code below.

<MTEntries days="30">

<?php
$entrydate = mktime(<MTEntryDate format="%H, %M, %S, intval('%m'), %e, %Y">);

if ($_COOKIE['CookiePublishing'] <= $entrydate) {
?>

<MTDateHeader>
<h2><$MTEntryDate format="%x"$></h2>
</MTDateHeader>

<h3 id="a<$MTEntryID pad="1"$>"><$MTEntryTitle$></h3>

<$MTEntryBody$>

<MTEntryIfExtended>
<p class="extended"><a href="<$MTEntryPermalink$>#more">Continue reading "<$MTEntryTitle$>"</a></p>
</MTEntryIfExtended>

<p class="posted">Posted by <$MTEntryAuthor$> at <a href="<$MTEntryPermalink valid_html="1"$>"><$MTEntryDate format="%X"$></a>
<MTEntryIfAllowComments>
| <a href="<$MTEntryPermalink archive_type="Individual"$>#comments">Comments (<$MTEntryCommentCount$>)</a>
</MTEntryIfAllowComments>
<MTEntryIfAllowPings>
| <a href="<$MTEntryPermalink archive_type="Individual"$>#trackbacks">TrackBack (<$MTEntryTrackbackCount$>)</a>
</MTEntryIfAllowPings>
</p>

<?php
}
?>

</MTEntries>

Summarize Older Entries

You never want to tell the user nothing is available, so I’m listing older entries in a summary format. If you’re looking to change it, note the 30-day setting in two spots.

<?php $anyold = 0; ?>

<MTEntries days="30">
<?php
$entrydate = mktime(<MTEntryDate format="%H, %M, %S, intval('%m'), %e, %Y">);

if ($_COOKIE['CookiePublishing'] >= $entrydate) {
  $anyold++;
} ?>
</MTEntries>


<?php if ($anyold !== 0) {
  echo '<h2>Older Recent Entries</h2>';
} ?>


<MTEntries days="30">

<MTEntriesHeader>
<ul>
</MTEntriesHeader>

<?php
$entrydate = mktime(<MTEntryDate format="%H, %M, %S, intval('%m'), %e, %Y">);

if ($_COOKIE['CookiePublishing'] >= $entrydate) {
?>

<li><a href="<MTEntryPermalink>" title="Written on <MTEntryDate>"><MTEntryTitle></a></li>

<?php
}
?>

<MTEntriesFooter>
</ul>
</MTEntriesFooter>

</MTEntries>

And that’s it!