PHP驗證碼生這個功能常用於用用戶注冊登錄或發布信息時一個安全驗證的基本功能,下面小編來給大家介紹一些常用的PHP驗證碼生成代碼與應用實例。
例1,直接使用數組,此方法比較簡單
代碼如下 復制代碼<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","中","國","南","北","大","小","多","少");
$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>(以上語句另存為一個php文件)
<?php
header(“Content-Type:text/html;charset=utf-8");
echo $_POST["yanzhengma"];
echo $_POST["code"];
if($_POST["yanzhengma"]==$_POST["code"])
{
echo “驗證碼正確";
}
else
{
die(“<script>alert(‘驗證碼不正確");location="array_rand.php";</script>");
}
?>
(以上語句另存為yz.php)
例2,也是用數組只不是數據多了一點
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 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('12345678901');
?>
例3,這個是一個比較完整的ajax刷新驗證碼實例
vcode.php
<?php
session_start();//開啟session功能
header("Cache-Control: no-cache, must-revalidate");
$im = imagecreate(60,30);//定義圖片寬度和高度
$vcode=getVCode();//獲取要顯示的字符
$bg = imagecolorallocate($im, 255, 255, 255);//定義圖片背景
$txt = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));//定義要顯示字符的顏色
imagestring($im, 8, 0, 0, $vcode, $txt);//寫入字符串到圖片
header(Content-type: image/jpeg);//定義Content-type
imagejpeg($im);//以JPEG格式顯示圖片
$_SESSION[vcode]=$vcode;//寫入SESSION
function getVCode(){ //隨機生成用戶指定個數的字符串
$codenum=4;
$checkcode="";
$string="";//要顯示的可選字符串,請自行定義;
for($i=0;$i<$codenum;$i ) {
$number=rand(0,2);
switch($number){ //根據可選字符串可靈活定義;
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
<!--詳細信息-->
<form name="loginform">
<table class="dtable">
<tr>
<td width="100"> 用戶名 </td><td><input class="txtbox" name="loginname" type=text size="30"/></td>
</tr>
<tr>
<td width="100"> 密碼 </td><td><input class="txtbox" name="loginpwd" type=password size="30"/></td>
</tr>
<tr>
<td width="100"> 驗證碼 </td>
<td><input class="txtbox" name="loginvcode" type=text size="10"/>
<img id="vcode" src="vcode.php" alt="驗證碼" align="absmiddle"/>
<a href="javascript:getVCode();">換一張</a></td>
</tr>
</table>
<table>
<tr><td colspan="2">
<input class="btn" name="ok" type="button" value="登錄" onclick="setType('usr');usrVCode();">
<input class="btn" name="reset" type="reset" value="重寫">
<input class="btn" name="exit" type="button" value="退出" onclick="Hide();">
</td></tr>
</table>
<table>
<tr><td colspan="2">
還沒有注冊? <a href="javascript:setType('usr');Show('0','addform');">馬上注冊</a>
忘記密碼? <a href="javascript:setType('usr');Show('0','pwdform');">取回密碼</a>
</td></tr>
</table>
</form>
vcode.js
代碼如下 復制代碼//該函數用來獲取驗證碼
function getVCode() {
var vcode=document.getElementById('vcode');
vcode.src = 'vcode.php?nocache='+new Date().getTime();
}
//該函數用來驗證驗證碼
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)?"恭喜您,驗證碼輸入正確!":"很抱歉,驗證碼輸入錯誤!";
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;
}
}
}
}