Use PHP to determine the user's first login on the day

Source: Internet
Author: User
Tags current greenwich mean time
This article describes how to use PHP to determine that the user is logged on for the first time of the day. For more information about PHP tutorials, see. Preface

Some time ago, I was working on a project and asked people to automatically send points when logging on to the project for the first time every day.
I don't actually sign in to send points. I'm depressed for a few seconds.
After a while in Baidu, I thought about it and finally came up with this method.
That is to say, points are sent silently. only when someone else checks the points will they know this is the case. Otherwise, they will not know.

RIP

Every time the APP clicks the user interface, it needs to be logged in, so this is easy to do. I just need to determine whether it is the first login every day when logging in.
But some things are easier to say than doing. How can this problem be determined.
Query

users

Database, I have learned that the database has

last_login

This field. Records the timestamp of the Greenwich mean time of each login time.

Greenwich Mean Time

Greenwich Mean Time (GMT) refers to the standard Time of the Royal Greenwich Observatory on the outskirts of London, England, because the meridian is defined as the Meridian through which it passes. Since January 1, February 5, 1924, the Greenwich mean Observatory will distribute time-of-adjustment information to the world every hour.

Timestamp

Timestamp is usually a string that uniquely identifies the time at a given moment. The digital timestamp technology is a variant of the digital signature technology.
As a result, after logging in, you can first query the last timestamp (data has not been modified yet) and record it. then, you can change the timestamp to the latest and record it, compare the two timestamps. This is used here.

getdate()

Function

Getdate ()

The getdate () function obtains the date/time information.

Assume that $ user_id has been set, you can use the following method:

/* Query the last login last_login */$ SQL = "SELECT last_login FROM users WHERE user_id = '$ user_id '"; /* obtain the Greenwich mean time stamp of the last logon */$ last_login_old = $ GLOBALS ['DB']-> getOne ($ SQL ); /* get the last login date */$ last_login_date_old = getdate ($ last_login_old ); /* UPDATE last_login */$ SQL = "UPDATE users SET last_login = '". gmtime (). "'Where user_id = '$ user_id'";/* execute UPDATE */$ GLOBALS ['DB']-> query ($ SQL ); /* query the timestamp of the latest logon Greenwich Mean Time */ $ Last_login_new = gmtime ();/* Get the latest date */$ last_login_date_new = getdate ($ last_login_new);/* Compare, yday is the day of the year * // * if it is not the same, it indicates that it is the first login on the day */if ($ last_login_date_old ['Year']! = $ Last_login_date_new ['Year'] | $ last_login_date_old ['yday']! = $ Last_login_date_new ['yday']) {/* success, doSomething */}

This function is also required

/*** Obtain the timestamp of the current Greenwich Mean time * @ return integer */function gmtime () {return (time ()-date ('Z '));}

Related articles:

Question about how to determine whether a user entered a page for the first time?

How to determine whether a user accesses a mobile phone through the php website

Use php to determine whether a user pays attention to the public account

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.