php实现的简单中文验证码功能示例

yipeiwu_com6年前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程序设计有所帮助。

相关文章

PHP函数spl_autoload_register()用法和__autoload()介绍

__autoload()的用法就不再说了,以前已经在我的WEB开发笔记中说过。PHP __autoload函数(自动载入类文件)的使用方法, 原文地址:https://www.jb51....

PHP5中的时间相差8小时的解决办法

方法1:        找到php.ini中的“;date.timezone =”这行,将“;”去掉,改成...

PHP学习笔记 用户注册模块用户类以及验证码类

所以,把第一章,可重用类的代码贴出来,便于以后查阅以及供给有需要的朋友。 :User类,包括读取和设置数据库,以及保存更改交互 复制代码 代码如下: <?php class Use...

php实现的单一入口应用程序实例分析

本文较为详细的分析了php单一入口应用程序。分享给大家供大家参考。具体如下: 什么是单一入口应用程序? 在解释什么是单一入口应用程序之前,我们先来看看传统的 web 应用程序。 news...

PHP定时执行计划任务的多种方法小结

PHP定时执行计划任务的多种方法小结

PHP定时执行的三种方式实现 1、windows 的计划任务 2、linux的脚本程序 3、让web浏览器定时刷新 具体实现 windows计划任务 PHP很少在win服务器上跑,具体实...