Cookie and PHP - an example
Posted: Wed Feb 17, 2010 8:05 pm
Hello guys,
I'm starting with PHP, and I came across an issue far too complicated for me to solve.
I'm running a blog and I would like to give an alert if a recent user comes back, and there are new comments on the listed posts to read on, since his last visit.
What I would figure "mentally" is the following process :
- the user arrives, and receives a cookie recording the server's time.
- Next time he comes, that cookie is checked against the time the last comment has
- if user's time is less than comment's, print a "New Comment!" message per new comment
- if not, nothing happens
But, if I were to translate this in PHP I didn't know how to do it.
Imagine I can have an array of comment's times, via a tag called get_last_comment_time();
If anybody can give me a snippet, I could figure out the rest of the syntaxis...
This is my terrible, hypothetical attempt :
Is this syntaxis correct ?
Could I use this in a loop ?
Many thanks for any input.
I'm starting with PHP, and I came across an issue far too complicated for me to solve.
I'm running a blog and I would like to give an alert if a recent user comes back, and there are new comments on the listed posts to read on, since his last visit.
What I would figure "mentally" is the following process :
- the user arrives, and receives a cookie recording the server's time.
- Next time he comes, that cookie is checked against the time the last comment has
- if user's time is less than comment's, print a "New Comment!" message per new comment
- if not, nothing happens
But, if I were to translate this in PHP I didn't know how to do it.
Imagine I can have an array of comment's times, via a tag called get_last_comment_time();
If anybody can give me a snippet, I could figure out the rest of the syntaxis...
This is my terrible, hypothetical attempt :
Code: Select all
<!-- I send the cookie -->
<?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, '/');
?>
<!--I check the cookie and print -->
<?php
$entrydate = get_last_comment_time();
if ($_COOKIE['CookiePublishing'] < $entrydate) {
echo '<p>New Comment!</p>';
}
?>
Could I use this in a loop ?
Many thanks for any input.