这里有新鲜出炉的PHP教程,程序狗速度看过来!
PHP(外文名: Hypertext Preprocessor,中文名:“超文本预处理器”)是一种通用开源脚本语言。语法吸收了C语言、Java和Perl的特点,入门门槛较低,易于学习,使用广泛,主要适用于web开发领域。PHP的文件后缀名为php。
这篇文章主要介绍了php实现的简单中文验证码功能,结合实例形式分析了php中文验证码功能的实现步骤与操作方法,包括图形创建、编码操作、session操作等相关技巧,需要的朋友可以参考下
本文实例讲述了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程序设计有所帮助。
来源: http://www.phperz.com/article/17/0901/340751.html