溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

使用php怎么實現(xiàn)一個網(wǎng)頁端驗證碼功能

發(fā)布時間:2021-02-04 17:06:52 來源:億速云 閱讀:144 作者:Leah 欄目:開發(fā)技術(shù)

這篇文章將為大家詳細講解有關(guān)使用php怎么實現(xiàn)一個網(wǎng)頁端驗證碼功能,文章內(nèi)容質(zhì)量較高,因此小編分享給大家做個參考,希望大家閱讀完這篇文章后對相關(guān)知識有一定的了解。

文件目錄結(jié)構(gòu):

    |----------phpyanzheng  項目文件夾

      |----fonttype 文件夾放字體文件,字體文件一般以 .ttf和.otf結(jié)尾

      |----1.php  里面放php語言代碼

      |----index.html  里面放網(wǎng)頁布局

1.php

<?php
  header('content-type:image/jpeg');//定義一下代碼以jpeg文件來解析
  $width = 120;//定義了圖像的寬
  $height = 40;//定義了圖像的高
  $element = array('q','w','e','r','t','y','u','i','o','p','l','k','j','h','g','f','d','s','a','z','x',
  'c','v','b','n','m','1','2','3','4','5','6','7','8','9','0','Q','W','E','R','T','Y','U','I','O','P','A'
  ,'S','D','F','G','H','J','K','L','M','N','B','V','C','X','Z');//定義一個顯示文本的數(shù)組
  $string = '';
  for($i=0;$i<4;$i++){
    $string.=$element[rand(0,count($element)-1)];//隨機產(chǎn)生四個文本目標
  }
  $img = imagecreatetruecolor($width, $height);//設(shè)置驗證區(qū)寬高
  $colorBg = imagecolorallocate($img,rand(185,255),rand(185,255),rand(185,255));//產(chǎn)生200-255的隨機數(shù)
  $colorBorder = imagecolorallocate($img,rand(50,100),rand(50,100),rand(50,100));//邊框顏色
  $colorDian = imagecolorallocate($img,rand(0,100),rand(0,100),rand(0,100));//背景小點的顏色
  $colorLine = imagecolorallocate($img,rand(0,255),rand(0,255),rand(0,255));
  $colorString = imagecolorallocate($img,rand(20,80),rand(20,80),rand(20,80));
  imagefill($img, 0, 0, $colorBg);//設(shè)置位置和背景顏色
  imagerectangle($img,0,0,$width-1,$height-1,$colorBorder);//畫一個邊框
  for($i=0;$i<200;$i++){//循環(huán)出200個干擾點
    imagesetpixel($img, rand(0, $width-1), rand(0, $height-1), $colorDian);
  }
  for($i=0;$i<round(5);$i++){//循環(huán)出5條干擾線
    imageline($img,rand(0,$width/2),rand(0,$height/2),rand($width/2,$width),rand($height/2,$height),$colorLine);
  }
  //imagestring($img, 4, 0, 0,'a5dg', $colorString);
  
  imagettftext($img,25,rand(5, 15),rand(0,40),39,$colorString,'fonttype/PrincetownStd.otf',$string);
  //    圖像名稱  字體大小 傾斜角度 起始位置X軸 起始位置Y軸 顏色 字體位置 顯示的內(nèi)容
  imagejpeg($img);//輸出圖像
?>

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>孫三峰--驗證碼</title>
    <script type="text/javascript" src="js/jquery-1.1.0.js" ></script>
  </head>
  <body>
    <!--注意:原文中對應(yīng)的ip地址是自己電腦端ip,如果不能正常運行,請修改ip-->
    <p>
      <label class="lbright">驗證碼:</label> 
      <span> 
        <input type="text" name="validcode"  id="validcode"/> 
        <img id="codePic" src="http://192.168.1.113:8601/PHP/phpyanzheng/1.php" width="120" height="40"/>          
      </span>         
      <span id="codePic" onclick="getPic();">看不清楚,換一張</span> <!---->
    </p>     
  </body>
  <script type="text/javascript"> 
     function getPic(){ 
         $("#codePic").attr("src","http://192.168.1.113:8601/PHP/phpyanzheng/1.php?flag="+Math.random()); <!--局部刷新--> 
     }; 
  </script>
</html>

關(guān)于使用php怎么實現(xiàn)一個網(wǎng)頁端驗證碼功能就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

php
AI