Home >PHP Framework >ThinkPHP >What to do if thinkphp token fails
Solution to thinkphp token failure: 1. Open the WeChat applet token verification code; 2. Clear the cache area before outputting "echostr", that is, place the "ob_clean();" code before echo. This solves the problem of token failure.
The operating environment of this tutorial: Windows 7 system, ThinkPHP version 5, Dell G3 computer.
What should I do if thinkphp token fails?
Solve the problem of ThinkPHP WeChat mini program token verification failure
Today I am studying the WeChat mini program and applied for a mini program account. The application steps are: WeChat Mini program development - register an account.
During the development and configuration process, you need to fill in the server information and verify the token, but the verification always fails. After reading other people's handling methods, I summarized the method and shared it.
Solution: Before outputting "echostr", clear the cache area, that is, place the "ob_clean();" code before echo.
Official explanation: ob_clean() This function is used to discard the content in the output buffer;
The verification code is as follows:
/** * 微信小程序token校验 * @return bool */ public function wxtoken() { $signature = input('get.signature'); $timestamp = input('get.timestamp'); $nonce = input('get.nonce'); $token = '1234567'; $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); $tmpStr = implode($tmpArr); $tmpStr = sha1($tmpStr); if($tmpStr == $signature){ ob_clean(); echo input('get.echostr'); }else{ return false; } }
WeChat official verification of token The description is: The developer verifies the request by checking the signature (the verification method is below). If it is confirmed that this GET request comes from the WeChat server, please return the echostr parameter content as it is, then the access will take effect and you will become a developer successfully, otherwise the access will fail.
The encryption/verification process is as follows:
1. Sort the three parameters token, timestamp, and nonce in lexicographic order
2. Splice the three parameter strings into A string is encrypted with sha1
3. The encrypted string obtained by the developer can be compared with the signature to identify that the request comes from WeChat;
Recommended learning: "thinkPHP Video Tutorial 》
The above is the detailed content of What to do if thinkphp token fails. For more information, please follow other related articles on the PHP Chinese website!