PHP instance: use PHP to implement form verification code login verification

Source: Internet
Author: User
Principle: create an image and keep the characters displayed in the image to the SESSION. when logging on, determine whether the entered verification code is the same as the verification code in the SESSION. this is a natural verification code and the checkNumber of the image file. php? Phpsession_start (); if ($ act = "init ")

Principle: A natural image is generated, and the characters displayed in the image are retained to the SESSION. when logging on, you can determine whether the entered verification code is the same as the verification code in the SESSION.

This is the checkNumber. php file of the inherent verification code and image.

Session_start ();
If ($ act = 'init ')
{
Header ('content-type: image/png ');
Srand (microtime () * 100000 );
$ Login_check_number = strval (rand ('20140901', '20160901 '));
Session_register ('login _ check_number ');
// Here the SESSION is applied to reserve the verification code.
// You can also use cookies.
// Setcookie ('login _ check_number ', $ login_check_number );
// Delete session_start () in the first line;
// The application COOKIE is not recommended because the application COOKIE cannot be used for security verification.
$ H_img = imagecreate (40, 17 );
$ C_black = ImageColorAllocate ($ h_img, 0, 0 );
$ C_white = ImageColorAllocate ($ h_img, 255,255,255 );
Imageline ($ h_img, 1, 1,350, 25, $ c_black );
Imagearc ($ h_img, 200, 15, 20, 20, 35,190, $ c_white );
Imagestring ($ h_img, 5, 2, 1, $ login_check_number, $ c_white );
ImagePng ($ h_img );
ImageDestroy ($ h_img );
Die ();
}
/*
Application method:
In the HTML file Enter the following code on the login verification PHP page (note: No output is allowed before the code is added, because the SESSION is applied)
// $ Number indicates the value of the verification code you entered.
Include_once ('./checkNumber. php ');
// Check the verification code
If ($ number! = $ Login_check_number | empty ($ number ))
{
Print ('check code is not accurate! ');
Die ();
}
*/
?>

Application SESSION Title:

If the logon page is opened and the logon fails after the SESSION expires, the logon fails.

Application COOKIE Title:

The COOKIE is retained by the customer, so it is better not to use it if the COOKIE is applied.

For the VBB Forum, after the checkNumber. php is retained, modify the template content of the MYSQL database template data table titled logincode forumhome_logincode username_loggedout.

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.