Home  >  Article  >  Backend Development  >  Login and user authentication of PHP and applet

Login and user authentication of PHP and applet

王林
王林Original
2023-07-04 20:05:071552browse

Login and user authentication of PHP and mini-programs

With the rapid development of the mobile Internet, mini-programs have grown rapidly in recent years and have become an indispensable tool in people's daily lives. User login and identity verification are a very important part of mini program development. This article will introduce how to use PHP and applet development to implement user login and authentication.

1. Mini program implementation

The login and identity verification of the mini program mainly uses the mini program login function provided by WeChat. The applet obtains the user's openid by calling WeChat's API and sends it to the server for verification.

  1. On the mini program side, introduce WeChat login

    wx.login({
      success: function(res) {
     if (res.code) {
       // 获取到code后,发送给服务器进行验证
       wx.request({
         url: 'https://yourserver.com/login.php',
         data: {
           code: res.code
         },
         success: function(res) {
           // 获取到用户的身份信息,保存到本地存储中
           wx.setStorageSync('token', res.data.token);
         }
       });
     } else {
       console.log('登录失败!' + res.errMsg);
     }
      }
    });
  2. Authenticate based on openid

    wx.request({
      url: 'https://yourserver.com/verify.php',
      data: {
     token: wx.getStorageSync('token')
      },
      success: function(res) {
     if (res.data.valid) {
       console.log('身份验证通过!');
     } else {
       console.log('身份验证失败!');
     }
      }
    });

2. Server-side implementation

The server-side uses PHP to process user login and authentication. The specific implementation is as follows:

  1. mini program login verification

    <?php
    $code = $_GET['code'];
    $appId = 'your_app_id';
    $appSecret = 'your_app_secret';
    
    $apiUrl = 'https://api.weixin.qq.com/sns/jscode2session';
    $queryString = http_build_query([
      'appid' => $appId,
      'secret' => $appSecret,
      'js_code' => $code,
      'grant_type' => 'authorization_code'
    ]);
    
    $response = file_get_contents($apiUrl . '?' . $queryString);
    $json = json_decode($response);
    
    if ($json && !empty($json->openid)) {
      // 生成token,并将其返回给小程序
      $token = md5(uniqid());
      $data = [
     'openid' => $json->openid,
     'token' => $token
      ];
      echo json_encode($data);
    } else {
      echo json_encode(['error' => '登录失败!']);
    }
    ?>
  2. identity verification

    <?php
    $token = $_GET['token'];
    // 根据token查询用户信息
    // ...
    
    if ($valid) {
      echo json_encode(['valid' => true]);
    } else {
      echo json_encode(['valid' => false]);
    }
    ?>

The above is Basic implementation of login and user authentication in PHP and mini-programs. It should be noted that the openid of the mini program plays an important role in the authentication process, and the user's detailed information can be queried or saved based on the openid.

In actual development, the user login and identity verification functions can be further improved, such as adding SMS verification codes, using passwords to log in, etc. In addition, in order to ensure the security of user information, security measures such as signature verification can also be added to the request.

I hope this article will help you understand the login and user authentication of PHP and mini programs. I wish you better results in mini program development!

The above is the detailed content of Login and user authentication of PHP and applet. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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