搜尋
首頁後端開發php教程php影像驗證碼生成

php影像驗證碼生成

Jun 28, 2017 pm 01:34 PM
php產生驗證

這篇文章主要為大家詳細 介紹了php影像驗證碼的產生程式碼,具有一定的參考價值,有興趣的夥伴們可以參考一下

本文實例為大家分享了php封裝的一個產生影像驗證碼,供大家參考,具體內容如下

一、程式碼

#index..php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>js+GD2函数制作无刷新验证码</title> 
<link rel="stylesheet" type="text/css" href="css/style.css" rel="external nofollow" > 
</head> 
<body> 
<p style="width: 750px; border: 1px solid #0D6C8C; text-align: left"> 
 <p style="width:100%; height:25px; border:1px #FFFFFF solid; background:url(img/t-background.gif); color:#FFFFFF; padding-top:5px; padding-left:20px; text-align:left">填写用户注册信息</p> 
 <p style="width:400px; text-align:left; padding-left:50px; padding-top:15px; float:left"> 
<script language="javascript"> 
  function chkinput(form) 
  { 
    if (form.username.value==&#39;&#39;){ 
      alert(&#39;请输入用户名!&#39;); 
      form.username.focus(); 
      return false; 
    } 
     
    if (form.password.value==&#39;&#39;){ 
      alert(&#39;请输入注册密码!&#39;); 
      form.password.focus(); 
      return false; 
    }   
     
    if (form.defPassword.value==&#39;&#39;){ 
      alert(&#39;请输入确认密码!&#39;); 
      form.defPassword.focus(); 
      return false; 
    } 
     
    if (form.password.value.length<6){ 
      alert(&#39;密码长度应大于6位!&#39;); 
      form.password.focus(); 
      return false; 
    } 
     
    if (form.password.value!=form.defPassword.value){ 
      alert(&#39;密码与确认密码不同!&#39;); 
      form.password.focus(); 
      return false; 
    } 
     
    if (form . email . value == "") { 
      alert("请输入E-mail地址!"); 
      form . email . select(); 
      return (false); 
    } 
    var i= form . email . value . indexOf("@"); 
    if (i < 0) { 
      alert("请输入正确的E-mail地址!"); 
      form . email . select(); 
      return (false); 
    } 
    if (form . tel . value == "") { 
      alert(&#39;请输入电话号码!&#39;); 
      form . tel . focus(); 
      return (false); 
    } 
    var t=/^([0-9]|[\-])+$/g; 
    if (! t . test(form . tel . value) || form . tel . value . length > 18 || form . tel . value . length < 7) { 
      alert(&#39;电话格式输入有误!&#39;); 
      form . tel . focus(); 
      return (false); 
    } 
                   
    if (form.address.value==&#39;&#39;){ 
      alert(&#39;请输入联系地址!&#39;); 
      form.address.focus(); 
      return false; 
    } 
     
    if (form.validatorCode.value==&#39;&#39;){ 
      alert(&#39;请输入验证码!&#39;); 
      form.validatorCode.focus(); 
      return false; 
    } 
     
    if (form.validatorCode.value!=form.defValidatorCode.value) { 
      alert(&#39;验证码输入有误!&#39;); 
      form.validatorCode.focus(); 
      return false; 
    } 
     
    return true; 
   
  } 
</script> 
 
<form name="form1" method="post" action="success.php" onSubmit="return chkinput(this)"> 
用户名:  <input type="text" name="username" size="30" class="input" /><br><br> 
密  码:  <input type="password" name="password" size="30" class="input" /><br><br> 
确认密码:<input type="password" name="defPassword" size="30" class="input"><br><br> 
E-mail:  <input type="text" name="email" size="30" class="input" /><br><br> 
电话:    <input type="text" name="tel" size="30" class="input" /><br><br> 
联系地址:<input type="text" name="address" size="30" class="input" /><br><br> 
验证码:  <input type="text" name="validatorCode" size="10" class="input" /> 
<input type="hidden" name="defValidatorCode" value="" /> 
   
  <script language="javascript"> 
   var num1=Math.round(Math.random()*10000000); 
   var num=num1.toString().substr(0,4); 
   document.write("<img  src="/static/imghwm/default1.png"  data-src="TestCode.php?code="  class="lazy"  name=codeimg src=&#39;TestCode.php?code="+num+"&#39; alt="php影像驗證碼生成" >"); 
   form1.defValidatorCode.value=num; 
   function reCode(){ 
      var num1=Math.round(Math.random()*10000000); 
      var num=num1.toString().substr(0,4); 
      document.codeimg.+num; 
      form1.defValidatorCode.value=num; 
    } 
  </script> 
  <a href="javascript:reCode()" rel="external nofollow" class="a1">看不清</a> 
  <br><br> 
  <input type="submit" value="提交">  <input type="reset" value=" 重写"> 
</form> 
 </p> 
 <p style="width:300px; float:right; padding-top:20px"> 
   <p style="width:285px; height:250px; border:1px dotted #999999; background:#E9F2F7; OVERFLOW-X: auto; OVERFLOW-Y:auto; text-align:left; line-height:20px"> 
    用户在使用***网站网络服务过程中,必须遵循以下原则:<br /> 
    1、遵守中国有关的法律和法规。不得利用本站危害国家安全、泄露国家秘密,不得侵犯国家社会集体的和公民的合法权益。不得利用***网站网络服务系统进行任何不利于***网站的行为。<br /> 
   2、遵守所有与网络服务有关的网络协议、规定和程序。<br /> 
   3、不干扰或混乱网络服务。<br /> 
   4、不得为任何非法目的而使用本网络服务系统。<br /> 
   5、不得传播任何虚假的、骚扰性的、中伤他人的、辱骂性的、恐吓性的、庸俗的或其他任何非法的信息资料。<br /> 
   6、不得侵犯其他任何第三方的专利权、著作权、商标权、名誉权或其他任何合法权益。<br /> 
   7、不得在论坛BBS或留言簿发表任何与政治相关的信息。 <br /> 
   8、遵守所有使用网站服务的网络协议、规定、程序和惯例。<br /> 
   9、如发现任何非法使用用户帐号,应立即通告***网站。<br /> 
  10、如用户在使用网络服务时违反上述任何规定,***网站有权要求用户改正或直接取消该帐号并停止为该用户提供相关的网络服务。<br />  
   </p> 
 </p> 
  
</p> 
<?php 
  require_once &#39;footer.php&#39;; 
?> 
</body> 
</html>

TestCode.php

<?php 
  class TestCode{//创建类名为TestCode 
   private $width; 
   private $height; 
   private $str; 
   private $im; 
   private $strColor;    
   function construct($width,$height){ 
    $this->width=$width; 
    $this->height=$height; 
    $this->str=$_GET[&#39;code&#39;]; 
    $this->createImage(); 
   } 
   function createImage(){ 
    $this->im=imagecreate($this->width,$this->height);//创建画布 
    imagecolorallocate($this->im,200,200,200);//为画布添加颜色 
    for($i=0;$i<4;$i++){//循环输出四个数字 
     $this->strColor=imagecolorallocate($this->im,rand(0,100),rand(0,100),rand(0,100)); 
     imagestring($this->im,rand(3,5),$this->width/4*$i+rand(5,10),rand(2,5),$this->str[$i],$this->strColor); 
    } 
    for($i=0;$i<200;$i++){//循环输出200个像素点 
     $this->strColor=imagecolorallocate($this->im,rand(0,255),rand(0,255),rand(0,255)); 
     imagesetpixel($this->im,rand(0,$this->width),rand(0,$this->height),$this->strColor); 
    } 
   } 
   function show(){// 
    header(&#39;content-type:image/png&#39;);//定义输出为图像类型 
    imagepng($this->im);//生成图像 
    imagedestroy($this->im);//销毁图像释放内存 
   } 
  } 
  $image=new TestCode(80,20);//将类实例化为对象 
  $image->show();//调用函数 
?>

 二、運行結果

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

以上是php影像驗證碼生成的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
可以在PHP會話中存儲哪些數據?可以在PHP會話中存儲哪些數據?May 02, 2025 am 12:17 AM

phpsessionscanStorestrings,數字,數組和原始物。

您如何開始PHP會話?您如何開始PHP會話?May 02, 2025 am 12:16 AM

tostartaphpsession,usesesses_start()attheScript'Sbeginning.1)placeitbeforeanyOutputtosetThesessionCookie.2)useSessionsforuserDatalikeloginstatusorshoppingcarts.3)regenerateSessiveIdStopreventFentfixationAttacks.s.4)考慮使用AttActAcks.s.s.4)

什麼是會話再生,如何提高安全性?什麼是會話再生,如何提高安全性?May 02, 2025 am 12:15 AM

會話再生是指在用戶進行敏感操作時生成新會話ID並使舊ID失效,以防會話固定攻擊。實現步驟包括:1.檢測敏感操作,2.生成新會話ID,3.銷毀舊會話ID,4.更新用戶端會話信息。

使用PHP會話時有哪些性能考慮?使用PHP會話時有哪些性能考慮?May 02, 2025 am 12:11 AM

PHP会话对应用性能有显著影响。优化方法包括:1.使用数据库存储会话数据,提升响应速度;2.减少会话数据使用,只存储必要信息;3.采用非阻塞会话处理器,提高并发能力;4.调整会话过期时间,平衡用户体验和服务器负担;5.使用持久会话,减少数据读写次数。

PHP會話與Cookie有何不同?PHP會話與Cookie有何不同?May 02, 2025 am 12:03 AM

PHPsessionsareserver-side,whilecookiesareclient-side.1)Sessionsstoredataontheserver,aremoresecure,andhandlelargerdata.2)Cookiesstoredataontheclient,arelesssecure,andlimitedinsize.Usesessionsforsensitivedataandcookiesfornon-sensitive,client-sidedata.

PHP如何識別用戶的會話?PHP如何識別用戶的會話?May 01, 2025 am 12:23 AM

phpIdentifiesauser'ssessionSessionSessionCookiesAndSessionId.1)whiwsession_start()被稱為,phpgeneratesainiquesesesessionIdStoredInacookInAcookInAcienamedInAcienamedphpsessIdontheuser'sbrowser'sbrowser.2)thisIdallowSphptpptpptpptpptpptpptpptoretoreteretrieetrieetrieetrieetrieetrieetreetrieetrieetrieetrieetremthafromtheserver。

確保PHP會議的一些最佳實踐是什麼?確保PHP會議的一些最佳實踐是什麼?May 01, 2025 am 12:22 AM

PHP會話的安全可以通過以下措施實現:1.使用session_regenerate_id()在用戶登錄或重要操作時重新生成會話ID。 2.通過HTTPS協議加密傳輸會話ID。 3.使用session_save_path()指定安全目錄存儲會話數據,並正確設置權限。

PHP會話文件默認存儲在哪裡?PHP會話文件默認存儲在哪裡?May 01, 2025 am 12:15 AM

phpsessionFilesArestoredIntheDirectorySpecifiedBysession.save_path,通常是/tmponunix-likesystemsorc:\ windows \ windows \ temponwindows.tocustomizethis:tocustomizEthis:1)useession_save_save_save_path_path()

See all articles

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

這個專案正在遷移到osdn.net/projects/mingw的過程中,你可以繼續在那裡關注我們。 MinGW:GNU編譯器集合(GCC)的本機Windows移植版本,可自由分發的導入函式庫和用於建置本機Windows應用程式的頭檔;包括對MSVC執行時間的擴展,以支援C99功能。 MinGW的所有軟體都可以在64位元Windows平台上運作。

EditPlus 中文破解版

EditPlus 中文破解版

體積小,語法高亮,不支援程式碼提示功能

Atom編輯器mac版下載

Atom編輯器mac版下載

最受歡迎的的開源編輯器

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!