The example of this article describes the function of php based on the login time to realize multiple logins in a day and only score once. I would like to share it with you for your reference. The details are as follows:
I found a lot of cases on the Internet, and they all seemed similar. Some of them were quite cumbersome, so I tried it myself to find out how to implement this function.
To implement this function, I added a field logintime in the data table, indicating the last login time, and then used 0:00:00 of the day and the last login time. Compare, if the last login time is greater than this time point, it means you have already logged in. If the last login time is less than this time point, it means you are logging in for the first time. Increase points.
Code:
// 判断是否是一天中第一次登录 // 上一次登陆的时间 $lastLogintime = $userinfo['logintime']; // 一天中的零时零分零秒 $today = strtotime(date('Y-m-d')); if($lastLogintime < $today) { // 一天中第一次登录增加积分(关联更新) // 注意:使用关联更新数据的时候需要传递两次id $data['id'] = $userinfo['id']; $data['userinfo'] = array( 'points' => $userinfo['points'] + C('LOGIN'), ); $user->relation(true)->where(array('id'=>$userinfo['id']))->save($data); }
It should be noted that the login time must also be modified:
// 更新登录时间和登录ip $updateData = array( 'id' => $userinfo['id'], 'userinfo' => array( 'logintime' => time(), 'loginip' => getIP(), ), ); $user->relation(true)->where(array('id'=>$userinfo['id']))->save($updateData);
This way this function is implemented
The above is the detailed content of Example of PHP implementation of the function of logging in multiple times a day and only earning points once. For more information, please follow other related articles on the PHP Chinese website!