PHP Verification Code Generation program several methods _php tutorial

Source: Internet
Author: User
PHP Authentication Code Students This function is often used to sign in or publish information with the user a basic function of security verification, the following small part to introduce some common PHP code generation codes and application examples.

Example 1, using arrays directly, this method is relatively simple

code as follows copy code






!--? php

$arr =array (2,3,4,5,8,1,9,7," a "," B "," C "," D "," E "," F "," Zhong "," Country "," South " , "North", "Big", "small", "more", "less");
$b =array_rand ($arr, 3);

?

!--? php

Echo "code:";
foreach ($b as $key)
{
echo $arr [$key];
}
?


(Save the above 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 "captcha is correct";
}
Else
{
Die ("");
}
?

(Save the above statement as yz.php)


Example 2, but also with the array is not the data a little more

The code is as follows Copy Code

function Upcabarcode ($code) {
$LW = 2; $hi = 100;
$Lencode = Array (' 0001101 ', ' 0011001 ', ' 0010011 ', ' 0111101 ', ' 0100011 ',
' 0110001 ', ' 0101111 ', ' 0111011 ', ' 0110111 ', ' 0001011 ');
$Rencode = Array (' 1110010 ', ' 1100110 ', ' 1101100 ', ' 1000010 ', ' 1011100 ',
' 1001110 ', ' 1010000 ', ' 1000100 ', ' 1001000 ', ' 1110100 ');
$ends = ' 101 '; $center = ' 01010 ';
/* upc-a must be one digits, we compute the checksum. */
if (strlen ($code)! = one) {die ("upc-a must is one 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. = (($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 [one], $FG);
/* Output the Header and Content. */
Header ("Content-type:image/png");
Imagepng ($IMG);
}

Upcabarcode (' 12345678901 ');

?>

Example 3, this is a more complete Ajax refresh Captcha instance


vcode.php

The code is as follows Copy Code

!--? php
session_start ();//Open Session function
Header ("Cache-control:no-cache, must-revalidate");

$im = Imagecreate (60,30);//define picture width and height
$vcode =getvcode ();//Gets the character to display
$BG = Imagecolorallocate ($im, 255, 255, 255);//define Picture background
$txt = Imagecolorallocate ($im, Rand (0,255), Rand (0,255), Rand (0,255));//define the color to display characters
Imagestring ($im, 8, 0, 0, $vcode, $txt);//write String to Picture
Header (content-type:image/jpeg);//Definition Content-type
Imagejpeg ($im);//display pictures in JPEG format
$_session[vcode]= $vcode;//write SESSION

function Getvcode () {//randomly generate a user-specified number of strings
$codenum = 4;
$checkcode = "";
$string = "";//the 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 an optional string;
Case 0: $rand _number=rand (0,10);
Case 1: $rand _number=rand (11,36);
Case 2: $rand _number=rand (37,62);
}
$code =substr ($string, $rand _number,1);
$checkcode = $checkcode. $code;
}
return $checkcode;
}
?>


Loginform.html

The code is as follows Copy Code



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, the CAPTCHA was entered incorrectly! ";
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;
}
}
}
}

http://www.bkjia.com/PHPjc/632832.html www.bkjia.com true http://www.bkjia.com/PHPjc/632832.html techarticle PHP Authentication Code Students This function is often used to sign in or publish information with the user a basic function of security verification, the following small part to introduce some of the commonly used PHP verification code generation codes and ...

  • 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.