程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> 關於PHP編程 >> php實現的簡單中文驗證碼功能示例,

php實現的簡單中文驗證碼功能示例,

編輯:關於PHP編程

php實現的簡單中文驗證碼功能示例,


本文實例講述了php實現的簡單中文驗證碼功能。分享給大家供大家參考,具體如下:

img.php

<?php
session_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 不大於100
for($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>

運行效果圖如下:

更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP圖形與圖片操作技巧匯總》、《PHP基本語法入門教程》、《PHP運算與運算符用法總結》、《php面向對象程序設計入門教程》、《PHP網絡編程技巧總結》、《PHP數組(Array)操作技巧大全》、《php字符串(string)用法總結》、《php+mysql數據庫操作入門教程》及《php常見數據庫操作技巧匯總》

希望本文所述對大家PHP程序設計有所幫助。

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved