php實現簡單中文驗證碼功能的方法

來源:互聯網
上載者:User
這篇文章主要介紹了php實現的簡單中文驗證碼功能,結合執行個體形式分析了php中文驗證碼功能的實現步驟與操作方法,包括圖形建立、編碼操作、session操作等相關技巧,需要的朋友可以參考下

具體如下:

img.php


<?phpsession_start();/*for($i=0;$i<4;$i++) {  $rand .= dechex(rand(1,15));}$_SESSION[check_pic] = $rand;*/$image = imagecreatetruecolor(100, 30);$bg = imagecolorallocate($image, 0, 0, 0);$color = imagecolorallocate($image, 255, 255, 255);//imagestring($image, rand(1,6), rand(3,60), rand(3,15), $rand, $color);for($i=0;$i<3;$i++) {  $color2 = imagecolorallocate($image, rand(0,255), rand(0,255),rand(0,255));  imageline($image, rand(0,100), 0, 100, 30, $color2);}//rand() ---->0-max 不大於100for($i=0;$i<200;$i++) {  imagesetpixel($image, rand()%100, rand()%30, $color2);}//$str = iconv("gbk", "utf-8", "中");$str = "中國";$_SESSION[check_pic] = $str;//解決中文,頁面本身為utf-8$str = mb_convert_encoding($str, "html-entities","utf-8" );//2:字型大小 3:傾斜角度 x , y 座標imagettftext($image, 12, 0, 20, 20, $color, 'MSYH.TTF', $str);//輸出圖片header("Content-type: image/jpeg;charset=utf-8");imagejpeg($image);/*修改eclipse的配置,可以使得eclipse的建立項目的預設編碼直接為UTF-8在功能表列的Window->Preferences->General->Workspace->Text file encoding將其改為UFT-8即可。*/?>


sub.php


<?php  header("Content-type: text/html;charset=utf-8");  session_start();  if($_POST[check]) {    if($_POST[check]==$_SESSION[check_pic]) {      echo "驗證碼正確:".$_SESSION[check_pic];    } else {      echo "驗證碼錯誤:".$_SESSION[check_pic];    }  }?><form action="" method="post">  <img alt="" src="img.php"><br/>  <input type="text" name="check"><br/>  <input type="submit" value="提交"></form>


運行如下:


以上就是本文的全部內容,希望對大家的學習有所協助。


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.