Home > Backend Development > PHP Tutorial > Example of PHP implementation of the function of logging in multiple times a day and only earning points once

Example of PHP implementation of the function of logging in multiple times a day and only earning points once

黄舟
Release: 2023-03-16 20:32:01
Original
1109 people have browsed it

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[&#39;id&#39;] = $userinfo[&#39;id&#39;];
  $data[&#39;userinfo&#39;] = array(
    &#39;points&#39; => $userinfo[&#39;points&#39;] + C(&#39;LOGIN&#39;),
  );
  $user->relation(true)->where(array(&#39;id&#39;=>$userinfo[&#39;id&#39;]))->save($data);
}
Copy after login

It should be noted that the login time must also be modified:

// 更新登录时间和登录ip
$updateData = array(
  &#39;id&#39; => $userinfo[&#39;id&#39;],
  &#39;userinfo&#39; => array(
    &#39;logintime&#39; => time(),
    &#39;loginip&#39; => getIP(),
  ),
);
$user->relation(true)->where(array(&#39;id&#39;=>$userinfo[&#39;id&#39;]))->save($updateData);
Copy after login

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template