Use php to generate a text verification code on the server (2 ),
Implementation principle of CAPTCHA:
1) Prepare some images and store them in the database. Each image corresponds to an ID field.
2): the image and the identification field are combined in the form of arrays on the server side.
3): Randomly returns an image to the client and accepts the fields entered by the user.
4): Compares user input fields with the information stored on the server with each image.
5): If it is the same, it passes. Otherwise, it fails.
A) The first step is to generate a verification code.
CreateCode. php
<? Php header ("content-type: text/html; charset = UTF-8"); session_start (); // enable server storage $ table = array ('pic0' => "cat", 'pic1' => 'Dog', 'pic2' => 'Monkey ', 'pic3' => 'fish'); $ index = rand (); // generate a random number $ value = $ table ['pic '. $ index]; // obtain the random value $ _ SESSION ['authcode'] = $ value; // store the value on the server side $ filename = dirname (_ FILE __). '\ pic'.w.index.'.jpg'; // address of the file $ content = file_get_contents ($ filename); // read the file content ob_clean (); // clear the cache header ("content-type: image /Jpg "); // specifies to output the image echo $ contents in jpg format; // output the image file?>
B) Use the verification code
UseCode. php:
<? Php header ("content-type: text/html; charset = UTF-8"); if (isset ($ _ REQUEST ["authcode"]) {session_start (); if ($ _ SESSION ['authcode'] == _ REQUEST ['authcode']) {// $ _ REQUEST ['authcode'] .. obtain the input box value echo "<font> Verification passed </font>" ;}else {echo "<font> Verification Failed </font> ";} exit () ;}?>
<! DOCTYPE html>
Fact: