php模擬登陸驗證碼
『壹』 php 繪制網站登錄首頁圖片驗證碼
幾乎所有的網站登錄頁都會有驗證碼,驗證碼是一種安全保護機制,在注冊時要求必須有人工操作進行驗證,用於防止垃圾注冊機大量注冊用戶賬號佔用伺服器內存從而使伺服器癱瘓。
圖片驗證碼的實現十分簡單。首先從指定字元集合中隨機抽取固定數目的字元,以一種不規則的方法畫在畫布上,再適當添加一些干擾點和干擾元素,最後將圖片輸出,一張嶄新的驗證碼就完成了。
先給大家展示下生成的驗證碼:
點擊刷新:
如果大家對實現效果非常滿意,請繼續往下看。
前端代碼如下:
<!DOCTYPE
html>
<html>
<head>
<meta
http-equiv="content-type"
content="text/html;charset=utf-8">
<title>This
is
a
test!</title>
<link
rel="stylesheet"
type="text/css"
href="css/bootstrap.min.css">
</head>
<body>
<form
name="form">
<input
type="text"
placeholder="賬號"/><br/>
<input
type="password"
placeholder="密碼"/><br/>
<input
type="text"
placeholder="驗證碼"/>
<img
id="verImg"
src="libs/verification.php"/>
<a
href="#"
class="change"
onclick="changeVer()">點擊刷新</a><br/>
<input
type="submit"
value="登錄"/>
</form>
<script
type="text/javascript">
//刷新驗證碼
function
changeVer(){
document.getElementById("verImg").src="libs/verification.php?tmp="+Math.random();
}
</script>
</body>
</html>
php腳本文件驗證碼的代碼如下:
<?php
session_start();
//開啟session記錄驗證碼數據
vCode(4,
15);//設置驗證碼的字元個數和圖片基礎寬度
//vCode
字元數目,字體大小,圖片寬度、高度
function
vCode($num
=
4,
$size
=
20,
$width
=
0,
$height
=
0)
{
!$width
&&
$width
=
$num
*
$size
*
4
/
5
+
15;
!$height
&&
$height
=
$size
+
10;
//設置驗證碼字元集合
$str
=
"";
//保存獲取的驗證碼
$code
=
''
//隨機選取字元
for
($i
=
0;
$i
<
$num;
$i++)
{
$code
.=
$str[mt_rand(0,
strlen($str)-1)];
}
//創建驗證碼畫布
$im
=
imagecreatetruecolor($width,
$height);
//背景色
$back_color
=
imagecolorallocate($im,
mt_rand(0,100),mt_rand(0,100),
mt_rand(0,100));
//文本色
$text_color
=
imagecolorallocate($im,
mt_rand(100,
255),
mt_rand(100,
255),
mt_rand(100,
255));
imagefilledrectangle($im,
0,
0,
$width,
$height,
$back_color);
//
畫干擾線
for($i
=
0;$i
<
5;$i++)
{
$font_color
=
imagecolorallocate($im,
mt_rand(0,
255),
mt_rand(0,
255),
mt_rand(0,
255));
imagearc($im,
mt_rand(-
$width,
$width),
mt_rand(-
$height,
$height),
mt_rand(30,
$width
*
2),
mt_rand(20,
$height
*
2),
mt_rand(0,
360),
mt_rand(0,
360),
$font_color);
}
//
畫干擾點
for($i
=
0;$i
<
50;$i++)
{
$font_color
=
imagecolorallocate($im,
mt_rand(0,
255),
mt_rand(0,
255),
mt_rand(0,
255));
imagesetpixel($im,
mt_rand(0,
$width),
mt_rand(0,
$height),
$font_color);
}
//隨機旋轉角度數組
$array=array(5,4,3,2,1,0,-1,-2,-3,-4,-5);
//
輸出驗證碼
//
imagefttext(image,
size,
angle,
x,
y,
color,
fontfile,
text)
@imagefttext($im,
$size
,
array_rand($array),
12,
$size
+
6,
$text_color,
'c:WINDOWSFontssimsun.ttc',
$code);
$_SESSION["VerifyCode"]=$code;
//no-cache在每次請求時都會訪問伺服器
//max-age在請求1s後再次請求會再次訪問伺服器,must-revalidate則第一發送請求會訪問伺服器,之後不會再訪問伺服器
//
header("Cache-Control:
max-age=1,
s-maxage=1,
no-cache,
must-revalidate");
header("Cache-Control:
no-cache");
header("Content-type:
image/png;charset=gb2312");
//將圖片轉化為png格式
imagepng($im);
imagedestroy($im);
}
?>
好了,關於小編給大家介紹的php繪制圖片驗證就給大家介紹這么多,希望對大家有所幫助!

『貳』 如何用PHP生成驗證碼
php生成驗證碼,php驗證碼,php怎樣生成驗證碼?
工具/原料
這個驗證碼較實用,大家可以應用到項目中。
方法/步驟
1.
<?php
/*設置文件頭為圖片輸出*/
Header("Content-type:image/JPEG");
/*調用生成驗證碼函數*/
$checkcode=make_rand(4);
/**
*生成驗證碼字元
*@paramint$length驗證碼字元長度
*@returnstring
*/
functionmake_rand($length="32"){
$str="ABCDEFGHIJKLMNOPQRSTUVWXYZ";
$result="";
for($i=0;$i<$length;$i++){
$num[$i]=rand(0,25);
$result.=$str[$num[$i]];
}
return$result;
}
2.
/*調用輸出驗證碼圖片函數*/
getAuthImage($checkcode,160,40);
/**
*生成驗證碼圖片
*@paramstring$text驗證碼字元
*/
functiongetAuthImage($text,$w,$y){
/*設置圖片的寬度和高度*/
$im_x=$w;
$im_y=$y;
/*創建圖片*/
$im=imagecreatetruecolor($im_x,$im_y);
$text_c=ImageColorAllocate($im,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
$tmpC0=mt_rand(100,255);
$tmpC1=mt_rand(100,255);
$tmpC2=mt_rand(100,255);
$buttum_c=ImageColorAllocate($im,$tmpC0,$tmpC1,$tmpC2);
imagefill($im,16,13,$buttum_c);
3.
/*字體文件*/
$font='t1.ttf';
for($i=0;$i<strlen($text);$i++)
{
$tmp=substr($text,$i,1);
$array=array(-1,1);
$p=array_rand($array);
$an=$array[$p]*mt_rand(1,10);//角度
$size=28;
imagettftext($im,$size,$an,15+$i*$size,35,$text_c,$font,$tmp);
}
/*將字元寫入文件中*/
$distortion_im=imagecreatetruecolor($im_x,$im_y);
imagefill($distortion_im,16,13,$buttum_c);
for($i=0;$i<$im_x;$i++){
for($j=0;$j<$im_y;$j++){
$rgb=imagecolorat($im,$i,$j);
if((int)($i+20+sin($j/$im_y*2*M_PI)*10)<=imagesx($distortion_im)&&(int)($i+20+sin($j/$im_y*2*M_PI)*10)>=0){
imagesetpixel($distortion_im,(int)($i+10+sin($j/$im_y*2*M_PI-M_PI*0.1)*4),$j,$rgb);
}
}
}
4.
/*干擾元素點的數量*/
$count=160;
/*創建干擾元素點*/
for($i=0;$i<$count;$i++){
$randcolor=ImageColorallocate($distortion_im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagesetpixel($distortion_im,mt_rand()%$im_x,mt_rand()%$im_y,$randcolor);
}
/*創建干擾線條*/
$rand=mt_rand(5,30);
$rand1=mt_rand(15,25);
$rand2=mt_rand(5,10);
for($yy=$rand;$yy<=+$rand+2;$yy++){
for($px=-80;$px<=80;$px=$px+0.1)
{
$x=$px/$rand1;
if($x!=0)
{
$y=sin($x);
}
$py=$y*$rand2;
imagesetpixel($distortion_im,$px+80,$py+$yy,$text_c);
}
}
5.
/*以PNG格式將圖像輸出到瀏覽器*/
ImagePNG($distortion_im);
/*銷毀圖像*/
ImageDestroy($distortion_im);
ImageDestroy($im);
『叄』 thinkphp 怎麼做登錄驗證
登錄無非就是驗證用戶名密碼以及驗證碼是否正確,我們可以新建一個CommonAction的公共類,用來校驗許可權,其他所有類繼承此類。該類內部寫一個初始化方法,用於驗證,這一講先不詳細講解。繼續說登錄,由於登錄是公開模塊的方法,所以可以新建一個PublicAction類,用於公共的免驗證方法,同時在配置文件中添加
'NOT_AUTH_MODULE'=>'Public',//默認不需要認證的模塊
'USER_AUTH_GATEWAY'=>'/Public/login',//默認的認證網關
然後開始編寫Public類,具體代碼如下:
<?php
{
//驗證碼顯示
publicfunction verify(){
import("ORG.Util.Image");
Image::buildImageVerify(4,1,"png",100,28,"verify");
}
//驗證是否賬號密碼
function checklogin(){
//此處多餘可自行改為Model自動驗證
if(empty($_POST['username'])){
            $this->error('帳號錯誤!');
}elseif (empty($_POST['password'])){
            $this->error('密碼必須!');
}elseif (empty($_POST['verify'])){
            $this->error('驗證碼必須!');
}
        $map=array();
        $map['username']=$_POST['username'];
        $map['status']=array('gt',0);
if($_SESSION['verify']!= md5($_POST['verify'])){
            $this->error('驗證碼錯誤!');
}
import('ORG.Util.RBAC');
//C('USER_AUTH_MODEL','User');
//驗證賬號密碼
         $authInfo=RBAC::authenticate($map);
if(empty($authInfo)){
            $this->error('賬號不存在或者被禁用!');
}else{
if($authInfo['password']!=md5($_POST['password'])){
                $this->error('賬號密碼錯誤!');
}else{
            $_SESSION[C('USER_AUTH_KEY')]=$authInfo['id'];//記錄認證標記,必須有。其他信息根據情況取用。
            $_SESSION['email']=$authInfo['email'];
            $_SESSION['nickname']=$authInfo['nickname'];
            $_SESSION['user']=$authInfo['username'];
            $_SESSION['last_login_date']=$authInfo['last_login_date'];
            $_SESSION['last_login_ip']=$authInfo['last_login_ip'];
//判斷是否為超級管理員
if($authInfo['username']=='admin'){
                $_SESSION[C('ADMIN_AUTH_KEY')]=true;
}
//以下操作為記錄本次登錄信息
            $user=M('User');
            $lastdate=date('Y-m-d H:i:s');
            $data=array();
            $data['id']=$authInfo['id'];
            $data['last_login_date']=$lastdate;
            $data['last_login_ip']=$_SERVER["REMOTE_ADDR"];
            $user->save($data);
            RBAC::saveAccessList();//用於檢測用戶許可權的方法,並保存到Session中
            $this->assign('jumpUrl',.'/Index/index');
            $this->success('登錄成功!');
        }
    }
}
//退出登錄操作
function logout(){
if(!empty($_SESSION[C('USER_AUTH_KEY')])){
            unset($_SESSION[C('USER_AUTH_KEY')]);
            $_SESSION=array();
            session_destroy();
            $this->assign('jumpUrl',/Code.'/login');
            $this->success('登出成功');
        }else{
            $this->error('已經登出了');
        }
    }
}
以上代碼僅實現功能,沒有做優化,有些驗證的操作可以放到model,session也不用一 一賦值,用數組即可,我想已經入門的應該可以自己改的更好。
『肆』 驗證碼怎麼用php實現
<?php 
          /* 
          *  Filename:  authpage.php 
          */ 
   
          srand((double)microtime()*1000000); 
   
          //驗證用戶輸入是否和驗證碼一致 
            if(isset($HTTP_POST_VARS['authinput']))  
            { 
                if(strcmp($HTTP_POST_VARS['authnum'],$HTTP_POST_VARS['authinput'])==0) 
   
                    echo "驗證成功!"; 
                else 
                    echo "驗證失敗!"; 
            } 
           
          //生成新的四位整數驗證碼 
            while(($authnum=rand()%10000)<1000);  
          ?> 
            <form action=authpage.php method=post> 
            <table> 
                請輸入驗證碼:<input type=text name=authinput style="width: 
        80px"><br> 
                <input type=submit name="驗證" value="提交驗證碼"> 
                <input type=hidden name=authnum value=<? echo $authnum; ?>> 
                <img src=authimg.php?authnum=<? echo $authnum; ?>> 
            </table> 
            </form> 
   
        代碼二: 
   
        <?php 
          /* 
          *  Filename:  authimg.php 
          *  Author:  hutuworm 
          *  Date:  2003-04-28 
          *  @Copyleft  hutuworm.org 
          */ 
   
          //生成驗證碼圖片 
            Header("Content-type: image/PNG");  
            srand((double)microtime()*1000000); 
            $im = imagecreate(58,28); 
            $black = ImageColorAllocate($im, 0,0,0); 
            $white = ImageColorAllocate($im, 255,255,255); 
            $gray = ImageColorAllocate($im, 200,200,200); 
            imagefill($im,68,30,$gray); 
   
          //將四位整數驗證碼繪入圖片 
            imagestring($im, 5, 10, 8, $HTTP_GET_VARS['authnum'], $black); 
   
            for($i=0;$i<50;$i++)  //加入干擾象素 
            { 
                imagesetpixel($im, rand()%70 , rand()%30 , $black); 
            } 
   
            ImagePNG($im); 
            ImageDestroy($im); 
        ?>
『伍』 PHP中模擬登錄的驗證碼問題應該如何解決
基本思路:
首先獲取一個cookies值,再帶著這個cookies去獲取驗證碼圖片,你再帶著驗證碼值和登錄數據去模擬post登錄。下面是一個模擬獲取驗證碼的。
這里忽略獲取cookies的過程。注意文件為UTF-8無BOM格式
?php
header('Content-Type:image/png');
$url="http://hbyw.e21.e.cn/global/gd.php";//圖片鏈接
$ch=curl_init();
//Cookie:PHPSESSID=
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_COOKIE,'PHPSESSID=');
curl_setopt($ch,CURLOPT_RETURNTRANSFER,0);
curl_setopt($ch,CURLOPT_TIMEOUT,0);//忽略超時
curl_setopt($ch,CURLOPT_NOBODY,false);
$str=curl_exec($ch);
curl_close($ch);
『陸』 我的php代碼中登陸界面加一個驗證碼,如何實現
php登陸頁面+驗證碼的實現,參考如下:
1、首先新建一個php站點;

『柒』 php curl模擬登陸兩次請求驗證碼的問題,登陸時請求一次驗證碼並獲取cook
取得驗證碼cookie 然後帶著cookie post登錄 再保存cookie
