PHP Verification Code Generation Program Methods

Source: Internet
Author: User
Tags php foreach

PHP verification code generation is often used to provide a basic security verification function when users register for Logon or publish information. The following section describes some common PHP Verification Code Generation Code and application instances.

Example 1: Use arrays directly. This method is relatively simple.

The Code is as follows: Copy code

<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
</Head>
<Body>
<? Php

$ Arr = array (2, 3, 4, 5, 8, 1, 9, 7, "a", "B", "c", "d", "e", "f ", "medium", "country", "South", "North", "big", "small", "more", "less ");
$ B = array_rand ($ arr, 3 );

?>
<Form action = "yz. php" method = "post">
<Input type = "text" name = "code"/>
<Input type = "hidden" name = "yanzhengma" value = "<? Php foreach ($ B as $ key)
{
Echo $ arr [$ key];
}
?> "
/>

<Input type = "submit" name = "submit" value = "do"/>
</Form>
<? Php

Echo "code :";
Foreach ($ B as $ key)
{
Echo $ arr [$ key];
}
?>

</Body>
</Html> (Save the preceding statement as a PHP file)

<? Php
Header ("Content-Type: text/html; charset = UTF-8 ");
Echo $ _ POST ["yanzhengma"];
Echo $ _ POST ["code"];
If ($ _ POST ["yanzhengma"] == _ POST ["code"])
{
Echo "the verification code is correct ";
}
Else
{
Die ("<script> alert ('incorrect Verification Code"); location = "array_rand.php"; </script> ");
}
?>

(Save the preceding statement as yz. php)


Example 2: Use arrays instead of a little more data.

The Code is as follows: Copy code

Function UPCAbarcode ($ code ){
$ Lw = 2; $ hi = 100;
$ Lencode = array ('20140901', '20160901', '20160901', '20160301', '20160301 ',
'123', '123', '123', '123', '123 ');
$ Rencode = array ('20140901', '20160901', '20160901', '20160301', '20160301 ',
'123', '123', '123', '123', '123 ');
$ Ends = '20180101'; $ center = '20180101 ';
/* UPC-A Must be 11 digits, we compute the checksum .*/
If (strlen ($ code )! = 11) {die ("UPC-A Must be 11 digits .");}
/* Compute the EAN-13 Checksum digit */
$ Ncode = '0'. $ code;
$ Even = 0; $ odd = 0;
For ($ x = 0; $ x <12; $ x ++ ){
If ($ x % 2) {$ odd + = $ ncode [$ x];} else {$ even + = $ ncode [$ x];}
}
$ Code. = (10-($ odd * 3 + $ even) % 10) % 10;
/* Create the bar encoding using a binary string */
$ Bars = $ ends;
$ Bars. = $ Lencode [$ code [0];
For ($ x = 1; $ x <6; $ x ++ ){
$ Bars. = $ Lencode [$ code [$ x];
}
$ Bars. = $ center;
For ($ x = 6; $ x <12; $ x ++ ){
$ Bars. = $ Rencode [$ code [$ x];
}
$ Bars. = $ ends;
/* Generate the Barcode Image */
$ Img = ImageCreate ($ lw * 95 + 30, $ hi + 30 );
$ Fg = ImageColorAllocate ($ img, 0, 0, 0 );
$ Bg = ImageColorAllocate ($ img, 255,255,255 );
ImageFilledRectangle ($ img, 0, 0, $ lw * 95 + 30, $ hi + 30, $ bg );
$ Shift = 10;
For ($ x = 0; $ x if ($ x <10) | ($ x >=45 & $ x <50) | ($ x> = 85) {$ sh = 10;} else {$ sh = 0 ;}
If ($ bars [$ x] = '1') {$ color = $ fg;} else {$ color = $ bg ;}
ImageFilledRectangle ($ img, ($ x * $ lw) + 15, 5, ($ x + 1) * $ lw + 14, $ hi + 5 + $ sh, $ color );
}
/* Add the Human Readable Label */
ImageString ($ img, 4,5, $ hi-5, $ code [0], $ fg );
For ($ x = 0; $ x <5; $ x ++ ){
ImageString ($ img, 5, $ lw * (13 + $ x * 6) + 15, $ hi + 5, $ code [$ x + 1], $ fg );
ImageString ($ img, 5, $ lw * (53 + $ x * 6) + 15, $ hi + 5, $ code [$ x + 6], $ fg );
}
ImageString ($ img, 4, $ lw * 95 + 17, $ hi-5, $ code [11], $ fg );
/* Output the Header and Content .*/
Header ("Content-Type: image/png ");
ImagePNG ($ img );
}

UPCAbarcode ('20140901 ');

?>

Example 3: A complete ajax refresh Verification Code instance


Vcode. php

The Code is as follows: Copy code

<? Php
Session_start (); // enable the session Function
Header ("Cache-Control: no-cache, must-revalidate ");

$ Im = imagecreate (60, 30); // you can specify the image width and height.
$ Vcode = getVCode (); // get the characters to be displayed
$ Bg = imagecolorallocate ($ im, 255,255,255); // define the image background
$ Txt = imagecolorallocate ($ im, rand (0,255), rand (0,255), rand (0,255); // defines the color of the characters to be displayed
Imagestring ($ im, 8, 0, 0, $ vcode, $ txt); // write a string to the image
Header (Content-type: image/jpeg); // define Content-type
Imagejpeg ($ im); // display images in JPEG format
$ _ SESSION [vcode] = $ vcode; // write to the SESSION
 
Function getVCode () {// randomly generates a specified number of strings.
$ Codenum = 4;
$ Checkcode = "";
$ String = ""; // optional string to be displayed, Please define it yourself;
For ($ I = 0; $ I <$ codenum; $ I ){
$ Number = rand (0, 2 );
Switch ($ number) {// can be flexibly defined based on optional strings;
Case 0: $ rand_number = rand (0, 10); break;
Case 1: $ rand_number = rand (11, 36); break;
Case 2: $ rand_number = rand (37,62); break;
}
$ Code = substr ($ string, $ rand_number, 1 );
$ Checkcode = $ checkcode. $ code;
}
Return $ checkcode;
}
?>


Loginform.html

The Code is as follows: Copy code


<! -- Details -->
<Form name = "loginform">
<Table class = "dtable">
<Tr>
<Td width = "100"> User name </td> <input class = "txtbox" name = "loginname" type = text size = "30"/> </ td>
</Tr>
<Tr>
<Td width = "100"> password </td> <input class = "txtbox" name = "loginpwd" type = password size = "30"/> </ td>
</Tr>
<Tr>
<Td width = "100"> Verification Code </td>
<Td> <input class = "txtbox" name = "loginvcode" type = text size = "10"/>

<A href = "javascript: getVCode ();"> change one </a> </td>
</Tr>
</Table>
<Table>
<Tr> <td colspan = "2">
<Input class = "btn" name = "OK" type = "button" value = "login" onclick = "setType ('usr'); usrVCode ();">
<Input class = "btn" name = "reset" type = "reset" value = "Rewrite">
<Input class = "btn" name = "exit" type = "button" value = "exit" onclick = "Hide ();">
</Td> </tr>
</Table>
<Table>
<Tr> <td colspan = "2">
Not registered yet? <A href = "javascript: setType ('usr'); Show ('0', 'addform');"> Register now </a>
Forgot password? <A href = "javascript: setType ('usr'); Show ('0', 'pwdform');"> retrieve password </a>
</Td> </tr>
</Table>
</Form>

Vcode. js

The Code is as follows: Copy code

// This function is used to obtain the verification code.

Function getVCode (){
Var vcode = document. getElementById ('vcode ');
Vcode. src = 'vcode. php? Nocache = '+ new Date (). getTime ();
}

// This function is used to verify the verification code
Function usrVCode (){
If (! CheckLogin () return false;
Var loginvcode = document. loginform. loginvcode. value;
Var xmlhttp1 = createAjax ();
Var data = '& loginvcode =' + loginvcode;
If (xmlhttp1 ){
Var state = document. getElementById ('state ');
Xmlhttp1.open ('get ',? Do = vcodedo '+ data, true );
Xmlhttp1.send (null );
Xmlhttp1.onreadystatechange = function (){
If (xmlhttp1.readyState = 4 & xmlhttp1.status = 200 ){
SetTimeout ("state. style. display = 'none';", 1000 );
Var myres = xmlhttp1.responseText;
Var result = (myres = 1 )? "Congratulations, the verification code is entered correctly! ":" Sorry, incorrect verification code! ";
If (myres = 0) alert (result );
If (myres = 1) usrLogin ();
}
Else {
State. style. display = "";
State. style. left = (document. body. offsetWidth-350)/2;
State. style. top = (document. body. offsetHeight-235)/2 + document. body. scrollTop;
}
}
}
}

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.