php中文汉字验证码程序

 更新时间:2016年11月25日 15:25  点击:1767
本文章利用三个文件来简单的讲了一下关于php中怎么去应用中文验证码,中文因为是汉字可能出现乱码所以我们就定义了一个文件来专门处理,有需要的朋友可以参考下。
 代码如下 复制代码


<?php   
/*   
* 文件:chinesechar.php   
* 作用:汉字数据储存   
*/   
$ChineseChar = array("人","出","来","友","学","孝","仁","义","礼","廉","忠","国","中","易","白","者","火 ","土","金","木","雷","风","龙","虎","天","地",   "生","晕","菜","鸟","田","三","百","钱","福 ","爱","情","兽","虫","鱼","九","网","新","度","哎","唉","啊","哦","仪","老","少","日",   "月 ","星");   
?>   

<?php   
/*   
* 文件:check.php   
* 作用:验证   

*/   
session_start();   
$errorMSG = '';   
//验证用户输入是否和验证码一致   
if(!is_null($_POST['check']))   
{   
        if (strcasecmp($_SESSION['code'],$_POST['code'])==0)   
            $errorMSG = "<p style="font-size:12px;color:#009900">验证成功!</p>";   
        else   
            $errorMSG = "<p style="font-size:12px;color:#FF0000">验证失败!</p>";   
}   
?>   
<html>   
<head>   
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">   
</head>   
<body>   
<?php   
if($errorMSG){   
echo $errorMSG;   
}   
?>   
<form action=<?php echo $_SERVER['PHP_SELF']?> method=post>   
请输入验证码:<input type="text" name="code" style="width:   
80px"><img src="code.php">   
<br>   
<input type="submit" name="check" value="提交验证码">   
</form>   
</body>   
</html>   

<?php   
/*   
* 文件:code.php   
* 作用:验证码生成   
 
* 特注:由 没牙的草 指导 版权所有转载注明出处!有付出才会有收获! 
*/   
include_once("chinesechar.php");   
session_start();   
// 设置 content-type   
header("Content-type: image/png");   
// 创建图片   
$im = imagecreatetruecolor(120, 30);   

// 创建颜色   
$fontcolor = imagecolorallocate($im, 255, 255, 255);   
$bg = imagecolorallocate($im, 0, 0, 0);   

// 设置文字   
for($i=0;$i<4;$i++) $text .= $ChineseChar[(array_rand($ChineseChar))];   

$_SESSION['code'] = $text;   
// 设置字体   
$font = 'simkai.ttf';   

// 添加文字   
imagettftext($im, 18, 0, 11, 21, $fontcolor, $font, iconv("GB2312","UTF-8",$text));   

// 输出图片   
imagepng($im);   
imagedestroy($im);   
?>

如果想把上面的程序改成英文数字,只要在chinesechar.php 里面的数组中文改成数字或字母就KO了。

我们举例只讲linux的系统,但是防止方法在任何系统都是有效的,下面我们先来看看等操作


你可以这样使用

 代码如下 复制代码

http://www.xxx.com/index.php?page=../etc/passwd
http://www.xxx.com/index.php?page=../../../etc/passwd
http://www.xxx.com/index.php?page=..../../etc/passwd

获取更多数据:
etc/profile
etc/services
/etc/passwd
/etc/shadow
/etc/group
/etc/security/group
/etc/security/passwd
/etc/security/user
/etc/security/environ
/etc/security/limits
/usr/lib/security/mkuser.default


像上面代码,如果你是

?page=$_GET的话这样就完了,分析原因,因为我们分页只有数字,那么我们这样操作

 代码如下 复制代码

?page=intval($_GET);

这样就无法把字符提交了,我们利用了intval函数进行了过滤,那么对于提交字符怎么处理呢。

在处理字符时我们利用php自带函数的函数 addslashes和htmlspecialchars进行过滤,

 代码如下 复制代码

$body = htmlspecialchars(isset($_GET[$str])?$_GET[$str]:'');

这样就基本过滤了各种安全注入,当然如果你服务器有漏洞在php上是解决不了的。

一个比较实用的php图形验证码生成类,调用方法也很简单的,有需要的朋友可以参考一下。
 代码如下 复制代码

<?php

class ImageCode{
 private $width;//验证码图片宽度
 private $height;//验证码图片高度
 private $codeNum;//验证码字符个数
 private $checkCode;//验证码字符
 private $image;//验证码画布
 function __construct($width=60,$height=20,$codeNum=4){
  $this->width=$width;
  $this->height=$height;
  $this->codeNum=$codeNum;
  $this->checkCode=$this->createCheckCode();
 }
 
 function getcreateImage(){
  $this->getcreateImage();
  $this->outputText();
  $this->setDisturbColor();
  $this->outputImage();
 }
 function getCheckCode(){
  return $this->checkCode;
 }
 
 private function getCreateImage(){
  $this->image=imagecreatetruecolor($this->width,$this->height);
  $black=imagecolorallocate($this->image,255,255,255,0);
  $border=imagecolorallocate($this->image,255,255,255,255);
  imagefilledrectangle($this->image,0,0,$this->width-1,$this->height-1,$border);
 }

 private function createCheckCode(){
  for($i=0;$i<$this->codeNum;$i){
   $number=rand(0,2);
   switch($number){
    case 0:
     $rand_number=rand(48,57);//数字
     break;
    case 1:
     $rand_number=rand(65,90);//大写字母
     break;
    case 2:
     $rand_number=rand(97,122);
     break;
   }
   $asc=sprintf("%c",$rand_number);
   $asc_number=$asc_number.$asc;
  }
  return $asc_number;
 }

 private function setDisturbColor(){
  for($i=0;$i<=100;$i++){
   $color=imagecolorallocate($this->image,255,255,255);
   imagesetpixel($this->image,rand(1,$this->width-2),rand(1,$this->height-2),$color);
  }
 }

 private function outputImage(){
  if(imagetypes()&IMG_GIF){
   header("Content_type:image/gif");
   imagegif($this->image);
  }elseif(imagetypes()&IMG_JGP){
   header("Content_type:image/jpeg");
   imagejpeg($this->image,"",0.5);
  }else{
   die("PHP不支持图像创建");
  }
 }

 function __destruct(){
  imagedestroy($this->image);
 }
}

?>

一款国外网站下载的php生成验证码图片代码,这个比较实用我们还举例说明了,有需要的朋友按上面保存成php就可以用了哦。
 代码如下 复制代码

<?php
session_start();

if( isset($_POST['submit'])) {
   if( $_SESSION['security_code'] == $_POST['security_code'] && !empty($_SESSION['security_code'] ) ) {
  // Insert you code for processing the form here, e.g emailing the submission, entering it into a database.
  echo 'Thank you. Your message said "'.$_POST['message'].'"';
  unset($_SESSION['security_code']);
   } else {
  // Insert your code for showing an error message here
  echo 'Sorry, you have provided an invalid security code';
   }
} else {
?>

 <form action="form.php" method="post">
  <label for="name">Name: </label><input type="text" name="name" id="name" /><br />
  <label for="email">Email: </label><input type="text" name="email" id="email" /><br />
  <label for="message">Message: </label><textarea rows="5" cols="30" name="message" id="message"></textarea><br />
  <img src="CaptchaSecurityImages.php?width=100&height=40&characters=5" /><br />
  <label for="security_code">Security Code: </label><input id="security_code" name="security_code" type="text" /><br />
  <input type="submit" name="submit" value="Submit" />
 </form>

<?php
 }
?>

验证程序 CaptchaSecurityImages.php

 代码如下 复制代码

<?php
session_start();

/*
* File: CaptchaSecurityImages.php
* Author: Simon Jarvis
* Copyright: 2006 Simon Jarvis
* Date: 03/08/06
* Updated: 07/02/07
* Requirements: PHP 4/5 with GD and FreeType libraries
* Link: http://www.white-hat-web-design.co.uk/articles/php-captcha.php
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details:
* http://www.gnu.org/licenses/gpl.html
*
*/

class CaptchaSecurityImages {

 var $font = 'monofont.ttf';

 function generateCode($characters) {
  /* list all possible characters, similar looking characters and vowels have been removed */
  $possible = '23456789bcdfghjkmnpqrstvwxyz';
  $code = '';
  $i = 0;
  while ($i < $characters) {
   $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
   $i++;
  }
  return $code;
 }

 function CaptchaSecurityImages($width='120',$height='40',$characters='6') {
  $code = $this->generateCode($characters);
  /* font size will be 75% of the image height */
  $font_size = $height * 0.75;
  $image = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');
  /* set the colours */
  $background_color = imagecolorallocate($image, 255, 255, 255);
  $text_color = imagecolorallocate($image, 20, 40, 100);
  $noise_color = imagecolorallocate($image, 100, 120, 180);
  /* generate random dots in background */
  for( $i=0; $i<($width*$height)/3; $i++ ) {
   imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
  }
  /* generate random lines in background */
  for( $i=0; $i<($width*$height)/150; $i++ ) {
   imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
  }
  /* create textbox and add text */
  $textbox = imagettfbbox($font_size, 0, $this->font, $code) or die('Error in imagettfbbox function');
  $x = ($width - $textbox[4])/2;
  $y = ($height - $textbox[5])/2;
  imagettftext($image, $font_size, 0, $x, $y, $text_color, $this->font , $code) or die('Error in imagettftext function');
  /* output captcha image to browser */
  header('Content-Type: image/jpeg');
  imagejpeg($image);
  imagedestroy($image);
  $_SESSION['security_code'] = $code;
 }

}

$width = isset($_GET['width']) ? $_GET['width'] : '120';
$height = isset($_GET['height']) ? $_GET['height'] : '40';
$characters = isset($_GET['characters']) && $_GET['characters'] > 1 ? $_GET['characters'] : '6';

$captcha = new CaptchaSecurityImages($width,$height,$characters);

?>

很多php开发者都会使用这个md()加密函数,但其中的参考有些朋友未必知道了,今天我们来看看md5()函数的参数吧.

语法
md5(string,raw)

md5() 函数计算字符串的 MD5 散列


string 必需。规定要计算的字符串

charlist

可选。规定十六进制或二进制输出格式:
TRUE - 原始 16 字符二进制格式
FALSE - 默认。32 字符十六进制数
注释:该参数是 PHP 5.0 中添加的。
 

 代码如下 复制代码

<?
$str ="123456";
echo 'md5 16位'.md5($str,true);

//输出乱码

//如果你想得到php5中md5( "abc ",   true)这样的返回值,那么可以:
function   bin_md5($val)   {
    return   pack( "H32 ",md5($val));
}

 

echo '<br />md5 32位'.md5($str);

//10adc3949ba59abbe56e057f20f883e
?>

[!--infotagslink--]

相关文章

  • js URLdecode()与urlencode方法支持中文解码

    下面来介绍在js中来利用urlencode对中文编码与接受到数据后利用URLdecode()对编码进行解码,有需要学习的机友可参考参考。 代码如下 复制代码 ...2016-09-20
  • PHP 验证码不显示只有一个小红叉的解决方法

    最近想自学PHP ,做了个验证码,但不知道怎么搞的,总出现一个如下图的小红叉,但验证码就是显示不出来,原因如下 未修改之前,出现如下错误; (1)修改步骤如下,原因如下,原因是apache权限没开, (2)点击打开php.int., 搜索extension=ph...2013-10-04
  • 关于Mysql中文乱码问题该如何解决(乱码问题完美解决方案)

    最近两天做项目总是被乱码问题困扰着,这不刚把mysql中文乱码问题解决了,下面小编把我的解决方案分享给大家,供大家参考,也方便以后自己查阅。首先:用show variables like “%colla%”;show varables like “%char%”;这两条...2015-11-24
  • jQuery Real Person验证码插件防止表单自动提交

    本文介绍的jQuery插件有点特殊,防自动提交表单的验证工具,就是我们经常用到的验证码工具,先给大家看看效果。效果图如下: 使用说明 需要使用jQuery库文件和Real Person库文件 同时需要自定义验证码显示的CSS样式 使用实例...2015-11-08
  • C#读取中文文件出现乱码的解决方法

    这篇文章主要介绍了C#读取中文文件出现乱码的解决方法,涉及C#中文编码的操作技巧,非常具有实用价值,需要的朋友可以参考下...2020-06-25
  • JS实现随机生成验证码

    这篇文章主要为大家详细介绍了JS实现随机生成验证码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-06
  • Windows服务器MySQL中文乱码的解决方法

    我们自己鼓捣mysql时,总免不了会遇到这个问题:插入中文字符出现乱码,虽然这是运维先给配好的环境,但是在自己机子上玩的时候咧,总得知道个一二吧,不然以后如何优雅的吹牛B。...2015-03-15
  • Mysql在debian系统中不能插入中文的终极解决方案

    在debian环境下,彻底解决mysql无法插入和显示中文的问题Linux下Mysql插入中文显示乱码解决方案mysql -uroot -p 回车输入密码进入mysql查看状态如下:默认的是客户端和服务器都用了latin1,所以会乱码。解决方案:mysql>use...2013-10-04
  • linux mint 下mysql中文支持问题

    一.mysql默认不支持中文,它的server和db默认是latin1编码.所以我们要将其改变为utf-8编码,因为utf-8包含了地球上大部分语言的二进制编码 1.关闭mysql服务 sudo /etc/init.d/mysql stop 2.修改mysql配置文件 mysql配...2015-10-21
  • php怎么用拼音 简单的php中文转拼音的实现代码

    小编分享了一段简单的php中文转拼音的实现代码,代码简单易懂,适合初学php的同学参考学习。 代码如下 复制代码 <?phpfunction Pinyin($_String...2017-07-06
  • Jquery插件实现点击获取验证码后60秒内禁止重新获取

    通过jquery.cookie.js插件可以快速实现“点击获取验证码后60秒内禁止重新获取(防刷新)”的功能效果图:先到官网(http://plugins.jquery.com/cookie/)下载cookie插件,放到相应文件夹,代码如下:复制代码 代码如下: <!DOCTYPE ht...2015-03-15
  • Java连接数据库oracle中文乱码解决方案

    这篇文章主要介绍了Java连接数据库oracle中文乱码解决方案,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-05-16
  • php实现点击可刷新验证码

    验证码类文件 CreateImg.class.php <&#63;php class ValidationCode { private $width,$height,$codenum; public $checkcode; //产生的验证码 private $checkimage; //验证码图片 private $disturbColor = ''; /...2015-11-08
  • 基于JavaScript实现验证码功能

    这篇文章主要介绍了基于JavaScript实现验证码功能的相关资料...2017-04-03
  • Bootstrap中文本框的宽度变窄并且加入一副验证码图片的实现方法

    这篇文章主要介绍了Bootstrap中文本框的宽度变窄并且加入一副验证码图片的实现方法的相关资料,非常不错,具有参考借鉴价值,需要的朋友可以参考下...2016-06-24
  • 单击按钮发送验证码,出现倒计时的简单实例

    下面小编就为大家带来一篇单击按钮发送验证码,出现倒计时的简单实例。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧 代码...2017-07-06
  • PHP验证码生成与验证例子

    验证码是一个现在WEB2.0中常见的一个功能了,像注册、登录又或者是留言页面,都需要注册码来验证当前操作者的合法性,我们会看到有些网站没有验证码,但那是更高级的验证了,...2016-11-25
  • 基于Pytorch版yolov5的滑块验证码破解思路详解

    这篇文章主要介绍了基于Pytorch版yolov5的滑块验证码破解思路详解,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-02-25
  • jQuery实现发送验证码控制按钮禁用功能

    最近接到新需求,需要实现一个点击发送验证码之后,按钮禁用,在5秒之后取消禁用,看似需求很简单,实现起来还真的好好动动脑筋,下面小编把jquery控制按钮禁用核心代码分享给大家,需要的朋友参考下吧...2021-07-24
  • FlashFXP连接站点中文显示乱码解决办法

    FlashFXP是一款常用的服务器客户连接软件了,我们可以通过FlashFXP来上传或下载文件,但有一些朋友使用FlashFXP时碰到中文目录或文件名乱码问题,那么要如何来解决呢?具体就...2016-10-10