PHP verification code implementation code _ PHP Tutorial

Source: Internet
Author: User
PHP verification code. The checkcode. php generates the verification code image and the variable $ _ SESSION [check_pic]. Copy the code as follows :? Session_start (); for ($ i0; $ i4; $ I ++) {$ rand. dechex (rand (1, 15);} $ _ SESSIO checkcode. php generates a verification code image with the variable $ _ SESSION [check_pic].

The code is as follows:


Session_start ();
For ($ I = 0; $ I <4; $ I ++ ){
$ Rand. = dechex (rand (1, 15 ));
}
$ _ SESSION [check_pic] = $ rand;
// Echo $ _ SESSION [check_pic];
// Set the image size
$ Im = imagecreatetruecolor (100,30 );
// Set the color
$ Bg = imagecolorallocate ($ im, 0, 0 );
$ Te = imagecolorallocate ($ im, 255,255,255 );
// Write the string in the upper left corner of the image
Imagestring ($ im, rand (5, 6), rand (25, 30), 5, $ rand, $ te );
// Output image
Header ("Content-type: image/jpeg ");
Imagejpeg ($ im );
?>


Form. php
Verification code image generated by calling

The code is as follows:



Message








Imagestring ($ im, rand (5, 6), rand (25, 30), 5, $ rand, $ te); int imagestring (int im, int font, int x, int y, string s, int col); function, which is used to draw a horizontal string.
This function draws horizontal strings on the image. The font parameter is a font. if it is set to 1 to 5, the default font is used. The x and y parameters are the starting coordinates of the string. The content of the string is placed on parameter s. The col parameter indicates the color of the string.
Post. php
Compare $ _ POST [check] and $ _ SESSION [check_pic]. if they are the same, insert the database. If they are not equal, the previous page is returned.

The code is as follows:


Session_start ();
If (isset ($ _ POST [check])
{
If ($ _ POST [check] === _ SESSION [check_pic])
{
// Echo "the verification code is correct". $ _ SESSION [check_pic];
Require ("dbinfo. php ");
$ Name = $ _ POST ['name'];
$ Email = $ _ POST ['email '];
$ Website = $ _ POST ['Website'];
$ Content = $ _ POST ['content'];
$ Date = date ("Y-m-d h: m: s ");
// Connect to the MySQL server
$ Connection = mysql_connect ($ host, $ username, $ password );
If (! $ Connection)
{
Die ('not connected: '. mysql_error ());
}
// Set the active MySQL database
$ Db_selected = mysql_select_db ($ database, $ connection );
If (! $ Db_selected)
{
Die ('Can \'t use db: '. mysql_error ());
}
// Insert data into the database
$ Query = "insert into table (nowamagic_name, nowamagic_email, region, nowamagic_content, nowamagic_date) values ('$ name',' $ email ',' $ website ',' $ content ', '$ date ')";
$ Result = mysql_query ($ query );
If ($ result)
{
Echo "script" alert ('submitted successfully'); history. go (-1); script ";
}
If (! $ Result)
{
Die ('invalid query: '. mysql_error ());
}
}
Else
{
Echo "script" alert ('verification code error'); history. go (-1); script ";
}
}
?>

The http://www.bkjia.com/PHPjc/323975.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/323975.htmlTechArticlecheckcode.php generates a verification code image with the variable $ _ SESSION [check_pic]. The code is as follows :? Session_start (); for ($ I = 0; $ i4; $ I ++) {$ rand. = dechex (rand ();} $ _ SESSIO...

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.