For a website, a page that provides verification is required. Now we need to consider the verification code application on the page. Next we will give you a detailed explanation.1. Prepare a page for displaying and submitting the PHP Verification Code
- <?Php
- @ Header ("content-type: text/html;Charset=UTF-8 ");
- // Open the session
- Session_start ();
- ?>
- <Html>
- <Head>
- <Meta Http-equiv="Content-Type" Content="Text/html; charset = UTF-8" />
- <Title>PHP verification code example</Title>
- </Head>
- <Body>
- Verification Code:<Br/>
- <Iframe Id="Iimg" Height="100" Width=300 Src="Img. php" Frameborder="0" ></Iframe>
- <Br/>
- <Input Type=Button Value="Cannot see clearly, change one" Onclick="Iimg. location. reload ();">
- <Br>
- <Form Action="Validate. php" Method="Post">
- Enter the verification code:<Input Name="ImgId" Style="Width: 60">
- <Input Type="Submit" Value="OK">
- </Form>
- </Body>
- </Html>
2. The following is the PHP verification code generation page, which is called by on the first page.
- <?Php
- Header ("Content-type: image/gif ");
- Session_start ();
- // The verification code is a random character. The following is an algorithm.
- $ Randval;
- For ($I=0; $ I<7; $ I ++ ){
- $Randstr=Mt_rand(Ord ('A'), ord ('Z '));
- Srand (double) microtime () * 1000000 );
- $Randv=Mt_rand(0, 10 );
- If ($ randv %2= 0 ){
- $Randval.=Mt_rand(0, 10 );
- } Else {
- $Randval.=Chr($ Randstr );
- }
- }
- // Register the PHP verification code to the session
- Session_register ($ randval );
- // The following figure shows the verification code.
- $Height=50; // Image Height
- $Width=100; // Graph width
- $Im=ImageCreateTrueColor($ Width, $ height );
- $White=ImageColorAllocate($ Instant, 255,255,255 );
- $Blue=ImageColorAllocate($ Im, 0, 0, 64 );
- ImageFill ($ im, 0, 0, $ white );
- Srand (double) microtime () * 1000000000 );
- ImageLine ($ im, mt_rand (0, $ width/3), mt_rand (0, $ height/3), mt_rand ($ width/3, $ width ), mt_rand ($ height/3, $ height), $ blue );
- Srand (double) microtime () * 1000000 );
- ImageLine ($ im, mt_rand ($ width/3, $ width), mt_rand (0, $ height/3), mt_rand (0, $ width/3), mt_rand (0, $ height/3), $ blue );
- Srand (double) microtime () * 1000000 );
- ImageString ($ im, 16, mt_rand (0, $ width-strlen ($ randval) * 10), mt_rand (0, $ height-12), $ randval, $ blue );
- ImageGIF ($ im );
- ImageDestroy ($ im );
- /*
- Note: To support the above plot functions, we must load the GD2 graph processing library in PHP to modify
- ;Extension=Php_gd2. Dll
- Is
- Extension=Php_gd2. Dll
- Enable the GD2 Library
- */
- ?>
3. This is the verification page, prompting whether the PHP Verification Code has passed verification.
- <?Php@ Header ("content-type: text/html;Charset=UTF-8 ");
- // Enable session
- Session_start ();
- // Obtain the verification code entered by the user and convert it to uppercase.
- $ImgId_req= $ _ REQUEST ['imgid'];
- $ImgId_req=Strtoupper($ ImgId_req );
- // Verify whether the session variable is registered for the string
- If (session_is_registered ($ imgId_req )){
- Echo"<Font Color=Blue >Verification passed!</Font>";
- } Else {
- Echo"<Font Color=Red >Verification error!</Font>";
- }
- // Close the session to clear all registered Variables
- Session_destroy ();
- ?>