php實現驗證碼(數字、字母、漢字)

來源:互聯網
上載者:User

標籤:height   oid   type   oca   頭部   color   sub   江湖   擷取   

驗證碼在表單實現越來越多了,但是用js的寫的驗證碼,總覺得不方便,所以學習了下php實現的驗證碼。好吧,其實是沒有事情幹,但是又不想浪費時間,所以學習了下php實現驗證碼。正所謂,技多不壓身。而且,也可以封裝成一個函數,以後使用的時候也是很方便的,當然現在未封裝。

  現在來說說簡單的純數字驗證碼吧。

    如果是初學者,建議按照我代碼的注釋 //數字  一步步來。最簡單的方法,還是把整個代碼複製走了。

    建立一個captcha.php:

<?php
//10>設定session,必須處於指令碼最頂部
session_start();

$image = imagecreatetruecolor(100, 30); //1>設定驗證碼圖片大小的函數
//5>設定驗證碼顏色 imagecolorallocate(int im, int red, int green, int blue);
$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff
//6>地區填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的地區著色,col 表示欲塗上的顏色
imagefill($image, 0, 0, $bgcolor);
//10>設定變數
$captcha_code = "";
//7>產生隨機數字
for($i=0;$i<4;$i++){
//設定字型大小
$fontsize = 6;
//設定字型顏色,隨機顏色
$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深顏色
//設定數字
$fontcontent = rand(0,9);
//10>.=連續定義變數
$captcha_code .= $fontcontent;
//設定座標
$x = ($i*100/4)+rand(5,10);
$y = rand(5,10);

imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
//10>存到session
$_SESSION[‘authcode‘] = $captcha_code;
//8>增加幹擾元素,設定雪花點
for($i=0;$i<200;$i++){

//設定點的顏色,50-200顏色比數字淺,不干擾閱讀
$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));
//imagesetpixel — 畫一個單一像素
imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);
}
//9>增加幹擾元素,設定橫線
for($i=0;$i<4;$i++){
//設定線的顏色
$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220));
//設定線,兩點一線
imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor);
}

//2>設定頭部,image/png
header(‘Content-Type: image/png‘);
//3>imagepng() 建立png圖形函數
imagepng($image);
//4>imagedestroy() 結束圖形函數 銷毀$image
imagedestroy($image);

php?>

 

接著就是靜態頁的代碼了:index.html

doctype html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>確認驗證碼title>
head>
<body>
<form method="post" action="./form.php">
<p>驗證碼: <img id="captcha_img" border=‘1‘ src=‘./captcha.php?r=echo rand(); ?>‘ style="width:100px; height:30px" />
<a href="javascript:void(0)" onclick="document.getElementById(‘captcha_img‘).src=‘./captcha.php?r=‘+Math.random()">換一個?a>
p>
<P>請輸入驗證碼:<input type="text" name=‘authcode‘ value=‘‘/>p>
<p><input type=‘submit‘ value=‘提交‘ style=‘padding:6px 5px;‘/>p>
body>
html>

 

從index.html可以看到,提交的表單是到form.php的,所以還要有一個判斷的form.php代碼:

<?php
header("Content-Type:text/html;charset=utf-8"); //設定頭部資訊
//isset()檢測變數是否設定
if(isset($_REQUEST[‘authcode‘])){
session_start();
//strtolower()小寫函數
if(strtolower($_REQUEST[‘authcode‘])== $_SESSION[‘authcode‘]){
//跳轉頁面
echo "<script language=\"javascript\">";
echo "document.location=\"./form.php\"";
echo "</script>";
}else{
//提示以及跳轉頁面
echo "<script language=\"javascript\">";
echo "alert(‘輸入錯誤!‘);";
echo "document.location=\"./form.php\"";
echo "</script>";
}
exit();
}

php?>

那麼,純數位實現了,數字加英文的也應該不難了。要修改的代碼   只是在 captcha.php 將 //7>產生隨機數字 修改成  //7>產生隨機的字母和數字,如果你真的很可愛的就修改這幾個字就認為可以實現的話,那麼祝賀你,你永遠保持快樂。腦殘兒童歡樂多。

  廢話不多說了,拉代碼吧。

<?php
//10>設定session,必須處於指令碼最頂部
session_start();

$image = imagecreatetruecolor(100, 30); //1>設定驗證碼圖片大小的函數
//5>設定驗證碼顏色 imagecolorallocate(int im, int red, int green, int blue);
$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff
//6>地區填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的地區著色,col 表示欲塗上的顏色
imagefill($image, 0, 0, $bgcolor);
//10>設定變數
$captcha_code = "";
//7>產生隨機的字母和數字
for($i=0;$i<4;$i++){
//設定字型大小
$fontsize = 8;
//設定字型顏色,隨機顏色
$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深顏色
//設定需要隨機取的值,去掉容易出錯的值如0和o
$data =‘abcdefghigkmnpqrstuvwxy3456789‘;
//取出值,字串截取方法 strlen擷取字串長度
$fontcontent = substr($data, rand(0,strlen($data)),1);
//10>.=連續定義變數
$captcha_code .= $fontcontent;
//設定座標
$x = ($i*100/4)+rand(5,10);
$y = rand(5,10);

imagestring($image,$fontsize,$x,$y,$fontcontent,$fontcolor);
}
//10>存到session
$_SESSION[‘authcode‘] = $captcha_code;
//8>增加幹擾元素,設定雪花點
for($i=0;$i<200;$i++){
//設定點的顏色,50-200顏色比數字淺,不干擾閱讀
$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));
//imagesetpixel — 畫一個單一像素
imagesetpixel($image, rand(1,99), rand(1,29), $pointcolor);
}
//9>增加幹擾元素,設定橫線
for($i=0;$i<4;$i++){
//設定線的顏色
$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220));
//設定線,兩點一線
imageline($image,rand(1,99), rand(1,29),rand(1,99), rand(1,29),$linecolor);
}

//2>設定頭部,image/png
header(‘Content-Type: image/png‘);
//3>imagepng() 建立png圖形函數
imagepng($image);
//4>imagedestroy() 結束圖形函數 銷毀$image
imagedestroy($image);

php?>

其他的兩個頁面,不許要修改。

一般而言,現在就已經夠用了,漢字的驗證碼怎麼實現呢?

<?php
//11>設定session,必須處於指令碼最頂部
session_start();

//1>設定驗證碼圖片大小的函數
$image = imagecreatetruecolor(200, 60);
//5>設定驗證碼顏色 imagecolorallocate(int im, int red, int green, int blue);
$bgcolor = imagecolorallocate($image,255,255,255); //#ffffff
//6>地區填充 int imagefill(int im, int x, int y, int col) (x,y) 所在的地區著色,col 表示欲塗上的顏色
imagefill($image, 0, 0, $bgcolor);
//7>設定ttf字型
$fontface = ‘FZYTK.TTF‘;
//7>設定字型檔,實現簡單的數字儲備
$str=‘天地不仁以萬物為芻狗聖人不仁以百姓為芻狗這句經常出現在控訴暴君暴政上地殘暴不仁把萬物都當成低賤的豬狗來看待而那些高高在上的所謂聖人們也沒兩樣還不是把我們老百姓也當成豬狗不如的東西但實在正取的解讀是地不情感用事對萬物一視同仁聖人不情感用事對百姓一視同仁執子之手與子偕老當男女主人公含情脈脈看著對方說了句執子之手與子偕老女方淚眼朦朧含羞地回一句討厭啦這樣的情節我們是不是見過很多但是我們來看看這句的原句死生契闊與子成說執子之手與子偕老於嗟闊兮不我活兮於嗟洵兮不我信兮意思是說戰士之間的約定說要一起死現在和我約定的人都走了我怎麼活啊赤裸裸的兄弟江湖戰友友誼啊形容好基友的基情比男女之間的愛情要合適很多吧‘;
//str_split()切割字串為一個數組,一個中文在utf_8為3個字元
$strdb = str_split($str,3);
//>11
$captcha_code = ‘‘;
//8>產生隨機的漢子
for($i=0;$i<4;$i++){
//設定字型顏色,隨機顏色
$fontcolor = imagecolorallocate($image, rand(0,120),rand(0,120), rand(0,120)); //0-120深顏色
//隨機選取中文
$in = rand(0,count($strdb));
$cn = $strdb[$in];
//將中文記錄到將儲存到session的字串中
$captcha_code .= $cn;
/*imagettftext (resource $image ,float $size ,float $angle ,int $x ,int $y,int $color,
string $fontfile ,string $text ) 幕布 ,尺寸,角度,座標,顏色,字型路徑,文本字串
mt_rand()產生更好的隨機數,比rand()快四倍*/
imagettftext($image, mt_rand(20,24),mt_rand(-60,60),(40*$i+20),mt_rand(30,35),$fontcolor,$fontface,$cn);
}
//11>存到session
$_SESSION[‘authcode‘] = $captcha_code;
//9>增加幹擾元素,設定點
for($i=0;$i<200;$i++){
//設定點的顏色,50-200顏色比數字淺,不干擾閱讀
$pointcolor = imagecolorallocate($image,rand(50,200), rand(50,200), rand(50,200));

//imagesetpixel — 畫一個單一像素
imagesetpixel($image, rand(1,199), rand(1,59), $pointcolor);
}
//10>增加幹擾元素,設定線
for($i=0;$i<4;$i++){
//設定線的顏色
$linecolor = imagecolorallocate($image,rand(80,220), rand(80,220),rand(80,220));
//設定線,兩點一線
imageline($image,rand(1,199), rand(1,59),rand(1,199), rand(1,59),$linecolor);
}

//2>設定頭部,image/png
header(‘Content-Type: image/png‘);
//3>imagepng() 建立png圖形函數
imagepng($image);
//4>imagedestroy() 結束圖形函數 銷毀$image
imagedestroy($image);

php?>

其他的頁面也是不需要修改的。

  如下:

 

php實現驗證碼(數字、字母、漢字)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.