Home > Article > PHP Framework > thinkphp5 determines whether it is a small program
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:
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".
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".
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');
2. Get the requested Referer information:
$referer = $this->request->header('Referer');
3. Get the requested ContentType information :
$contentType = $this->request->contentType();
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); } } }
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, ];
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!