程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> 實用PHP驗證碼類代碼(1/2)

實用PHP驗證碼類代碼(1/2)

編輯:關於PHP編程

開發應用中驗證碼是少不了的,我們經常會碰以關於被機器注冊,那麼有了驗證碼可以有效的防止這類行為,下面我們來看看我提供的這款代碼。

開發應用中驗證碼是少不了的,我們經常會碰以關於被機器注冊,那麼有了驗證碼可以有效的防止這類行為,下面我們來看看我提供的這款代碼。

<?php教程
session_start();
class authnum {
//圖片對象、寬度、高度、驗證碼長度
private $im;
private $im_width;
private $im_height;
private $len;
//隨機字符串、y軸坐標值、隨機顏色
private $randnum;
private $y;
private $randcolor;
//背景色的紅綠藍,默認是淺灰色
public $red=238;
public $green=238;
public $blue=238;
/**
* 可選設置:驗證碼類型、干擾點、干擾線、y軸隨機
* 設為 false 表示不啟用
**/
//默認是大小寫數字混合型,1 2 3 分別表示 小寫、大寫、數字型
public $ext_num_type='';
public $ext_pixel = false; //干擾點
public $ext_line = false; //干擾線
public $ext_rand_y= true; //y軸隨機
function __construct ($len=4,$im_width='',$im_height=25) {
// 驗證碼長度、圖片寬度、高度是實例化類時必需的數據
$this->len = $len; $im_width = $len * 15;
$this->im_width = $im_width;
$this->im_height= $im_height;
$this->im = imagecreate($im_width,$im_height);
}
// 設置圖片背景顏色,默認是淺灰色背景
function set_bgcolor () {
imagecolorallocate($this->im,$this->red,$this->green,$this->blue);
}
// 獲得任意位數的隨機碼
function get_randnum () {
$an1 = 'abcdefghijklmnopqrstuvwxyz';
$an2 = 'abcdefghijklmnopqrstuvwxyz';
$an3 = '0123456789';
if ($this->ext_num_type == '') $str = $an1.$an2.$an3;
if ($this->ext_num_type == 1) $str = $an1;
if ($this->ext_num_type == 2) $str = $an2;
if ($this->ext_num_type == 3) $str = $an3;
for ($i = 0; $i < $this->len; $i++) {
$start = rand(1,strlen($str) - 1);
$randnum .= substr($str,$start,1);
}
$this->randnum = $randnum;
$_session[an] = $this->randnum;
}
// 獲得驗證碼圖片y軸
function get_y () {
if ($this->ext_rand_y) $this->y = rand(5, $this->im_height/5);
else $this->y = $this->im_height / 4 ;
}

1 2

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved