Use php to generate a text verification code on the server (2 ),

Source: Internet
Author: User

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:

 

 

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.