⑴ php的驗證碼提示怎樣製作
一般製作驗證碼會按照下面的幾步走:
一:創建出來一個圖片,通常我們成為源,可以用imagecreatetruecolor()這個函數搞定
二:給這個源 添加背景色,同時設置文本顯示的顏色,GD庫函數為我們提供了imagecolorallocate()函數
三:材料弄好了,我們要給它添點內容了,就是我們隨機生成的數字或者字母,甚至可以是它們的組合,這里我們可以選擇兩個函數 imagettftext()、imagesrting(),這兩個函數的不同,我們會在後面講解。
例:
<?php
session_start();//開啟session,用來記錄獲得的驗證碼,這個函數要寫在程序的開頭,不然會出現錯誤
header(「Content-type :image/gif」);//把文件的返回類型設為image/gif格式,這個格式可以輸出圖片
$codelen=4;//設置你要讓用戶輸入字元的個數,一般為4,過長用戶體驗不好。
$charset =」ABCDEFGHKLMNPRSTUVWYZ23456789″;//我們可以盡量把一些難以辨認的字元去掉,比如阿拉伯數字0和字母o,這也是提高用戶體驗的一種方法。
$code =」;
for($i=0;$i<$codelen;$i++){//用for循環得到4個隨機的字元,在這里用到了mt_rand,這個函數比rand的效率要高的多,建議大家用這個
$code .=$charset{mt_rand(0,strlen($charset)-1)};
}
$_SESSION['code']=$code;//下篇關於session驗證的文章將會用到
$width = 80;
$height = 40;
$im = imagecreatetruecolor($width,$height);//用imagecreatetruecolor()函數來建立一個新的圖片,裡面的兩個數值分別是寬度和高度,這是製作驗證碼的第一步
$bg = imagecolorallocate($im,255,255,0); //圖片背景的顏色,這里是第二步
$textcolor = imagecolorallocate($im,255,0,0);//文字的顏色
imagefill($im,0,0,$bg);//給圖片填充背景色
//好了上面的鋪墊任務做的差不多了,現在關鍵就是讓字元顯示在圖片上,這里有兩種方法我們一一介紹。
$font =」ggbi.ttf」;//如果你有字體的話 就填上字體的相對路徑,如果沒有就留空。下面的兩個用法我會一一講解。
if($font!==」"){
for($num=0;$num<4;$num++){
imagettftext($im,mt_rand(12,16),(mt_rand(0,75)+330)%360,5+15*$num,20+mt_rand(2,5),$textcolor,$font,$code[$num]);//這里是第三步
}
}
else{
for($num=0;$num<4;$num++){
imagestring($im,5,10+15*$num,10+mt_rand(0,5),$code[$num],$textcolor);
}
}
header(「Content-type: image/jpeg」);
imagejpeg($im);
?>
⑵ 如何用PHP寫圖片驗證碼驗證代碼
你去php100看看視頻教程吧,踏踏實實的看完了,你的問題也就解決了
⑶ php 驗證碼 驗證
session是把數據保存在伺服器的,如果是本地可以考慮使用cookie
⑷ 怎麼使用PHP PDO 寫一個登錄驗證代碼
<span style="font-size:18px;"><span style="font-size:18px;"><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset="utf-8" />
</head>
<?php
session_start(); //初始化session變數
$username = $_POST['name']; //接收表單提交的用戶名
$password=md5($_POST['pwd']); //接收表單提交的密碼
class chkinput //定義類
{
var $name;
var $pwd;
function chkinput($x,$y) //定義一個方法
{
$this->name=$x; //將管理員名稱傳給類對象$this->name
$this->pwd=$y; //將管理員密碼傳給類對象$this->pwd
}
function checkinput()
{
include("conn.php"); //連接資料庫文件
$sql=mysql_query("select username,password from admin where username='".$this->name."' and password='".$this->pwd."'",$conn);
$info=mysql_fetch_array($sql); //檢索管理員名稱和密碼是否正確
if($info==false) //如果管理員名稱或密碼不正確,則彈出相關提示信息
{
echo "<script language='javascript'>alert('您輸入的管理員名稱或密碼錯誤,請重新輸入!');history.back();</script>";
exit;
}
else //如果管理員名稱或密碼正確,則直接跳轉到登陸成功後界面
{
echo "<script>window.location='home.php';</script>";
$_SESSION['admin_name']=$info['username']; //將管理員名稱存到$_SESSION[admin_name]變數中
$_SESSION['pwd']=$info['password']; ////將管理員名稱存到$_SESSION[pwd]變數中
}
}
}
$obj=new chkinput(trim($username),trim($password)); //創建對象
$obj->checkinput(); //調用類
?>
</html></span></span>
⑸ 怎麼用PHP寫個驗證碼
首先,當用戶打開頁面時隨機產生一個session,然後根據這個值生成驗證碼圖片。
第二,將驗證碼圖片顯示到表單上。
第三,當用戶提交時表單時,比較session里的值與表單中驗證碼的值進行比較。
簡單的實現過程:http://www.nowamagic.net/php/php_CheckCode.php
復雜的驗證碼圖片生成:http://www.admin5.com/article/20080314/75984.shtml
⑹ php的圖片驗證碼代碼
這個是phpcms的驗證碼,經過十幾萬個網站經驗的,非常好用
<?php
session_start();
$enablegd = 1;
//判斷圖像處理函數是否存在
$funcs = array('imagecreatetruecolor','imagecolorallocate','imagefill','imagestring','imageline','imagerotate','imagedestroy','imagecolorallocatealpha','imageellipse','imagepng');
foreach($funcs as $func)
{
if(!function_exists($func))
{
$enablegd = 0;
break;
}
}
ob_clean(); //清理緩沖
if($enablegd)
{
//create captcha
$consts = 'cdfgkmnpqrstwxyz23456';
$vowels = 'aek23456789';
for ($x = 0; $x < 6; $x++)
{
$const[$x] = substr($consts, mt_rand(0,strlen($consts)-1),1); //獲取$consts中的一個隨機數
$vow[$x] = substr($vowels, mt_rand(0,strlen($vowels)-1),1); //獲取$vowels中的一個隨機數
}
$radomstring = $const[0] . $vow[0] .$const[2] . $const[1] . $vow[1] . $const[3] . $vow[3] . $const[4];
$_SESSION['checkcode'] = $string = substr($radomstring,0,4); //顯示4個字元
$imageX = strlen($radomstring)*8; //圖像的寬
$imageY = 20; //圖像的高
$im = imagecreatetruecolor($imageX,$imageY); //新建一個真彩色圖像
//creates two variables to store color
$background = imagecolorallocate($im, rand(180, 250), rand(180, 250), rand(180, 250)); //背景色
$foregroundArr = array(imagecolorallocate($im, rand(0, 20), rand(0, 20), rand(0, 20)),
imagecolorallocate($im, rand(0, 20), rand(0, 10), rand(245, 255)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(0, 10)),
imagecolorallocate($im, rand(245, 255), rand(0, 20), rand(245, 255))
);
$foreground2 = imagecolorallocatealpha($im, rand(20, 100), rand(20, 100), rand(20, 100),80); //分配顏色並說明透明度
$middleground = imagecolorallocate($im, rand(200, 160), rand(200, 160), rand(200, 160)); //中間背景
$middleground2 = imagecolorallocatealpha($im, rand(180, 140), rand(180, 140), rand(180, 140),80); //中間背景2
//與左上角的顏色相同的都會被填充
imagefill($im, 0, 0, imagecolorallocate($im, 250, 253, 254));
//往圖像上寫入文字
imagettftext($im, 12, rand(30, -30), 5, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ALGER.TTF', $string[0]);
imagettftext($im, 12, rand(50, -50), 20, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ARIALNI.TTF', $string[1]);
imagettftext($im, 12, rand(50, -50), 35, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/ALGER.TTF', $string[2]);
imagettftext($im, 12, rand(30, -30), 50, rand(14, 16), $foregroundArr[rand(0,3)], XINCHENG_ROOT.'include/fonts/arial.ttf', $string[3]);
//畫邊框
$border = imagecolorallocate($im, 133, 153, 193);
imagerectangle($im, 0, 0, $imageX - 1, $imageY - 1, $border);
//畫一些隨機出現的點
$pointcol = imagecolorallocate($im, rand(0,255), rand(0,255), rand(0,255));
for ($i=0;$i<80;$i++)
{
imagesetpixel($im,rand(2,$imageX-2),rand(2,$imageX-2),$pointcol);
}
//畫隨機出現的線
for ($x=0; $x<9;$x++)
{
if(mt_rand(0,$x)%2==0)
{
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999)); //畫線
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground2); //畫橢圓
}
else
{
imageline($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 999999));
imageellipse($im, rand(0, 120), rand(0, 120), rand(0, 120), rand(0, 120), $middleground);
}
}
//output to browser
header("content-type:image/png\r\n");
imagepng($im);
imagedestroy($im);
}
else
{
$files = glob(XINCHENG_ROOT.'images/checkcode/*.jpg');
if(!is_array($files)) die('請檢查文件目錄完整性:/images/checkcode/');
$checkcodefile = $files[rand(0, count($files)-1)]; //隨機其中一個文件
$_SESSION['checkcode'] = substr(basename($checkcodefile), 0, 4); //獲得文件名
header("content-type:image/jpeg\r\n");
include $checkcodefile;
}
?>
⑺ 如何用PHP生成驗證碼
PHP生成驗證碼的原理:使用PHP的GD庫,生成一張帶驗證碼的圖片,並將驗證碼保存在Session中。PHP生成驗證碼的大致流程有:
1、產生一張png的圖片;
2、為圖片設置背景色;
3、設置字體顏色和樣式;
4、產生4位數的隨機的驗證碼;
5、把產生的每個字元調整旋轉角度和位置畫到png圖片上;
6、加入噪點和干擾線防止注冊機器分析原圖片來惡意破解驗證碼;
7、輸出圖片;
8、釋放圖片所佔內存。
session_start();
getCode(4,60,20);
functiongetCode($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);//釋放圖片所佔內存
}
⑻ 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;$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);
接著就是靜態頁的代碼了: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();
}
那麼,純數字的實現了,數字加英文的也應該不難了。要修改的代碼 只是在 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 =''; //取出值,字元串截取方法 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);
其他的兩個頁面,不許要修改。
一般而言,現在就已經夠用了。但是就像動漫一樣,總會有番外。
那麼,我們來個漢字的番外吧。其實我也准備將漢字的驗證碼放到我的畢業設計裡面,雖然現在很流行滑動驗證碼,但是本人畢竟不是專門學習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;$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
//驗證碼:文本類型為圖像
header("content-type:image/png");
define('TYPE',3);//1.字母 2.字母數字 3.數字 4.邏輯 5.漢字
session_start();
//創建畫布
$img = imagecreatetruecolor(90,33);
//創建顏色
//$bgcolor = imagecolorallocate($img,rand(200,255),rand(200,255),rand(200,255));
$bgcolor = imagecolorallocate($img,255,255,255);
$textcolor = imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100));
//填充顏色到畫布
imagefill($img,0,0,$bgcolor);
//創建但像素的點為干擾項
//for($i=0;$i<100;$i++){
// $pixelcolor = imagecolorallocate($img,rand(150,200),rand(150,200),rand(150,200));
// imagesetpixel($img,rand(0,70),rand(0,30),$pixelcolor);
//}
//
////劃線
//$linecolor = imagecolorallocate($img,255,0,0);
//imageline($img,0,0,70,30,$linecolor);
//
////多邊形
// $col_poly = imagecolorallocate ( $img , 0 , 255 , 0 );
// imagepolygon ( $img ,
// array (
// 5 , 5,
// 5, 15 ,
// 20,15,
// 20,5
// ),
// 4 ,
// $col_poly );
////弧線
//$arcColor = imagecolorallocate ( $img , 0 , 0 , 255 );
//imagearc($img,35,15,30,30,0,360,$arcColor);
//創建驗證碼的內容
//#字母
$letter = range('A','Z');
$letterStr = $letter[rand(0,25)].$letter[rand(0,25)].$letter[rand(0,25)].$letter[rand(0,25)];
//數字字母
$num = range(0,9);
$numberAndLetter = array_merge($letter,$num);
$nal = $numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)].$numberAndLetter[rand(0,35)];
//#數字
$number = rand(1000,9999);
//#邏輯
$x = rand(1,9);
$y = rand(1,9);
$expression = $x."+".$y."=?";
$sum = $x+$y;
//#漢字
$CH = array('恭喜發財','財源滾滾','財源廣進','才高八斗','學富五車','抬頭見喜');
$chstr = $CH[rand(0,count($CH)-1)];
switch(TYPE){
case 1 : imagettftext($img,14,0,7,23,$textcolor,'MSYH.TTF',$letterStr);$_SESSION['code']=$letterStr;break;
case 2 : imagettftext($img,14,0,7,23,$textcolor,'MSYH.TTF',$nal);$_SESSION['code']=$nal;break;
case 3 : imagettftext($img,14,0,13,23,$textcolor,'MSYH.TTF',$number);$_SESSION['code']=$number;break;
case 4 : imagettftext($img,14,0,7,23,$textcolor,'MSYH.TTF',$expression);$_SESSION['code']=$sum;break;
case 5 : imagettftext($img,11,0,6,21,$textcolor,'MSYHBD.TTF',$chstr);$_SESSION['code']=$chstr;break;
}
//輸入圖像到瀏覽器
imagepng($img);
?>
⑽ php實現驗證碼,能給具體的代碼嗎 在這謝謝過各位高手了
index.php:
<?php
/* index.php start*/
if(!empty($_POST)) {
session_start();
if($_POST['seccode'] == $_SESSION['seccode']) {
echo '<script>alert("驗證成功")</script>';
} else {
echo '<script>alert("驗證失敗")</script>';
}
session_destroy();
}
?>
<form action="" method="post" />
<img id="seccode" src="seccode.php?rand=".<?=rand()?> /> <input type="text" name="seccode" /> <input type="submit" value="submit" />
<input type="button" onclick="document.getElementById('seccode').src = 'seccode.php?reload=1&' + Math.random()" value="change one"/>
</form>
<?php
/* index.php end*/
?>
******************************
seccode.php:
<?php
/*seccode.php start*/
session_start();
if(isset($_SESSION['seccode']) && empty($_GET['reload'])) {
$arr = $_SESSION['seccode'];
} else {
for($i=0; $i<4; $i++) {
$arr[] = rand(0, 9);
}
$_SESSION['seccode'] = implode($arr);
}
$im = imagecreate(90, 25);
$backgroundcolor = imagecolorallocate ($im, 255, 255, 255);
for($i = 0; $i < 4; $i++) {
$s = iconv('GBK', 'UTF-8', $arr[$i]);
$x = $i * 20 + mt_rand(0, 4) - 2;// 隨機X
$y = mt_rand(0, 4); // 隨機Y
$angle = mt_rand(0,4);// 隨機角度
$text_color = imagecolorallocate($im, mt_rand(50, 255), mt_rand(50, 128), mt_rand(50, 255)); // 隨機顏色
imagettftext($im,20, $angle,$x,20+$y,$text_color,"C:\\Windows\\Fonts\\SIMSUN.TTC",$s);
}
// 線條
$linenums = mt_rand(10, 32);
for($i=0; $i <= $linenums; $i++) {
$linecolor = imagecolorallocate($im, mt_rand(0, 255), mt_rand(0, 255), mt_rand(0, 255));
$linex = mt_rand(0, 62);
$liney = mt_rand(0, 25);
imageline($im, $linex, $liney, $linex + mt_rand(0, 4) - 2, $liney + mt_rand(0, 4) - 2, $linecolor);
}
// 雜點
for($i=0; $i <= 64; $i++) {
$pointcolor = imagecolorallocate($im, mt_rand(50, 255), mt_rand(50, 255), mt_rand(50, 255));
imagesetpixel($im, mt_rand(0, 62), mt_rand(0, 25), $pointcolor);
}
// 邊框
$bordercolor = imagecolorallocate($im , 150, 150, 150);
imagerectangle($im, 0, 0, 89, 24, $bordercolor);
header('Content-type: image/png');
imagepng($im);
imagedestroy($im);
/*seccode.php end*/
?>