#1
  1. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,548
    Rep Power
    221

    Is this a good solution? Or are there better ways to build this system?


    Hi;

    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?
  2. #2
  3. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,830
    Rep Power
    9646
    Originally Posted by English Breakfast Tea
    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.
    Cookie.

    Originally Posted by English Breakfast Tea
    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.
    IP addresses aren't unique, remember?

    If it's critical to know when the user visited then you'd go for a session, which is based on a cookie, so you might as well forget sessions and just use the cookie.

    If it's ridiculously absurdly critical to know when the user visited then you can try to fingerprint them - though it's quite difficult to do it reliably. But that sort of thing is evil. Fortunately I doubt this is the case anyways.
  4. #3
  5. A Change of Season
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    3,548
    Rep Power
    221
    Originally Posted by requinix
    Cookie.


    IP addresses aren't unique, remember?

    If it's critical to know when the user visited then you'd go for a session, which is based on a cookie, so you might as well forget sessions and just use the cookie.

    If it's ridiculously absurdly critical to know when the user visited then you can try to fingerprint them - though it's quite difficult to do it reliably. But that sort of thing is evil. Fortunately, I doubt this is the case anyways.
    Without passing email in the URL, it seems impossible to get this 100% right.

    I think cookie be the best too. Not 100% but as good as it gets. For this one, I have to do all the date calculations with javascript instead of PHP.
  6. #4
  7. Impoverished Moderator
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    16,830
    Rep Power
    9646
    Originally Posted by English Breakfast Tea
    For this one, I have to do all the date calculations with javascript instead of PHP.
    Correct, unless you want to borrow the value from the cookie (if present) and pass it to the script via the query string.
  8. #5
  9. Wiser? Not exactly.
    Devshed God 2nd Plane (6000 - 6499 posts)

    Join Date
    May 2001
    Location
    Bonita Springs, FL
    Posts
    6,270
    Rep Power
    4193
    You could store a token in the browsers localStorage. Not sure if it'd be any better than a cookie or not regarding lifetime. Apps that filter cookies may not (yet) filter localStorage, but I think if a user clears their cookies it clears localStorage also these days. The down side is that localStorage values cannot be accessed from PHP unlike cookies, so you'd have to have the page load some JS then make an AJAX request with your localStorage token. Alternatively, just store the countdown timer data into localStorage directly and do all the math client side.

    Whatever you do, using the IP address to identify users is incorrect. You need to either have something more personal like an email or generate unique tokens and store those somewhere.
    Recycle your old CD's



    If I helped you out, show some love with some reputation, or tip with Bitcoins to 1N645HfYf63UbcvxajLKiSKpYHAq2Zxud

IMN logo majestic logo threadwatch logo seochat tools logo