php實現(xiàn)可運算的驗證碼
更新時間:2015年11月10日 14:22:25 投稿:lijiao
這篇文章主要介紹了php如何實現(xiàn)可運算的驗證碼,代碼很詳細(xì),值得大家學(xué),感興趣的小伙伴們可以參考一下
本文分享了php實現(xiàn)可運算的驗證碼的代碼實例,希望對大家的學(xué)習(xí)有所幫助。
demo.php代碼
<?php
session_start();
if(!emptyempty($_POST['Login'])){
if(md5(strtoupper($_POST['Login'])) == $_SESSION['Login']){
echo 'Correct';
}else{
echo 'Error';
}
}
?>
<form method="post">
<input type="text" name="Login" value="" /><img src="img.php?Action=Login&imgW=80&imgH=30" align="absmiddle">
<input type="submit" value="Login" />
</form>
img.php代碼
<?php
session_start();
/**
* 隨機的數(shù)字,之和驗證碼
* 修改日期 2006-12-20
*/
function getCode ($length = 32, $mode = 0)
{
switch ($mode)
{
case '1':
$str = '123456789';
break;
case '2':
$str = 'abcdefghijklmnopqrstuvwxyz';
break;
case '3':
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
break;
case '4':
$str = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
break;
case '5':
$str = 'ABCDEFGHIJKLMNPQRSTUVWXYZ123456789';
break;
case '6':
$str = 'abcdefghijklmnopqrstuvwxyz1234567890';
break;
default:
$str = 'ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz23456789';
break;
}
$result = '';
$l = strlen($str)-1;
for($i = 0;$i < $length;$i ++)
{
$num = rand(0, $l);
$result .= $str[$num];
}
return $result;
}
//建立驗證圖片
function createAuthNumImg($randStr,$imgW=100,$imgH=40,$fontName)
{
header ("content-type: image/png");
$image = imagecreate($imgW , $imgH);
$color_white = imagecolorallocate($image , 255 , 255 , 255);
$color_gray = imagecolorallocate($image , 228 , 228 , 228);
$color_black = imagecolorallocate($image , 255 , 102 , 204);
for ($i = 0 ; $i < 1000 ; $i++)
{
imagesetpixel($image , mt_rand(0 , $imgW) , mt_rand(0 , $imgH) , $color_gray);
}
imagerectangle($image , 0 , 0 , $imgW - 1 , $imgH - 1 , $color_gray);
for ($i=10;$i<$imgH;$i+=10)
imageline($image, 0, $i, $imgW, $i, $color_gray);
imagettftext($image,16,5,3,25,$color_black,$fontName,$randStr);
for ($i=10;$i<$imgW;$i+=10)
imageline($image, $i, 0, $i, $imgH, $color_gray);
imagepng($image);
imagedestroy($image);
}
$a=GetCode(1,1);
$b=GetCode(1,1);
$c=GetCode(1,1);
$Passport=$a."+".$b."+".$c;
$Total=$a+$b+$c;
$Total;
$_SESSION[$_GET['Action']]=md5(strtoupper($Total));
createAuthNumImg($Passport,$_GET['imgW'],$_GET['imgH'],"verdana.ttf");
?>
以上就是為大家介紹的php可運算的驗證碼全部代碼,希望對大家的學(xué)習(xí)有所幫助。
您可能感興趣的文章:
- php常用數(shù)學(xué)函數(shù)匯總
- 第四章 php數(shù)學(xué)運算
- PHP幾個數(shù)學(xué)計算的內(nèi)部函數(shù)學(xué)習(xí)整理
- php 數(shù)學(xué)運算驗證碼實現(xiàn)代碼
- 簡單談?wù)刾hp浮點數(shù)精確運算
- 非常全面的php日期時間運算匯總
- ThinkPHP自定義函數(shù)解決模板標(biāo)簽加減運算的方法
- php對文件進行hash運算的方法
- php中的比較運算符詳解
- (PHP實現(xiàn))只使用++運算實現(xiàn)加法,減法,乘法,除法
- PHP常用特殊運算符號和函數(shù)總結(jié)(php新手入門必看)
- PHP數(shù)學(xué)運算函數(shù)大匯總(經(jīng)典值得收藏)
相關(guān)文章
php空間不支持socket但支持curl時recaptcha的用法
php空間不支持socket但支持curl時recaptcha的用法,需要的朋友可以參考下。2011-11-11
php array_walk() 數(shù)組函數(shù)
函數(shù)array_walk():單一數(shù)組回調(diào)函數(shù)---對數(shù)組中的每個成員應(yīng)用用戶函數(shù)2011-07-07
PHP簡單實現(xiàn)“相關(guān)文章推薦”功能的方法
這篇文章主要介紹了PHP簡單實現(xiàn)“相關(guān)文章推薦”功能的方法,方法簡單功能實用,需要的朋友可以參考下2014-07-07
PHP學(xué)習(xí)筆記 (1) 環(huán)境配置與代碼調(diào)試
學(xué)習(xí)php第一步就是需要配置php運行環(huán)境,這個是基礎(chǔ),需要的朋友可以參考下。2011-06-06

