Home> PHP Framework> ThinkPHP> body text

thinkphp5 determines whether it is a small program

PHPz
Release: 2023-05-29 12:23:40
Original
1086 people have browsed it

With the development of small programs, more and more web applications are developing small programs. When developing mini programs, we often need to judge access requests in order to provide exclusive services for the mini program.

For web applications developed using the ThinkPHP5 framework, it is very simple to determine whether the access request comes from a small program. The specific implementation method is introduced below.

1. Characteristics of mini program requests

When the mini program sends a request to a web application, it will add some special identifiers to the HTTP header (header) so that the web application can Identify these requests as coming from mini-programs. Among them, common mini program request characteristics include the following:

  1. User-Agent contains the identifier of the mini program

in the User-Agent field of the HTTP header , the mini program will have its own logo. For example, the logo of the WeChat applet is "miniProgram" and the logo of the Alipay applet is "AlipayClient".

  1. Referer contains the identifier of the mini program

When the mini program initiates a request to the web application, it will carry its own source information, that is, the Referer field. Web applications can use the Referer field to determine whether the request comes from the applet. For example, the Referer of the WeChat applet will contain "servicewechat.com".

  1. ContentType is "application/json"

When the applet sends a request to the web application, it is usually sent in the "application/json" format. Therefore, the web application can determine whether the request comes from the applet through the ContentType field.

Next, we can use the above characteristics to determine whether the request comes from the mini program to provide exclusive services.

2. Use ThinkPHP5 to determine mini program requests

In ThinkPHP5, we can use the Request class to obtain and determine the relevant information of the request. The specific implementation method is as follows:

1. Get the requested User-Agent information:

$userAgent = $this->request->header('User-Agent');
Copy after login

2. Get the requested Referer information:

$referer = $this->request->header('Referer');
Copy after login

3. Get the requested ContentType information :

$contentType = $this->request->contentType();
Copy after login

The above code can be placed in the controller method and called.

3. Mini Program Request Interceptor

In addition to directly determining whether it is a mini program request in the controller method, we can also use ThinkPHP5's request interceptor to request the mini program Perform unified interception and processing.

The specific implementation method is as follows:

1. Create the middleware directory in the application directory, and create the CheckMiniProgram.php file in the middleware directory (the file name can be customized).

2. Write the interceptor code in the CheckMiniProgram.php file to determine whether the request comes from the mini program, and if so, perform relevant processing. For example:

namespace appmiddleware; use thinkRequest; class CheckMiniProgram { public function handle(Request $request, Closure $next) { $contentType = $request->contentType(); $userAgent = $request->header('User-Agent'); $referer = $request->header('Referer'); if ($contentType == 'application/json' && strpos($userAgent, 'miniProgram') !== false && strpos($referer, 'servicewechat.com') !== false) { //是小程序请求,进行相关处理 ... } else { //不是小程序请求,直接放行 return $next($request); } } }
Copy after login

In the above code, we first use the $request->contentType() method to obtain the requested ContentType information, and then use the $request->header() method to obtain the requested User-Agent and Referer information. Finally, it is judged whether the information meets the characteristics requested by the mini program. If so, the corresponding processing will be carried out, otherwise it will be released directly. It should be noted that the actual processing logic needs to be filled in at the "..." in the above code.

3. Add the CheckMiniProgram middleware in the middleware.php configuration file in the application directory:

return [ 'CheckMiniProgram' => appmiddlewareCheckMiniProgram::class, ];
Copy after login

In this way, the mini program request can be intercepted and processed in all requests of the application. .

4. Summary

This article introduces the method of using ThinkPHP5 to judge mini program requests and the method of intercepting and processing mini program requests. By judging the User-Agent, Referer, ContentType and other information in the request, you can easily determine whether the request comes from the mini program and provide exclusive services for the mini program.

The above is the detailed content of thinkphp5 determines whether it is a small program. For more information, please follow other related articles on the PHP Chinese website!

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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!