The picture verification code developed by PHP fails to display

王林
Release: 2023-04-07 20:00:01
Original
2083 people have browsed it

The picture verification code developed by PHP fails to display

1. Install the gd module

In the window environment, you only need to find ";extension=php_gd2.dll" in the php.ini file. Just remove the semicolon in front of it. After restarting apache, you can find the gd information in phpinfo().

If you are in the ubuntu environment, you need to install the gd module. The command is

sudo apt-get install php5-gd
Copy after login

After installation, you can also see the above module.

2. Confirm the encoding of the code file

If it is utf-8 with BOM, it needs to be changed to a BOM-free format.

3. Use ob_clean() to clear the cache.

The function of ob_clean is to discard the output buffer. If you have many generated image files, you need to clear the buffer frequently if you want to access them correctly.

public function create()
    {
        ob_clean();
        $a = new verify();
        session::set('captcha', strtolower($a->create(4, 15)));
        view::assign("captcha", $a);
        view::display("captcha");
        exit(0);
    }
Copy after login

4. PHP version problem

If you use a higher PHP version, you cannot use undefined variables, otherwise a notice error will be reported, $ New_number and $_SESSION['check_checks'] must be judged by isset before use, or if you use old code, add error_reporting(E_ALL & ~E_NOTICE); to shield the notice error (not recommended) ),
So use isset() to check to make sure it is defined.

If it’s not solved?

Through debugging, I found that the verification code was not displayed because the font file could not be found.

$font="simhei.ttf"
Copy after login

is written as a relative path, and the font file needs to be placed in the project. Or write the absolute path to the system font file.
I put it in the lib folder:

$font=LIB_PATH."simhei.ttf"
Copy after login

Recommended tutorial: PHP tutorial

The above is the detailed content of The picture verification code developed by PHP fails to display. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template