1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.
  2. Better Analytics for WordPress Get It Free

Detecting if logged in daily

Discussion in 'PHP' started by Jeehan, Mar 5, 2014.

  1. #1
    trying to do this:

    a data will be inserted in db if a user log in every day, like if he logged in anytime on March 6 it will insert a row, and then again if he log in again any time on March 7 it will add another row again, continue like this. So trying to detect if user returning and login to site within every 24 hours or not. Like in some online games users get reward if they login every day.

    Hoping I was able to explain properly. Any help is appreciated.
     
    Jeehan, Mar 5, 2014 IP
  2. TIEro

    TIEro Active Member

    Messages:
    742
    Likes Received:
    177
    Best Answers:
    5
    Trophy Points:
    70
    #2
    Without knowing the platform or database or anything, it'll be a vague answer... but all you'd do is something like this:
    1. Add a line to your login script that calls a function (record_login or something) when their login is successful.
    2. The record_login checks a table of logins and sees if there's a record for today.
    3. If not, it adds a line to a table with today's date. php has a date() function that you can use to store today's date in any format.
    Step 2 is the important bit because you only want a single line for each date. That way, you can check logins either by counting the number of entries (for things like "10 logins in the last 14 days") or by recursively stepping back through dates and checking each one exists in turn. Or by pulling all dates between certain limits into an array and stepping through that to see if there's one for each day. Or some other method. It could get quite slow to check through over time, of course.

    Alternatively, if you're only interested in continuous daily logins, store a field with the number of days and the last login date, linked to the user id:
    1. Same as above.
    2. The record_login function checks the last login date.
    3. If last login was yesterday, then increment the number of successive days logged in.
    4. Otherwise set to 1 day.
    That way you don't have any specific date info to store, your table only ever has one line per user and it's VERY fast to check. But you don't have the login history, should you ever want it for anything.

    You could, in fact, mix both methods and use the second for a quick check with the first as the full data version if you ever need to look through their login history - just link both tables to user_id and it'd work.

    There are dozens of ways to do it, depending on what exactly you want to achieve. :)
     
    TIEro, Mar 5, 2014 IP
  3. Jeehan

    Jeehan Well-Known Member

    Messages:
    1,578
    Likes Received:
    29
    Best Answers:
    1
    Trophy Points:
    115
    #3
    Thank you very much for your reply. Sorry forgot to mention them :p its PHP and Mysql. On #2 you said if thers a record for today, how does it know if the data added is "today" ?
     
    Jeehan, Mar 5, 2014 IP
  4. TIEro

    TIEro Active Member

    Messages:
    742
    Likes Received:
    177
    Best Answers:
    5
    Trophy Points:
    70
    #4
    You'd get the data for the user and use date() for today's date, and the same date function combined with strtotime() to get the raw data into a format that php recognises and will compare.

    e.g. if (date('Ymd', strtotime($user_login_date)) == date('Ymd'))

    ("if the user_login_date converted to a timestamp, then put into the 'Ymd' date format, is the same as the current date in the 'Ymd' format...")

    Just make sure you use the same format on both dates and the comparison will work.
     
    TIEro, Mar 5, 2014 IP
  5. Jeehan

    Jeehan Well-Known Member

    Messages:
    1,578
    Likes Received:
    29
    Best Answers:
    1
    Trophy Points:
    115
    #5
    Thank you very much buddy, I really appreciate it.
     
    Jeehan, Mar 5, 2014 IP