Is this a good approach to create countdown timer like this?

Published 3 months ago by behnampmdg3

Hi;

How would you tackle this issue:

Requirements:

When a visitor hits the page, load a countdown timer that shows X days, hours and minutes before expiring. When the visitor returns, the timer should be set from the time visitor first visited the page.

My solution:

Give a piece of Javascript code to the client to embed in the page. Each time that code gets loaded, I create a record with IP address of the visitor in the database.

When that page gets reloaded, I query the database, if the IP exists, instead of creating the record, I load the countdown timer associated with that id, calculate the time since created, and load the countdown accordingly.

Ideas?

Best Answer (As Selected By behnampmdg3)
Cronix

You actually don't need a database to do that. You can create and read cookies from javascript, so you can just store the data in a cookie. When a visitor reaches the site, read the cookie. If the cookie exists, use the values you stored, if it doesn't, create it and store the values, like the last visit date/time, etc.

IP isn't really a good solution, especially in today's mobile world, where someone might access a site from their home (1 ip), their mobile (a different ip), some public wifi (another ip), their office (another ip), etc.

Cronix
Cronix
3 months ago (730,880 XP)

You actually don't need a database to do that. You can create and read cookies from javascript, so you can just store the data in a cookie. When a visitor reaches the site, read the cookie. If the cookie exists, use the values you stored, if it doesn't, create it and store the values, like the last visit date/time, etc.

IP isn't really a good solution, especially in today's mobile world, where someone might access a site from their home (1 ip), their mobile (a different ip), some public wifi (another ip), their office (another ip), etc.

behnampmdg3

Yo;

Is cookie reliable option?

Cronix
Cronix
3 months ago (730,880 XP)

I think it's more reliable than assuming the visitor will always have the same IP. I mean, at home mine changes every few days.

behnampmdg3

Ok,

I have to do calculations with JS rather than PHP now.

For this one, I'll drop the IP and DB idea altogether and focus 100% on the cookie.

WIll probably post about it soon.

Thanks

newbie360

depends on is a "visitor" or "real user" ?

i will open private browser, so cookie not work for me

behnampmdg3

"i will open private browser, so cookie not work for me"

What does Cronix have to say to that?

Cronix
Cronix
3 months ago (730,880 XP)

I'd say that the vast majority of the general public doesn't use private browsing. Just people who know better, like us geeks.

There isn't going to be a 100% foolproof solution. They all have drawbacks. But I think the chances of a users IP changing is a lot higher than the people who use private browsing.

newbie360

if use js write the time or a hash value ?

if just the time, visitor can edit the cookie

i think still need a table, well i dont know xD

Cronix
Cronix
3 months ago (730,880 XP)

if just the time, visitor can edit the cookie

Sure, but how many people in the general public do you think would actually do that, or even know how to do it? 99.99% of people don't even know there's a console or developer tools in their browser, even if they've been using it for 20 years. Most people aren't coders or understand how the web actually works. They just know how to use their browser.

I can mask my ip, or use a friends computer with a different ip, or use a VPN to use a different IP too, if I really wanted to get around something. But again, would the general public?

Every solution has drawbacks. What you want is the thing that will work in the vast majority of cases. Not all cases. That's impossible.

newbie360

yea agreed

behnampmdg3

The only reliable solution is getting them to sign up and pass the email in the url. Nothing beats the database.

Cronix
Cronix
3 months ago (730,880 XP)

Sure, but you never mentioned a user account as a possibility, or listed it as a criteria. All you talked about was storing a visitors IP in the database.

behnampmdg3

Yes, I don't have that option, unfortunately.

I am going with cookies.

Thanks bro

behnampmdg3

Also cant create cookies in Safari => https://laracasts.com/discuss/channels/javascript/safari-doesnt-create-cookie-in-iframe

That means iphones too :(

Please sign in or create an account to participate in this conversation.