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 ();
}