Use PHP to implement the login verification code.

Source: Internet
Author: User
A few days ago saw the DEV-CLUB implementation of the verification code login verification. we implemented it in PHP last night. welcome to discuss with me the principles of polygame@163.net: generate an image and save the characters shown in the image to the SESSION. check whether the entered verification code is the same as the verification code in the SESSION during login. demo address: www. bingdu. netbbs: this is the verification code generated.

A few days ago saw the DEV-CLUB implementation of the verification code login verification. last night in PHP to achieve. welcome everyone and I discuss polygame@163.net

Principle: generate an image and save the characters displayed in the image to the SESSION. when logging in, determine whether the entered verification code is the same as the verification code in the SESSION.
Demo address:

Http://www.bingdu.net/bbs/

This is the checkNumber. php file that generates the verification code and image.

Session_start ();

If ($ act = "init ")
{
Header ("Content-type: image/png ");
Srand (microtime () * 100000 );
$ Login_check_number = strval (rand ("1111", "9999 "));

Session_register ("login_check_number ");
// The SESSION is used to save the verification code.
// You can also use cookies.
// Setcookie ("login_check_number", $ login_check_number );
// Delete session_start () in the first line;
// COOKIE is not recommended because it 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 ();
}
/*
Usage:
Add

Add the following code to the login verification PHP page (note: No output is allowed before the code is added because SESSION is used)

// $ 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 ("incorrect verification code! ");
Die ();
}
*/
?>

SESSION usage problems:
If the logon page is opened and the logon fails after the SESSION expires, the logon fails.
COOKIE usage problems:
The COOKIE is stored on the client, so it is better not to use the COOKIE.

For the VBB Forum, save checkNumber. php and modify the template content of the MYSQL database template data table titled logincode forumhome_logincode username_loggedout.
Then in the member. php
If ($ action = "login ")
Add later

// Check the verification code
Include_once ("./checkNumber. php ");
If ($ number! = $ Login_check_number | $ number = "")
{
Print ("incorrect verification code! ");
Die ();
}

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.