Jump to content

Countdown timer per user using $session


NooseLadder
 Share

Recommended Posts

Hi,

I have created a JS coundown timer displaying on a site. Example here: A-F33

At the moment the end date is hard coded into the function as a variable endDate.

The time difference is based on the current time versus the endDate. What I would like to do is when a user arrives at the page the timer is set from that moment to a specific enddate, maybe based on a set number of days e.g 7, 14, 30 etc. Then when user 2 arrives at the page, say the next day the timer begins the countdown for that user for the same number of days.

If user 1 returns to the page between the start of the coundown and the end he sees the time left relative to his visits and user 2 sees the time left relative to his visits, if that makes sense.

I'm looking for advice on how to set this up and how to track each user. I thought I could use $session and set a custom expiry time. When the user's countdown reaches zero, I would like them to be taken to a custom page that explains that the offer has expired and what they can do next etc etc.

So in a nutshell I would like each visitor to the page to have their own countdown initiated from their first visit. So each offer will have a set number of days to run for each user and at the end they are redirected to a n expiry page.

I hope that makes sense?

Link to comment
Share on other sites

Seems to me, if I understand your question correctly, that logged-in users could have a date/time field in their user page (admin/access/users/username), so that would be relatively trivial, but you'd need to set a cookie or use browser fingerprinting for non-logged-in users, which are more work and easier to circumvent from a user point of view, and then store a unique id for each anonymous user spotted, along with a date/time.

(Sorry that's all one sentence.  ??? )

Link to comment
Share on other sites

I just found this in the config.php file:

 * sessionExpireSeconds: how many seconds of inactivity before session expires
 *
 */
$config->sessionExpireSeconds = 86400; 
 

So this is set to 24 hours. I guess it can be changed to suit, but does the user closing the browser within 24 hours override the expiry time in respect of ending the session?

Link to comment
Share on other sites

A session also puts a cookie in the browser, so the user has exactly the same control over it. They would just have to clean the cookies or use another browser to reset the counter. I think you would have to have some kind of login to track this.

Link to comment
Share on other sites

So this is set to 24 hours. I guess it can be changed to suit, but does the user closing the browser within 24 hours override the expiry time in respect of ending the session?

No the session should still be active even if they close the browser. Now if they completely quit the browser, that might be different. 

Link to comment
Share on other sites

No the session should still be active even if they close the browser. Now if they completely quit the browser, that might be different. 

I'm not quite sure what you mean the difference to be between 'close the browser' and 'completely quit the browser'?

  • Like 1
Link to comment
Share on other sites

Thanks Ryan and Martijn. @DaveP Thanks for the tip on persistent cookies. I have come up with a basic process for a countdown timer and would like some feedback please. Now this uses cookies and I understand the risk of users clearing cookies, cookies disabled etc. A login to capture email would be ideal but for the offer I am working on it may put people off having to login.

Overview
Use cookies to set a countdown timer in JS for a set time when the user first visits the page.
An expiry date will be set. If the user returns before the expiry date the timer is set to the time remaining for that user.
If the user returns after the expiry date they are redirected to an expired page. Two cookies are set at the same time;
The first holds the expiry date of the timer as it's value, the second holds an expiry date further in the future than the first one.
I'll call the timer cookie 'countdown' and the other one 'itsgone'.
 
If both cookies already set {
    // Use 'countdown' value = expiry to calculate time remaining
    // Update timer via JS
    // Display timer page
}

If 'itsgone' is set and 'countdown' NOT set { // 'countdown' has expired
    // Redirect to expired page
}

If 'itsgone' and 'countdown' NOT set {
    //Set both cookies

    //$cExpiry = the expiry date of 'countdown'
    //$cValue = $cExpiry
    //$dExpiry = the expiry date of 'countdown'
    //$dValue = $dExpiry

    //setcookie('countdown', $cValue, $cExpiry);
    //setcookie('itsgone', $dValue, $dExpiry);
    // Display timer page

}
 
Link to comment
Share on other sites

My mind doesn't seem to understand pseudocode so well, but seems like you are on the right track. For something like this you'll want to test it all out to confirm it works the way you want to. Nobody can say for sure it'll work the way you want until it's confirmed to. :) 

Link to comment
Share on other sites

  • 1 year later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...