Home>Article>PHP Framework> Solve the problem of ThinkPHP verification code not displaying

Solve the problem of ThinkPHP verification code not displaying

藏色散人
藏色散人 forward
2020-09-05 09:19:13 4363browse

The followingthinkphpframework tutorial column will introduce to you the solution to the problem that the ThinkPHP verification code is not displayed. I hope it will be helpful to friends in need!

Solve the problem of ThinkPHP verification code not displaying

I encountered a very strange BUG today, that is, I wrote a PHP program that runs normally locally, but when it is published to Party A's server, there is a BUG that the verification code cannot be displayed. .

Solve the problem of ThinkPHP verification code not displaying

The specific manifestation is that a small cracked picture icon is displayed on the Chrome browser. Right-click the picture to open it in a new window. It is still a small cracked picture, not Error message (when xdebug extension and php error reporting are enabled), if so, it means that there is no problem with the verification code URL routing and business logic code, but there is a problem with the verification code generation process. My debugging idea is as follows:

1. Check whether the GD library is installed correctly. Linux recommends using yum, apt, or pecl to install it. Windows recommends using the phpStudy integrated environment. It has integrated commonly used extensions and can be opened with the right mouse button. (Applicable to situations where the verification code image displays an error message and a bunch of garbled characters when it opens in a new window. If there is an error message, please debug according to the error message)

Solve the problem of ThinkPHP verification code not displaying

2. Check whether the font called by the verification code is missing. Some fonts called by the verification code components are not installed on the target computer. This requires analysis of the source code of the verification code module. I use ThinkPHP’s official think-captcha. You can find its source code at vendortopthinkthink-captchasrc. You can see here at vendortopthinkthink-captchaassets that this verification code plug-in comes with its own font, so ThinkPHP’s verification code is basically unnecessary. Considering the font issue, if it is another verification code plug-in, you still need to check this place.

3. Download the buggy verification code image and the local test normal verification code image, use Notepad to view and compare.

Solve the problem of ThinkPHP verification code not displaying

#The picture above is the notepad of the verification code image in the local environment. We use a more professional hexadecimal editor to analyze the file differences.

Solve the problem of ThinkPHP verification code not displaying

As shown in the picture, we found that the bad file bad has exactly the same content except for the addition of EF BB BF and other numbers in front of it, indicating that PHP has too many errors when rendering the image. These texts were rendered. As for the reason, I guess it may be that there is no professional code editor on the server, so the operation and maintenance personnel used ordinary Windows Notepad to modify the PHP code, resulting in the addition of this BOM header.

If it is the third reason, there are two solutions. One is to use ob_clean to clear the output buffer of php before calling the imagepng output image code of the verification code. There is also the use of some tools to remove the BOM of the code in batches.

Solve the problem of ThinkPHP verification code not displaying

I use ob_clean here, add ob_clean() before line 203 of Captcha.php, the main file of think-captcha, so that it can be solved once and for all. If you have strict requirements on performance and stability, it is recommended to use tools to remove BOM in batches, which is safer. At the same time, it is also recommended that you install a professional code editor such as editplus on the server at any time. Do not use Windows Notepad to modify the code.

The above is the detailed content of Solve the problem of ThinkPHP verification code not displaying. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete