本文目錄一覽:
- 1、請問PHP生成驗證碼的類怎麼寫?
- 2、php如何實現驗證碼?許昌鯉魚IT計算機電腦軟體編程培訓中心
- 3、php怎麼實現驗證碼的
- 4、php驗證碼類幫看下,謝謝很短
- 5、php 調用驗證碼為一個類 怎麼調用
- 6、如何用PHP生成驗證碼
請問PHP生成驗證碼的類怎麼寫?
?php
class Code{
// 1. 定義各個成員 有寬、高、畫布、字數、類型、畫類型
private $width; //寬度
private $height; //高度
private $num; //驗證碼字數
private $imgType; //生成圖片類型
private $Type; //字串類型 1,2,3 三個選項 1 純數字 2 純小寫字母 3 大小寫數字混合
private $hb; //畫布
public $codestr; // 驗證碼字串
public function __construct($height=20,$num=4,$imgType=”jpeg”,$Type=1){
$this-width = $num*20;
$this-height = $height;
$this-num = $num;
$this-imgType = $imgType;
$this-Type = $Type;
$this-codestr = $this-codestr();
$this-zuhe();
}
// 2. 定義隨機獲取字元串函數
private function codestr(){
switch($this-Type){
case 1: // 類型為1 獲取1-9隨機數
$str = implode(“”,array_rand(range(0,9),$this-num));
break;
case 2: // 類型為2 獲取a-z隨機小寫字母
$str = implode(“”,array_rand(array_flip(range(a,z)),$this-num));
break;
case 3: // 類型為3 獲取數字,小寫字母,大寫字母 混合
for($i=0;$i$this-num;$i++){
$m = rand(0,2);
switch($m){
case 0:
$o = rand(48,57);
break;
case 1:
$o = rand(65,90);
break;
case 2:
$o = rand(97,122);
break;
}
$str .= sprintf(“%c”,$o);
}
break;
}
return $str;
}
// 3. 初始化畫布圖像資源
private function Hb(){
$this-hb = imagecreatetruecolor($this-width,$this-height);
}
// 4. 生成背景顏色
private function Bg(){
return imagecolorallocate($this-hb,rand(130,250),rand(130,250),rand(130,250));
}
// 5. 生成字體顏色
private function Font(){
return imagecolorallocate($this-hb,rand(0,100),rand(0,100),rand(0,100));
}
// 6. 填充背景顏色
private function BgColor(){
imagefilledrectangle($this-hb,0,0,$this-width,$this-height,$this-Bg());
}
// 7. 干擾點
private function ganrao(){
$sum=floor(($this-width)*($this-height)/3);
for($i=0;$i$sum;$i++){
imagesetpixel($this-hb,rand(0,$this-width),rand(0,$this-height),$this-Bg());
}
}
// 8. 隨機直線 弧線
private function huxian(){
for($i=0;$i$this-num;$i++){
imageArc($this-hb,rand(0,$this-width),rand(0,$this-height),rand(0,$this-width),rand(0,$this-height),rand(0,360),rand(0,360),$this-Bg());
}
}
// 9. 寫字
private function xiezi(){
for($i=0;$i$this-num;$i++){
$x=ceil($this-width/$this-num)*$i;
$y=rand(1,$this-height-15);
imagechar($this-hb,5,$x+4,$y,$this-codestr[$i],$this-Font());
}
}
// 10. 輸出
private function OutImg(){
$shuchu=”image”.$this-imgType;
$header=”Content-type:image/”.$this-imgType;
if(function_exists($shuchu)){
header($header);
$shuchu($this-hb);
}else{
exit(“GD庫沒有此類圖像”);
}
}
// 11. 拼裝
private function zuhe(){
$this-Hb();
$this-BgColor();
$this-ganrao();
$this-huxian();
$this-xiezi();
$this-OutImg();
}
public function getCodeStr(){
return $this-codestr;
}
}
$a = new Code();
$a -getCodeStr();
?
php如何實現驗證碼?許昌鯉魚IT計算機電腦軟體編程培訓中心
驗證碼在表單實現越來越多了,但是用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;$i4;$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;$i200;$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;$i4;$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’); //3imagepng() 建立png圖形函數
imagepng($image); //4imagedestroy() 結束圖形函數 銷毀$image
imagedestroy($image);
接著就是靜態頁的代碼了:index.html
doctype htmlhtml
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
pinput type=’submit’ value=’提交’ style=’padding:6px 5px;’/p
bodyhtml
從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();
}
那麼,純數字的實現了,數字加英文的也應該不難了。要修改的代碼 只是在 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;$i4;$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;$i200;$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;$i4;$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’); //3imagepng() 建立png圖形函數
imagepng($image); //4imagedestroy() 結束圖形函數 銷毀$image
imagedestroy($image);
其他的兩個頁面,不許要修改。
一般而言,現在就已經夠用了。但是就像動漫一樣,總會有番外。
那麼,我們來個漢字的番外吧。其實我也準備將漢字的驗證碼放到我的畢業設計裡面,雖然現在很流行滑動驗證碼,但是本人畢竟不是專門學習js的。
而且,還可以和答辯的老師說,我們驗證碼不需要素材,連圖片也是生成的,用自己的知識裝13,也沒有設么的。
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;$i4;$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;$i200;$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;$i4;$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’); //3imagepng() 建立png圖形函數
imagepng($image); //4imagedestroy() 結束圖形函數 銷毀$image
imagedestroy($image);
其他的頁面也是不需要修改的。
效果圖如下:
php怎麼實現驗證碼的
驗證碼功能機制實現思路
常規的驗證碼實現:
a、產生一張png的圖片
b、為圖片設置背景色
c、設置字體顏色和樣式
d、產生4位數的隨機的驗證碼
e、把產生的每個字元調整旋轉角度和位置畫到png圖片上
f、加入噪點和干擾線防止註冊機器分析原圖片來惡意註冊
g、輸出圖片
h、釋放圖片所佔內存
i、將驗證碼保存到session或是資料庫
j、將和輸入的驗證碼進行對比
簡訊(郵箱)驗證碼機制:
a、產生4-6位數的隨機的驗證碼
b、把產生的每個字元保存到session或是資料庫
c、將驗證碼發送到用戶的手機(郵箱)
d、用戶在規定時間內進行輸入
e、將驗證碼從session或是資料庫中取出
f、將和輸入的驗證碼進行對比驗證
php驗證碼類幫看下,謝謝很短
一、修改php.ini(保舉)
memory_limit = 12M
2、在程序裡面添加如下語句
ini_set(”memory_limit”, ”12M”);
3、在根目錄建立
.htaccess文件,添加如下內容
php_value memory_limit 12M
如果還不能解決,就把它改得再大一些。
最後注意的是重啟伺服器
==========================
上面是我抄的,看到你的提問我感覺是配置的問題,但我這人記不住東西,只能找,其實你把報錯的提示一搜就會有答案的
php 調用驗證碼為一個類 怎麼調用
1,php驗證碼類
?php
// usage:
/*
顯示驗證碼:
img src=”captcha.php?cap=login.png”
檢查驗證碼:
檢查輸入的驗證碼與 $_SESSION[‘login’] 中保存的值是否相等。
*/
error_reporting(E_ALL);
session_start();
(!isset($_GET[‘cap’]))?die(‘Error !’):1;
$captcha_array=array(‘login.png’,’contact.png’,’comment.png’);
(!in_array($_GET[‘cap’],$captcha_array))?die(‘Error !’):1;
$captcha_cod=new captcha(basename($_GET[‘cap’],’.png’)) ;
//驗證碼類
class captcha
{
private $session_name;
private $image_width;
private $image_height;
private $cod_length;
private $cod_mode;
private $font_path;
private $avtage_font_size;
private $sec_cod;
private $res_image;
function __construct($name,$width=200,$height=50,$length=5,$mod=2,$font=’arial.ttf’,$av_font_size=25)
{
$this- session_name = $name ;
$this- image_width = $width ;
$this- image_height = $height ;
$this- cod_length = $length ;
$this- mode = $mod ;
$this- font_path = $font ;
$this- avrage_font_size = $av_font_size ;
$this-Gen_Cod();
}
function Write_Text($text)
{
$x_pos=10;
for($pos=0;$posstrlen($text);$pos++) {
imagettftext($this-res_image,rand($this-avrage_font_size -2,$this-avrage_font_size +2),
rand(-40,+40),$x_pos,rand(35,$this-image_height – $this-avrage_font_size),
imagecolorallocate($this-res_image,rand(0,150),rand(0,150),rand(0,150)),
$this-font_path,$text[$pos]);
$x_pos+=($this-image_width/$this-cod_length);
}
}
function Draw_Line()
{
//
for($pos=0;$pos$this-image_height;$pos+=8)
imageline($this-res_image,0,$pos,$this-image_width,$pos,imagecolorallocate($this-res_image,rand(200,230),rand(200,230),rand(200,230)));
//
for($pos=0;$pos$this-image_width;$pos+=8)
imageline($this-res_image,$pos,0,$pos,$this-image_height,imagecolorallocate($this-res_image,rand(200,230),rand(200,230),rand(200,230)));
}
function Gen_Cod()
{
//generate rand cod :
//mode:1 = 0-9 , mode:2 = 0-9 , a-z
($this-mode==1) ? $this-sec_cod=substr((string)rand(1000000000,9999999999),0,$this-cod_length) :
$this-sec_cod=substr(md5(rand(1000000000,9999999999)),0,$this-cod_length);
//set session :
$_SESSION[$this-session_name] = $this-sec_cod ;
//creat image :
$this-res_image=imagecreatetruecolor( $this-image_width , $this-image_height );
//fill color:
imagefilledrectangle($this-res_image,0,0,$this-image_width,$this-image_height,imagecolorallocate($this-res_image,255,255,255));
//write text :
$this-Write_Text($this-sec_cod);
//draw line :
$this-Draw_Line();
//output :
imagejpeg($this-res_image);
header(‘content-type:image/jpeg’);
//destroy:
imagedestroy($this-res_image);
}
}
2,php驗證碼類的調用示例:
?php
session_start();
if(isset($_POST[‘captchacod’])){
if($_SESSION[‘login’]==$_POST[‘captchacod’])echo’a style=”color:green”Your Entered Cod Was Correct/abr’;
else echo’a style=”color:red”Your Entered Cod Was Incorrect/abr’;
}
?
img src=”captcha.php?cap=login.png”
form action=”?php echo $_SERVER[‘PHP_SELF’]; //safe it later (xss)?” method=”post”
aINPUT TEXT :/abr
input type=”text” name=”captchacod”br
input type=”submit” value=”check”br
/form
如何用PHP生成驗證碼
PHP生成驗證碼的原理:使用PHP的GD庫,生成一張帶驗證碼的圖片,並將驗證碼保存在Session中。PHP生成驗證碼的大致流程有:
1、產生一張png的圖片;
2、為圖片設置背景色;
3、設置字體顏色和樣式;
4、產生4位數的隨機的驗證碼;
5、把產生的每個字元調整旋轉角度和位置畫到png圖片上;
6、加入噪點和干擾線防止註冊機器分析原圖片來惡意破解驗證碼;
7、輸出圖片;
8、釋放圖片所佔內存。
session_start();
getCode(4,60,20);
function getCode($num,$w,$h) {
$code = “”;
for ($i = 0; $i $num; $i++) {
$code .= rand(0, 9);
}
//4位驗證碼也可以用rand(1000,9999)直接生成
//將生成的驗證碼寫入session,備驗證時用
$_SESSION[“helloweba_num”] = $code;
//創建圖片,定義顏色值
header(“Content-type: image/PNG”);
$im = imagecreate($w, $h);
$black = imagecolorallocate($im, 0, 0, 0);
$gray = imagecolorallocate($im, 200, 200, 200);
$bgcolor = imagecolorallocate($im, 255, 255, 255);
//填充背景
imagefill($im, 0, 0, $gray);
//畫邊框
imagerectangle($im, 0, 0, $w-1, $h-1, $black);
//隨機繪製兩條虛線,起干擾作用
$style = array ($black,$black,$black,$black,$black,
$gray,$gray,$gray,$gray,$gray
);
imagesetstyle($im, $style);
$y1 = rand(0, $h);
$y2 = rand(0, $h);
$y3 = rand(0, $h);
$y4 = rand(0, $h);
imageline($im, 0, $y1, $w, $y3, IMG_COLOR_STYLED);
imageline($im, 0, $y2, $w, $y4, IMG_COLOR_STYLED);
//在畫布上隨機生成大量黑點,起干擾作用;
for ($i = 0; $i 80; $i++) {
imagesetpixel($im, rand(0, $w), rand(0, $h), $black);
}
//將數字隨機顯示在畫布上,字元的水平間距和位置都按一定波動範圍隨機生成
$strx = rand(3, 8);
for ($i = 0; $i $num; $i++) {
$strpos = rand(1, 6);
imagestring($im, 5, $strx, $strpos, substr($code, $i, 1), $black);
$strx += rand(8, 12);
}
imagepng($im);//輸出圖片
imagedestroy($im);//釋放圖片所佔內存
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/312570.html