寫教學的最大目的是教會未來的自己

驗證碼的製作

本次教學 要感謝許多人的幫助

感謝 kidpen 及其作者 提供了最原始的code

感謝 orinx.chen 學弟教我如何使用這個code

感謝 http://jax-work-archive.blogspot.com/2007/11/php.html 提供完整的教學

感謝 魏藥 同學 一直被我叫出來解決GD 程式庫的問題(沒辦法 這台server 他負責XD)

 

另外 kipen 是使用 MIT 授權

而本人是使用 GPL v3授權

使用本code 的網友們  請保留我們的版權宣告,並請遵守 GPLv3 的規範,謝謝

 

驗證碼是使用 PHP 自動生成一塊圖片,這部分會使用到GD 程式庫,請自行安裝

http://www.arthurtoday.com/2010/03/ubuntu-php-gd-library.html

 

本次我製作的 驗證碼 是 顯示 中文數字,要求使用者輸入 阿拉伯數字的雙重驗證

 


 *
 * This file incorporates work covered by the following copyright and
 * permission notice:
 *
 *Copyright (c) 2011 Chen-Heng Chang
 *
 *Permission is hereby granted, free of charge, to any person obtaining a copy
 *of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
 * Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
 *
 *The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
 *
 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
 *LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
 *TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
	/*
	這是一個驗證碼產生器
	*/
	session_start(); //開啟 seesion 功能
	$long=5;//驗證碼長度
	mt_srand((double)microtime()*1000000);
	$code = '';
	$possible = '0123456789'; //放入阿拉伯數字
	$possibleLen=strlen($possible);
	$chinese=array('零','壹','貳','參','肆','伍','陸','柒','捌','玖');//相對應的 中文數字
	$show='';
	for($i=0; $i< $long; $i++){//產生驗證碼
		$code .=$possible[rand(0,$possibleLen-1)];
	}
	$_SESSION['code'] = $code; //存入資料,之後要使用 就用這個SESSION
	for($i=0; $i< $long; $i++)//轉換成中文字
	{
		$show.=$chinese[$code[$i]];
	}
	$width=$long * 20+20;
	$height=30;
	$image = imagecreate($width, $height) or die('GD image creating error.');//產生一塊圖
	$background_color = imagecolorallocate($image, 239, 239, 239); //設定底色
	$text_color = imagecolorallocate($image, 0, 169, 225);//設定文字顏色
	$noise_color = imagecolorallocate($image, 200, 200, 200);//設定雜訊顏色
	imagefill($image,0,0,$background_color);
	imagettftext($image,15,0,10,20,$text_color,'/home/pupuliao/web/code/kaiu.ttf',$show);
	/*
imagettftext (int im, int size, int angle, int x, int y, int col, string fontfile, string text)
im 圖片物件 size 文字大小 angle 0度將會由左到右讀取文字,而更高的值表示逆時鐘旋轉
x y 文字起始座標 col 顏色物件 fontfile 字形路徑,為主機實體目錄的絕對路徑,
可自行設定想要的字型 text 寫入的文字字串
*/
	for ($i=0; $i<($width*$height)/250; $i++) {//產生雜訊
		imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
	}
	  header("Content-Disposition:filename=image_code.png");
	header("Content-type:image/png");
	imagepng($image);
	imagedestroy($image);
	exit;
?>

 

 

這隻程式碼有一點要注意的地方在 字體

  imagettftext($image,15,0,10,20,$text_color,'/home/pupuliao/web/code/kaiu.ttf',$show);

這部分 要引用一個可以使用中文的字體,本次範例我是使用電腦中的 標楷體

 

這是實際產生出來的結果

 

如果驗證碼 的php 檔案名稱為 image.php

那我在 表單中 只需要加入

<input type="text" name="code" /><img src="image.php">

即可

 

在後端只需要檢查 $_SESSION[‘code’] 和送出的值是否相同即可

 

DEMO

Post to Twitter Post to Plurk Post to Facebook Send Gmail

發表迴響

Copyright © 2024. All Rights Reserved.

歡迎光臨
初音