PHP Example: Use PHP to implement form verification code login check

Source: Internet
Author: User
Tags implement php example first row mysql database

Principle: Generate a picture, and save the characters displayed in the picture to the session. When landing to determine whether the input check code and the session commander of the same code.

This is the file that generates the checksum code and the picture checknumber.php

<?php
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");
Here is the session used to save the checksum code.
Of course, you can use cookies.
Setcookie ("Login_check_number", $login _check_number);
Then delete the first row of session_start ();
Cookies are not recommended because it is not safe to authenticate with cookies.
$h _img = imagecreate (40,17);
$c _black = imagecolorallocate ($h _img, 0,0,0);
$c _white = imagecolorallocate ($h _img, 255,255,255);
Imageline ($h _img, 1, 1, _black, $c);
Imagearc ($h _img, M, 190, $c _white);
Imagestring ($h _img, 5, 2, 1, $login _check_number, $c _white);
Imagepng ($h _img);
Imagedestroy ($h _img);
Die ();
}
/*
How to use:
Add the following code to the Login verification php page by adding <input Type=text name=number to the HTML file maxlength=4> (Note: cannot have output before adding code because session is used)
$number is the value of the checksum code you entered.
Include_once ("./checknumber.php");
Check-Check Code
if ($number!= $login _check_number | | Empty ($number))
{
Print ("Incorrect check code!");
Die ();
}
*/
?>

Problem with session:

If you open the landing page in the session after the expiration of the landing, then the landing will fail.

Problem with cookies:

Cookies are stored on the client, so you might as well not use cookies.

For VBB Forum. After saving the checknumber.php. Modify the MySQL database template The title of the datasheet is Logincode Forumhome_logincode username_loggedout Content.

And then in Member.php's

if ($action = = "Login")

After joining

Check-Check Code
Include_once ("./checknumber.php");
if ($number!= $login _check_number | | $number = = "")
{
Print ("Incorrect check 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.