php驗證碼實例
1. php 驗證碼 使用
你訪問http://你地址/上述程序的文件名.php?action=verifycode
這樣就可以看到圖片了,同理插入到登錄框用
<imgsrc="http://你地址/上述程序的文件名.php?action=verifycode"/>
就可以了
-------------------------
leboc代碼你都沒看懂,$_GET["action"]=="verifycode"是判斷動作的,當動作為verifycode的時候調用rand_create()函數產生一個隨機驗證碼.不是你說的
"每個驗證碼不會都是"verifycode"?吧?".而是每次調用驗證碼都要用verifycode
補充回答-----------------------------------
彈出迅雷?請確認你的電腦支持PHP,的運行環境.
我用你的代碼保存為c.php,保存在伺服器上,
同時,建立一個1.html,代碼內容僅為
<imgsrc="c.php?action=verifycode"/>.存放與c.php同一目錄.
運行後是可以正常顯示驗證碼的.

2. PHP如何生成加減演算法方式的驗證碼
<?php  
namespace mobile\components;
/** 
* @author fenghuo 
* 
* 改造的加減法驗證類 
* 使用示例 VerifyCode::get(1,2); 
* 驗證示例 VerifyCode::check($code); 
*/  
class VerifyCode  
{  
/** 
* php驗證碼 
*/  
public static function get($one,$two,$prefix = '', $font_size = 28)  
{  
//文件頭...  
ob_get_clean();
header("Content-type: image/png;charset=utf-8;");  
//創建真彩色白紙  
$width            = $font_size*5;  
$height           = $font_size+1;  
$im               = @imagecreatetruecolor($width, $height) or die("建立圖像失敗");  
//獲取背景顏色  
$background_color = imagecolorallocate($im, 255, 255, 255);  
//填充背景顏色  
imagefill($im, 0, 0, $background_color);  
//獲取邊框顏色  
$border_color     = imagecolorallocate($im, 200, 200, 200);  
//畫矩形,邊框顏色200,200,200  
imagerectangle($im,0,0,$width - 1, $height - 1,$border_color);  
//逐行炫耀背景,全屏用1或0  
for($i = 2;$i < $height - 2;$i++) {  
//獲取隨機淡色  
$line_color = imagecolorallocate($im, rand(200,255), rand(200,255), rand(200,255));  
//畫線  
imageline($im, 2, $i, $width - 1, $i, $line_color);  
}  
//設置印上去的文字  
$firstNum  = $one;  
$secondNum = $two;  
$actionStr = $firstNum > $secondNum ? '-' : '+';  
//獲取第1個隨機文字  
$imstr[0]["s"] = $firstNum;  
$imstr[0]["x"] = rand(2, 5);  
$imstr[0]["y"] = rand(1, 4);  
//獲取第2個隨機文字  
$imstr[1]["s"] = $actionStr;  
$imstr[1]["x"] = $imstr[0]["x"] + $font_size - 1 + rand(0, 1);  
$imstr[1]["y"] = rand(1,5);  
//獲取第3個隨機文字  
$imstr[2]["s"] = $secondNum;  
$imstr[2]["x"] = $imstr[1]["x"] + $font_size - 1 + rand(0, 1);  
$imstr[2]["y"] = rand(1, 5);  
//獲取第3個隨機文字  
$imstr[3]["s"] = '=';  
$imstr[3]["x"] = $imstr[2]["x"] + $font_size - 1 + rand(0, 1);  
$imstr[3]["y"] = 3;  
//獲取第3個隨機文字  
$imstr[4]["s"] = '?';  
$imstr[4]["x"] = $imstr[3]["x"] + $font_size - 1 + rand(0, 1);  
$imstr[4]["y"] = 3;  
//文字  
$text = '';  
//寫入隨機字串  
for($i = 0; $i < 5; $i++) {  
//獲取隨機較深顏色  
$text_color = imagecolorallocate($im, rand(50, 180), rand(50, 180), rand(50, 180));  
$text .= $imstr[$i]["s"];  
//畫文字  
imagechar($im, $font_size, $imstr[$i]["x"], $imstr[$i]["y"], $imstr[$i]["s"], $text_color);  
}  
session_start();
$_SESSION[$prefix.'verifycode'] = $firstNum > $secondNum ? ($firstNum - $secondNum) : ($firstNum + $secondNum);  
//顯示圖片  
ImagePng($im);  
//銷毀圖片  
ImageDestroy($im);  
}  
public static function check($code)  
{  
if(trim($_SESSION[$prefix.'verifycode']) == trim($code)) {  
return true;  
} else {  
return false;  
}  
}
3. 驗證碼怎麼用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); 
        ?>
4. 如何用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);
5. php寫了驗證碼,但打開的時候驗證碼圖片打不開。。
驗證碼網上大把的!隨便搜了一個!
<?php
Header("Content-type: image/gif");
/*
* 初始化
*/
$border = 0; //是否要邊框 1要:0不要
$how = 4; //驗證碼位數
$w = $how*15; //圖片寬度
$h = 20; //圖片高度
$fontsize = 5; //字體大小
$alpha = "abcdefghijkmnopqrstuvwxyz"; //驗證碼內容1:字母
$number = "023456789"; //驗證碼內容2:數字
$randcode = ""; //驗證碼字元串初始化
srand((double)microtime()*1000000); //初始化隨機數種子
$im = ImageCreate($w, $h); //創建驗證圖片
/*
* 繪制基本框架
*/
$bgcolor = ImageColorAllocate($im, 255, 255, 255); //設置背景顏色
ImageFill($im, 0, 0, $bgcolor); //填充背景色
if($border)
{
    $black = ImageColorAllocate($im, 0, 0, 0); //設置邊框顏色
    ImageRectangle($im, 0, 0, $w-1, $h-1, $black);//繪制邊框
}
/*
* 逐位產生隨機字元
*/
for($i=0; $i<$how; $i++)
{   
    $alpha_or_number = mt_rand(0, 1); //字母還是數字
    $str = $alpha_or_number ? $alpha : $number;
    $which = mt_rand(0, strlen($str)-1); //取哪個字元
    $code = substr($str, $which, 1); //取字元
    $j = !$i ? 4 : $j+15; //繪字元位置
    $color3 = ImageColorAllocate($im, mt_rand(0,100), mt_rand(0,100), mt_rand(0,100)); //字
符隨即顏色
    ImageChar($im, $fontsize, $j, 3, $code, $color3); //繪字元
    $randcode .= $code; //逐位加入驗證碼字元串
}
/*
* 添加干擾
*/
for($i=0; $i<5; $i++)//繪背景干擾線
{   
    $color1 = ImageColorAllocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); //干
擾線顏色
    ImageArc($im, mt_rand(-5,$w), mt_rand(-5,$h), mt_rand(20,300), mt_rand(20,200), 55, 44, 
$color1); //干擾線
}   
for($i=0; $i<$how*40; $i++)//繪背景干擾點
{   
    $color2 = ImageColorAllocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255)); //干
擾點顏色 
    ImageSetPixel($im, mt_rand(0,$w), mt_rand(0,$h), $color2); //干擾點
}
//把驗證碼字元串寫入session
session_start();
$_SESSION['randcode'] = $randcode;
/*繪圖結束*/
Imagegif($im);
ImageDestroy($im);
/*繪圖結束*/
?>
6. 在php中進行驗證碼的判斷,對的話提交,錯誤的話,表單不提交,如何進行實現
PHP驗證程序,假設資料庫存儲的是用戶名和 md5 加密後的密碼: 
  // 表單提交後... 
  $posts = $_POST; 
  // 清除一些空白符號 
  foreach ($posts as $key => $value) 
  { 
  $posts[$key] = trim($value); 
  } 
  $password = md5($posts["password"]); 
  $username = $posts["username"]; 
  $query = "SELECT `username` FROM `user` WHERE `password` = '$password'"; 
  // 取得查詢結果 
  $userInfo = $DB->getRow($query); 
  if (!empty($userInfo)) 
  { 
  if ($userInfo["username"] == $username) 
  { 
  // 當驗證通過後,啟動 session 
  session_start(); 
  // 注冊登陸成功的 admin 變數,並賦值 true 
  $_session["admin"] = true; 
  } 
  else 
  { 
  die("用戶名密碼錯誤"); 
  } 
  } 
  else 
  { 
  die("用戶名密碼錯誤"); 
  } 
  我們在需要用戶驗證的頁面啟動 session,判斷是否登陸: 
  // 防止全局變數造成安全隱患 
  $admin = false; 
  // 啟動會話,這步必不可少 
  session_start(); 
  // 判斷是否登陸 
  if (isset($_SESSION["admin"]) && $_session["admin"] === true) 
  { 
  echo "您已經成功登陸"; 
  } 
  else 
  { 
  // 驗證失敗,將 $_session["admin"] 置為 false 
  $_session["admin"] = false; 
  die("您無權訪問"); 
  } 
  ?>
7. php如何使用隨機函數rand()生成一個數字驗證碼
參考這個
$code="";
//畫布
$image=imagecreatetruecolor(80, 25);
imagefill($image, 0, 0, imagecolorallocate($image, 255, 255, 255));
for($i=0;$i<4;$i++){
    $rand_color=imagecolorallocate($image, rand(0,155), rand(0,155), rand(0,155));
    $code_tmp=dechex(rand(1,15));
    $code.=$code_tmp;
    imagestring($image, rand(4,5), rand($i*20,$i*20+20-6), rand(0,13),$code_tmp , $rand_color);
    //干擾線
    imageline($image, rand($i*20,$i*20+20), rand(0,25), rand($i*20,$i*20+20), rand(0,25), $rand_color);
    imageline($image, rand($i*20,$i*20+20), rand(0,25), rand($i*20,$i*20+20), rand(0,25), $rand_color);
}
//保存
session_start();
$_SESSION['yzm']=$code;
session_write_close();
header("content-type:image/png");
imagepng($image);
imagedestroy($image);
