When we want to useAdd a line of reference in php. ini: extension = php_gd2.dll
Restart apache. Make a test page var_dump (gd_info (). The output data indicates that the php gd library has been referenced successfully.
Table ticket auth.html
- <Html>
- <Head>
- <Meta Http-equiv='Content-type' Content='Text/html; charset = UTF-8'>
- <Title>Verification Code</Title>
- </Head>
- <Body>
- <H1>Enter Verification Code</H1>
- <Form Action="Check_auth.php" Method="Post">
- <Input Name="Auth" Type="Text">
- <Img Src="Auth. php" Border="0" />
- <Input Type="Submit" Value="Submit">
- </Form>
- </Body>
- </Html>
Php gd library to generate Verification Code auth. php
- <?Php
- Session_start ();
- Header ("Content-type: image/png ");
-
- $Img_width=100;
- $Img_height=20;
-
- Srand (microtime () * 100000 );
- For ($I=0; $ I<4; $ I ++)
- {
- $New_number.=Dechex(Rand (0, 15 ));
- }
-
- $ _ SESSION [check_auth] = $ new_number;
- $New_number=ImageCreate($ Img_width, $ img_height); // create an image
- ImageColorAllocate ($ new_number, 255,255,255); // set the background color to white.
-
- For ($I=0; $ I<Strlen($ _ SESSION [check_auth]); $ I ++)
- {
- $Font=Mt_rand(3, 5 );
- $X=Mt_rand(1, 8) + $ img_width * $ I/4;
- $Y=Mt_rand(1, $ img_height/4 );
- $Color=ImageColorAllocate($ New_number, mt_rand (0,100), mt_rand (0,150), mt_rand (0,200); // set the Character Color
- ImageString ($ new_number, $ font, $ x, $ y, $ _ SESSION [check_auth] [$ I], $ color); // output character
- }
-
- ImagePng ($ new_number );
- ImageDestroy ($ new_number );
- ?>
Php gd library submission page check_auth.php
- <?Php
- Session_start ();
- $Auth= $ _ POST ['auth '];
-
- If (empty ($ auth ))
- {
- Echo 'error: The Verification Code cannot be blank ';
- Die;
- }
-
- If ($Auth==$ _ SESSION ['check _ auth'])
- {
- Echo 'true ';
- }
- Else
- {
- Echo 'error: Incorrect verification code input ';
- }
- ?>
The above is the knowledge about generating verification codes in the php gd library described in this article. I hope it will be helpful to you.