溫馨提示×

溫馨提示×

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

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

怎么在php項目中實(shí)現(xiàn)一個中文驗(yàn)證碼功能

發(fā)布時間:2021-01-30 14:34:40 來源:億速云 閱讀:148 作者:Leah 欄目:開發(fā)技術(shù)

這期內(nèi)容當(dāng)中小編將會給大家?guī)碛嘘P(guān)怎么在php項目中實(shí)現(xiàn)一個中文驗(yàn)證碼功能,文章內(nèi)容豐富且以專業(yè)的角度為大家分析和敘述,閱讀完這篇文章希望大家可以有所收獲。

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 坐標(biāo)
imagettftext($image, 12, 0, 20, 20, $color, 'MSYH.TTF', $str);
//輸出圖片
header("Content-type: image/jpeg;charset=utf-8");
imagejpeg($image);
/*修改eclipse的配置,可以使得eclipse的新建項目的默認(rèn)編碼直接為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 "驗(yàn)證碼正確:".$_SESSION[check_pic];
    } else {
      echo "驗(yàn)證碼錯誤:".$_SESSION[check_pic];
    }
  }
?>
<form action="" method="post">
  <img  src="img.php"><br/>
  <input type="text" name="check"><br/>
  <input type="submit" value="提交">
</form>

運(yùn)行效果圖如下:

怎么在php項目中實(shí)現(xiàn)一個中文驗(yàn)證碼功能

上述就是小編為大家分享的怎么在php項目中實(shí)現(xiàn)一個中文驗(yàn)證碼功能了,如果剛好有類似的疑惑,不妨參照上述分析進(jìn)行理解。如果想知道更多相關(guān)知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細(xì)節(jié)

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

php
AI