Home  >  Article  >  Backend Development  >  How to solve the php verification garbled problem

How to solve the php verification garbled problem

藏色散人
藏色散人Original
2020-11-03 14:19:291764browse

The solution to php verifying garbled characters: first open the file code; then use the function "mb_convert_encoding($code,"utf-8","gbk");" to convert and encode the Chinese.

How to solve the php verification garbled problem

Recommended: "PHP Video Tutorial"

php Chinese verification code, Chinese garbled code

File code:

1.cn.php

<?php
/*********************************
* Code by Gently
* 24/07/07
*严正声明:验证码为程序随机生成,“某种巧合”的词语组合属于正常现象
,
*某些别有用心的人不要借题发挥!
*Power by ZendStudio.Net
*(http://www.zendstudio.net/)
*********************************/
session_start();
header("Content-type: image/PNG");
$w=180;
$h=60;
$fontface="fonts/GB2312.ttf"; //字体文件linux支持直接使用windows的字体
$str = "据了解受意大利卡拉拉市文化部部长乔凡娜贝尔纳迪尼邀请云浮市美术家协会主席叶仲桥带着他的四十多幅作品前往卡拉拉市在当地的大理石博物馆举办画展其中欢乐兰寨花开时节两幅作品赠予卡拉拉市博物馆永久收藏全国书法家协会副会长陈永正欣然为画展题字叶仲桥中国画展此次画展不但获得卡拉拉市政府高度重视与支持还引起当地市民及华人华侨的热情关注卡拉拉市的新闻媒体记者专门采访了叶仲桥当地报纸以图文形式大篇幅报道了此次画展的情况";
$code="";
for($i=0;$i<4;$i++){
$Xi=mt_rand(0,strlen($str)/2);
if($Xi%2) $Xi+=1;
$code.=substr($str,$Xi,2);
}
$_SESSION["checkcode"] = mb_convert_encoding($code,"utf-8","gbk");
$im=imagecreatetruecolor($w,$h);
$bkcolor=imagecolorallocate($im,250,250,250);
imagefill($im,0,0,$bkcolor);
/***添加干扰***/
for($i=0;$i<15;$i++){
$fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand
(0,255),mt_rand(0,255));
imagearc($im,mt_rand(-10,$w),mt_rand(-10,$h),mt_rand
(30,300),mt_rand(20,200),55,44,$fontcolor);
}
for($i=0;$i<255;$i++){
$fontcolor=imagecolorallocate($im,mt_rand(0,255),mt_rand
(0,255),mt_rand(0,255));
imagesetpixel($im,mt_rand(0,$w),mt_rand(0,$h),$fontcolor);
}
/***********内容*********/
for($i=0;$i<4;$i++){
$fontcolor=imagecolorallocate($im,mt_rand(0,120),mt_rand
(0,120),mt_rand(0,120)); //这样保证随机出来的颜色较深。
$codex=iconv("GB2312","UTF-8",substr($code,$i*2,2));
imagettftext($im,mt_rand(20,24),mt_rand(-
60,60),40*$i+20,mt_rand(30,35),$fontcolor,$fontface,$codex);
}
imagepng($im);
?>

2. Form file

<?php
#header("Content-Type: text/html; charset=utf-8");
session_start();
if(@$_POST[&#39;checkcode&#39;])
{
if($_POST["checkcode"] == $_SESSION["checkcode"])
    {
echo" 恭喜你!验证码输入正确!";
    }else{
echo" 不好意思验证码输入错误!<br>";
echo" 你刚才输入的验证码是:<br>";
echo  $_POST["checkcode"];
echo" <br>正确验证码是:<br>";
echo  $_SESSION["checkcode"];
    }
}
?>
<form action="" method="post">
<img src="cn.php"/></br>
<input type="text" name="checkcode"></br>
<input type="submit" value="submit"></br>
</form>

The first file cn.php contains very key Not a single line of code has been written into the program posted online.

That is

$_SESSION["checkcode"] = mb_convert_encoding($code,"utf-8","gbk");

Use the function mb_convert_encoding to convert Chinese, otherwise the variables sent by the session will be garbled.

The above is the detailed content of How to solve the php verification garbled problem. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn