php 中文字符验证码类函数
<?php
002 class Captcha
003 {
004 private $_about = array(
005 'imageLine'=>8, // 线条干扰,为0正常
006 'imagePixel'=>300, // 像素干扰,为0正常
007 'location'=>true, // 字体随机位置,false正常
008 'bgColor'=>'#ffffff', // 背景
009 'textColor'=>'', // 文本
010 'borColor'=>'#e1e1e1', // 边框
011 'lineColor'=>'#dedede', // 线条
012 'pixelColor'=>'#646464' // 像素
013 );
014
015 /**
016 * 构造<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=%BA%AF%CA%FD">函数</SPAN>
017 */
018 public function __construct(){}
019
020 /**
021 * setCookie
022 *
023 * @param string $data 写入<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=%CA%FD%BE%DD">数据</SPAN>
024 */
025 public function setCookie($data)
026 {
027 if (is_array($data)) {
028 $string = '';
029 foreach ($data as $v) $string .= $v;
030 $data = $string;
031 }
032 setcookie('captcha',$data,time()+60,'/');
033 }
034 /**
035 * setCaptcha
036 *
037 * 设置输出<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=%D1%E9%D6%A4">验证</SPAN>码
038 *
039 * @param integer $width 宽
040 * @param integer $height 高
041 * @param integer $fontSize 字体大小
042 * @param string $font 字体<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=%CE%C4%BC%FE">文件</SPAN>位置
043 * @param integer $length 验证<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=%D7%D6%B7%FB">字符</SPAN>长度
044 * @param integer $type 验证字符类型
045 * 1:只数字,2:只小写字母,3:只大写字母,
046 * 4:小写字母数字混合,5:大写字母数字混合,6:大小写字母数字混合,7:汉字
047 * @param array $about 验证码配置
048 */
049 public function setCaptcha($width=80,$height=30,$fontSize=13,$font='code.ttf',$length=4,$type=3,$about=array())
050 {
051 is_array($about) || exit('参数出错!');
052 foreach ($about as $key=>$value) {
053 if (array_key_exists($key,$this->_about)) {
054 $this->_about[$key] = $value;
055 }
056 }
057
058 extract($this->_about,EXTR_OVERWRITE);
059 $im = imagecreatetruecolor($width,$height);
060 // 背景颜色
061 $bgC_Arr = $this->getColor($bgColor);
062 $imgBgColor = imagecolorallocate($im,$bgC_Arr[0],$bgC_Arr[1],$bgC_Arr[2]);
063 // 边框颜色
064 $borC_Arr = $this->getColor($borColor);
065 $imgBorderColor = imagecolorallocate($im,$borC_Arr[0],$borC_Arr[1],$borC_Arr[2]);
066 // 填充
067 imagefill($im,0,0,$imgBgColor);
068 imagerectangle($im,0,0,$width-1,$height-1,$imgBorderColor);
069
070 // 画线
071 if ($imageLine) {
072 // 线条颜色
073 $lineC_Arr = $this->getColor($lineColor);
074 $imgLineColor = imagecolorallocate($im,$lineC_Arr[0],$lineC_Arr[1],$lineC_Arr[2]);
075 for ($i=0;$i<$imageLine;$i++) {
076 imageline($im,0,mt_rand(0,$height),$width,mt_rand(0,$height),$imgLineColor);
077 }
078 }
079 // 像素干扰
080 if ($imagePixel) {
081 // 像素颜色
082 $pixelC_Arr = $this->getColor($pixelColor);
083 $imgPixelColor = imagecolorallocate($im,$pixelC_Arr[0],$pixelC_Arr[1],$pixelC_Arr[2]);
084 for ($i=0;$i<$imagePixel;$i++) {
085 imagesetpixel($im,mt_rand(2,$width-2),mt_rand(2,$height-2),$imgPixelColor);
086 }
087 }
088
089 // 获取字串
090 if (!$text = $this->getText($type,$length)) exit('验证码没有查找到指定字符类型!');
091 // 检查字体文件
092 is_file($font) || exit('验证码未查找到指定字体文件!');
093 $y = ceil($height / 2)+ceil($fontSize/2)*0.9;
094 $pitch = ceil($width / $length);
095 $text = ctype_alnum($text)?$text:str_split($text,3);
096 $pad = 0;
097 for ($i=0;$i<$length;$i++) {
098 if (!empty($textColor)) {
099 // 文本颜色
100 $txtC_Arr = $this->getColor($textColor);
101 $imgTextColor = imagecolorallocate($im,$txtC_Arr[0],$txtC_Arr[1],$txtC_Arr[2]);
102 } else {
103 $imgTextColor = imagecolorallocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
104 }
105 if (false === $location) {
106 $fontSizePad = ctype_alnum($text[$i]) ? $fontSize*1.4 : $fontSize*1.7;
107 $x = $pad + $fontSize + (ceil($pitch/2) - $fontSizePad);
108 imagettftext($im,$fontSize,0,$x,$y,$imgTextColor,$font,$text[$i]);
109 $pad += $pitch;
110 } else {
111 $d = $pad += $pitch;
112 $d += $fontSize*0.5;
113 imagettftext($im,$fontSize,mt_rand(-20,20),mt_rand($d-=$pitch,$pad-$fontSize*1.4),$y,$imgTextColor,$font,$text[$i]);
114 }
115 }
116 // 写入<SPAN class=t_tag onclick=tagshow(event) href="tag.php?name=session">session</SPAN>
117 $this->setCookie($text);
118 $this->getCaptcha($im);
119 }
120
121 /**
122 * getText
123 *
124 * 获取字符串
125 *
126 * @param integer $type 字串类型
127 * @param integer $length 长度
128 * @return string $result 字串结果
129 */
130 public function getText($type,$length)
131 {
132 $result = '';
133 $range = array();
134 switch ($type) {
135 case 1 :
136 for ($i=0;$i<$length;$i++) $result .= mt_rand(0,9);
137 break;
138 case 2 :
139 $range = range('a','z');
140 break;
141 case 3 :
142 $range = range('A','Z');
143 break;
144 case 4 :
145 $range = array_merge(range(0,9),range('a','z'));
146 break;
147 case 5 :
148 $range = array_merge(range(0,9),range('A','Z'));
149 break;
150 case 6 :
151 $range = array_merge(range(0,9),range('a','z'),range('A','Z'));
152 break;
153 case 7 :
154 // for ($i=0;$i<$length;$i++) $result .= iconv('gb2312','utf-8',chr(rand(0xB0,0xF7)).chr(rand(0xA1,0xFE)));
155 $range = array(
156 '佟','爱','菲','可','天','一','新','他','策','韵','年','明','礼','杰','雅','诗','忍','子',
157 '好','快','乐','半','美','拜','亮','臣','香','中','国','开','心','阿','笑','图','成','虎'
158 );
159 break;
160 default : return false;
161 }
162 if (!empty($range)) {
163 shuffle($range);
164 $result = mb_substr(implode('',$range),0,$length,'utf-8');
165 }
166 return $result;
167 }
168
169 /**
170 * getColor
171 *
172 * 十六进制颜色转为十进制
173 */
174 public function getColor($string)
175 {
176 $string = str_replace('#','',$string);
177 if (!ctype_alnum($string)) {
178 return false;
179 }
180 $rgb_Arr = array();
181 if (strlen($string) == 3) {
182 for ($i=0;$i<3;$i++) {
183 $rgb_Arr[$i] = hexdec(str_repeat($string{$i},'2'));
184 }
185 } else if (strlen($string) == 6) {
186 $colorSp = str_split($string,2);
187 foreach ($colorSp as $v) {
188 $rgb_Arr[] = hexdec($v);
189 }
190 } else {
191 return false;
192 }
193 return $rgb_Arr;
194 }
195
196 public function getCaptcha($im)
197 {
198 header('Content-type: image/gif');
199 imagegif($im);
200 imagedestroy($im);
201 }
202 }
203
204 $about = array(
205 'imageLine'=>0, // 线条干扰,为0正常
206 'imagePixel'=>300, // 像素干扰,为0正常
207 'location'=>false, // 字体随机位置,false正常
208 'bgColor'=>'#ffffff', // 背景颜色
209 'textColor'=>'', // 文本颜色,默认随机颜色
210 'borColor'=>'#e1e1e1', // 边框颜色
211 'lineColor'=>'#ff0000', // 线条颜色
212 'pixelColor'=>'#a13e3e' // 像素颜色
213 );
214 error_reporting(0);
215 $a = new Captcha();
216 $a->setCaptcha(100,30,13,'font.ttf',4,1,$about);
(1) 打开php教程的安全模式
php的安全模式是个非常重要的内嵌的安全机制,能够控制一些php中的函数,比如system(),
同时把很多文件操作函数进行了权限控制,也不允许对某些关键文件的文件,比如/etc/passwd,
但是默认的php.ini是没有打开安全模式的,我们把它打开:
safe_mode = on
(2) 用户组安全
当safe_mode打开时,safe_mode_gid被关闭,那么php脚本能够对文件进行访问,而且相同
组的用户也能够对文件进行访问。
建议设置为:
safe_mode_gid = off
如果不进行设置,可能我们无法对我们服务器网站目录下的文件进行操作了,比如我们需要
对文件进行操作的时候。
(3) 安全模式下执行程序主目录
如果安全模式打开了,但是却是要执行某些程序的时候,可以指定要执行程序的主目录:
safe_mode_exec_dir = D:/usr/bin
一般情况下是不需要执行什么程序的,所以推荐不要执行系统程序目录,可以指向一个目录,
然后把需要执行的程序拷贝过去,比如:
safe_mode_exec_dir = D:/tmp/cmd
但是,我更推荐不要执行任何程序,那么就可以指向我们网页目录:
safe_mode_exec_dir = D:/usr/www
(4) 安全模式下包含文件
如果要在安全模式下包含某些公共文件,那么就修改一下选项:
safe_mode_include_dir = D:/usr/www/include/
其实一般php脚本中包含文件都是在程序自己已经写好了,这个可以根据具体需要设置。
(5) 控制php脚本能访问的目录
使用open_basedir选项能够控制PHP脚本只能访问指定的目录,这样能够避免PHP脚本访问
不应该访问的文件,一定程度上限制了phps教程hell的危害,我们一般可以设置为只能访问网站目录:
open_basedir = D:/usr/www
(6) 关闭危险函数
如果打开了安全模式,那么函数禁止是可以不需要的,但是我们为了安全还是考虑进去。比如,
我们觉得不希望执行包括system()等在那的能够执行命令的php函数,或者能够查看php信息的
phpinfo()等函数,那么我们就可以禁止它们:
disable_functions = system,passthru,exec,shell_exec,popen,phpinfo
如果你要禁止任何文件和目录的操作,那么可以关闭很多文件操作
disable_functions = chdir,chroot,dir,getcwd,opendir,readdir,scandir,fopen,unlink,delete,copy,mkdir, rmdir,rename,file,file_get_contents,fputs,fwrite,chgrp,chmod,chown
以上只是列了部分不叫常用的文件处理函数,你也可以把上面执行命令函数和这个函数结合,
就能够抵制大部分的phpshell了。
(7) 关闭PHP版本信息在http头中的泄漏
我们为了防止黑客获取服务器中php版本的信息,可以关闭该信息斜路在http头中:
expose_php = Off
比如黑客在 te.net教程 www.111cn.net 80 的时候,那么将无法看到PHP的信息。
(8) 关闭注册全局变量
在PHP中提交的变量,包括使用POST或者GET提交的变量,都将自动注册为全局变量,能够直接访问,
这是对服务器非常不安全的,所以我们不能让它注册为全局变量,就把注册全局变量选项关闭:
register_globals = Off
当然,如果这样设置了,那么获取对应变量的时候就要采用合理方式,比如获取GET提交的变量var,
那么就要用$_GET['var']来进行获取,这个php程序员要注意。
(9) 打开magic_quotes_gpc来防止SQL注入
SQL注入是非常危险的问题,小则网站后台被入侵,重则整个服务器沦陷,
所以一定要小心。php.ini中有一个设置:
magic_quotes_gpc = Off
这个默认是关闭的,如果它打开后将自动把用户提交对sql的查询进行转换,
比如把 ' 转为 '等,这对防止sql注射有重大作用。所以我们推荐设置为:
magic_quotes_gpc = On
(10) 错误信息控制
一般php在没有连接到数据库教程或者其他情况下会有提示错误,一般错误信息中会包含php脚本当
前的路径信息或者查询的SQL语句等信息,这类信息提供给黑客后,是不安全的,所以一般服务器建议禁止错误提示:
display_errors = Off
如果你却是是要显示错误信息,一定要设置显示错误的级别,比如只显示警告以上的信息:
error_reporting = E_WARNING & E_ERROR
当然,我还是建议关闭错误提示。
(11) 错误日志
建议在关闭display_errors后能够把错误信息记录下来,便于查找服务器运行的原因:
log_errors = On
同时也要设置错误日志存放的目录,建议根apache的日志存在一起:
error_log = D:/usr/local/apache2/logs/php_error.log
注意:给文件必须允许apache用户的和组具有写的权限。
MYSQL的降权运行
新建立一个用户比如mysql教程start
net user mysqlstart ****microsoft /add
net localgroup users mysqlstart /del
不属于任何组
如果MYSQL装在d:mysql ,那么,给 mysqlstart 完全控制 的权限
然后在系统服务中设置,MYSQL的服务属性,在登录属性当中,选择此用户 mysqlstart 然后输入密码,确定。
重新启动 MYSQL服务,然后MYSQL就运行在低权限下了。
如果是在windos平台下搭建的apache我们还需要注意一点,apache默认运行是system权限,
这很恐怖,这让人感觉很不爽.那我们就给apache降降权限吧。
net user apache ****microsoft /add
net localgroup users apache /del
ok.我们建立了一个不属于任何组的用户apche。
我们打开计算机管理器,选服务,点apache服务的属性,我们选择log on,选择this account,我们填入上面所建立的账户和密码,
重启apache服务,ok,apache运行在低权限下了。
实际上我们还可以通过设置各个文件夹的权限,来让apache用户只能执行我们想让它能干的事情,给每一个目录建立一个单独能读写的用户。
在该脚本的所有需要,我们会计算生成图表变量第一的第二部分。然后,我们准备的
主要形象,实际输出数据。每个类型的结果是一个从数据库教程中挑选一个,得票率计算
,然后酒吧和解释性说明的输出。
在此脚本中,我们使用的是TrueType字体获得反锯齿文本。功能与TrueType字体的工
作有自己的特点。首先,你需要的字体点所在。为此,系统变量GDFONTPATH设置。然
后,我们需要设置字体的名称。 TrueType字体渲染使用ImageTTFText()。
我们已经讨论了上面的绘图中使用的所有功能,代码有意见,是很清楚的,所以你不
应该有任何麻烦。
//Tell gd2, where your fonts reside
putenv('GDFONTPATH=C:WINDOWSFonts');
$font = 'arial';
//Set starting point for drawing
$y = 50;
//Specify constant values
$width = 700; //Image width in pixels
$bar_height = 20; //Bars height
$height = $num_poller * $bar_height * 1.5 + 70; //Calculating image height
$bar_unit = ($width - 400) / 100; //Distance on the bar chart standing for
1 unit
//Create the image resource
$image = ImageCreate($width, $height);
//We are making four colors, white, black, blue and red
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$red = ImageColorAllocate($image, 255, 0, 0);
$blue = imagecolorallocate($image,0,0,255);
//Create image background
ImageFill($image,$width,$height,$white);
//Draw background shape
ImageRectangle($image, 0, 0, $width-1, $height-1, $black);
//Output header
ImageTTFText($image, 16, 0, $width/3 + 50, $y - 20, $black, $font, 'Poll
Results');
while ($row = mysql教程_fetch_object($result)) {
if ($total_votes > 0)
$percent = intval(round(($row->num_votes/$total_votes)*100));
else
$percent = 0;
//Output header for a particular value
ImageTTFText($image,12,0,10, $y+($bar_height/2), $black, $font, $row-
>book_type);
//Output percentage for a particular value
ImageTTFText($image, 12, 0, 170, $y +
($bar_height/2),$red,$font,$percent.'%');
$bar_length = $percent * $bar_unit;
//Draw a shape that corresponds to 100%
ImageRectangle($image, $bar_length+221, $y-2, (220+(100*$bar_unit)),
$y+$bar_height, $black);
//Output a bar for a particular value
ImageFilledRectangle($image,220,$y-2,220+$bar_length, $y+$bar_height,
$blue);
//Output the number of votes
ImageTTFText($image, 12, 0, 250+100*$bar_unit, $y+($bar_height/2), $black,
$font, $row->num_votes.' votes cast.');
//Going down to the next bar
$y = $y + ($bar_height * 1.5);
}
//Tell the browser what kind of file is come in
header("Content-Type: image/jpeg");
//Output the newly created image in jpeg format
ImageJpeg($image);
//Free up resources
ImageDestroy($image);
?>
起初,有一个在php.ini 看看gd库是不是开启的,
下一步,我们会考虑每一个细节的图像生成步骤。
<?php
//Send a generated image to the browser
create_image();
exit();
function create_image()
{
//Let's generate a totally random string using md5
$md5 = md5(rand(0,999));
//We don't need a 32 character long string so we trim it down to 5
$pass = substr($md5, 10, 5);
//Set the image width and height
$width = 100;
$height = 20;
//Create the image resource
$image = ImageCreate($width, $height);
//We are making three colors, white, black and gray
$white = ImageColorAllocate($image, 255, 255, 255);
$black = ImageColorAllocate($image, 0, 0, 0);
$grey = ImageColorAllocate($image, 204, 204, 204);
//Make the background black
ImageFill($image, 0, 0, $black);
//Add randomly generated string in white to the image
ImageString($image, 3, 30, 3, $pass, $white);
//Throw in some lines to make it a little bit harder for any bots to
break
ImageRectangle($image,0,0,$width-1,$height-1,$grey);
imageline($image, 0, $height/2, $width, $height/2, $grey);
imageline($image, $width/2, 0, $width/2, $height, $grey);
//Tell the browser what kind of file is come in
header("Content-Type: image/jpeg");
//Output the newly created image in jpeg format
ImageJpeg($image);
//Free up resources
ImageDestroy($image);
}
?>
验证码调用方法
<img height="120" alt="Dynamically generated image"
src="generate_image.php" width="200">
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>php 文件上传只允许上传图像文件</title>
</head>
<body>
<form id="form1" name="form1" enctype="multipart/form-data" method="post" action="">
<label>
<input type="file" name="file" />
</label>
<label>
<input type="submit" name="Submit" value="提交" />
</label>
</form>
</body>
</html>
<?php
if($_FILES){
foreach( $_FILES as $key => $_value )
{
$_FILES[$key]['type'] =$_value['type'];
}
if(substr($_FILES[$key]['type'],0,6) !='image/')
{
exit;
}
}
echo '<hr/>';
print_r($_FILES);
echo '<hr/>';
echo var_export($_FILES);
?>
首先判断$_FILES['inputname']['type'],对于能够识别的类型更正文件后缀,然后再判断后缀。
因为$_FILES['inputname']['type']只能识别少数类型,但是它是根据文件内容来识别的,特别是对于许多把BMP图形保存为JPG文件的相机,一下就现原型了,而你的程序可能针对BMP有转换为JPG的处理。
相关文章
js URLdecode()与urlencode方法支持中文解码
下面来介绍在js中来利用urlencode对中文编码与接受到数据后利用URLdecode()对编码进行解码,有需要学习的机友可参考参考。 代码如下 复制代码 ...2016-09-20- eval函数在php中是一个函数并不是系统组件函数,我们在php.ini中的disable_functions是无法禁止它的,因这他不是一个php_function哦。 eval()针对php安全来说具有很...2016-11-25
- 在php中eval是一个函数并且不能直接禁用了,但eval函数又相当的危险了经常会出现一些问题了,今天我们就一起来看看eval函数对数组的操作 例子, <?php $data="array...2016-11-25
Python astype(np.float)函数使用方法解析
这篇文章主要介绍了Python astype(np.float)函数使用方法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-06-08- 这篇文章主要介绍了Python中的imread()函数用法说明,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2021-03-16
- 本文主要介绍了C# 中取绝对值的函数。具有很好的参考价值。下面跟着小编一起来看下吧...2020-06-25
- 下面小编就为大家带来一篇C#学习笔记- 随机函数Random()的用法详解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...2020-06-25
- CREATE FUNCTION ChangeBigSmall (@ChangeMoney money) RETURNS VarChar(100) AS BEGIN Declare @String1 char(20) Declare @String2 char...2016-11-25
关于Mysql中文乱码问题该如何解决(乱码问题完美解决方案)
最近两天做项目总是被乱码问题困扰着,这不刚把mysql中文乱码问题解决了,下面小编把我的解决方案分享给大家,供大家参考,也方便以后自己查阅。首先:用show variables like “%colla%”;show varables like “%char%”;这两条...2015-11-24- 最近想自学PHP ,做了个验证码,但不知道怎么搞的,总出现一个如下图的小红叉,但验证码就是显示不出来,原因如下 未修改之前,出现如下错误; (1)修改步骤如下,原因如下,原因是apache权限没开, (2)点击打开php.int., 搜索extension=ph...2013-10-04
- 这篇文章主要介绍了C++中Sort函数详细解析,sort函数是algorithm库下的一个函数,sort函数是不稳定的,即大小相同的元素在排序后相对顺序可能发生改变...2022-08-18
Android开发中findViewById()函数用法与简化
findViewById方法在android开发中是获取页面控件的值了,有没有发现我们一个页面控件多了会反复研究写findViewById呢,下面我们一起来看它的简化方法。 Android中Fin...2016-09-20jQuery Real Person验证码插件防止表单自动提交
本文介绍的jQuery插件有点特殊,防自动提交表单的验证工具,就是我们经常用到的验证码工具,先给大家看看效果。效果图如下: 使用说明 需要使用jQuery库文件和Real Person库文件 同时需要自定义验证码显示的CSS样式 使用实例...2015-11-08- strstr() 函数搜索一个字符串在另一个字符串中的第一次出现。该函数返回字符串的其余部分(从匹配点)。如果未找到所搜索的字符串,则返回 false。语法:strstr(string,search)参数string,必需。规定被搜索的字符串。 参数sea...2013-10-04
PHP函数分享之curl方式取得数据、模拟登陆、POST数据
废话不多说直接上代码复制代码 代码如下:/********************** curl 系列 ***********************///直接通过curl方式取得数据(包含POST、HEADER等)/* * $url: 如果非数组,则为http;如是数组,则为https * $header:...2014-06-07- Foreach 函数(PHP4/PHP5)foreach 语法结构提供了遍历数组的简单方式。foreach 仅能够应用于数组和对象,如果尝试应用于其他数据类型的变量,或者未初始化的变量将发出错误信息。...2013-09-28
- 这篇文章主要介绍了C#读取中文文件出现乱码的解决方法,涉及C#中文编码的操作技巧,非常具有实用价值,需要的朋友可以参考下...2020-06-25
- 这篇文章主要为大家详细介绍了JS实现随机生成验证码,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-06
- free函数是释放之前某一次malloc函数申请的空间,而且只是释放空间,并不改变指针的值。下面我们就来详细探讨下...2020-04-25
- 我们自己鼓捣mysql时,总免不了会遇到这个问题:插入中文字符出现乱码,虽然这是运维先给配好的环境,但是在自己机子上玩的时候咧,总得知道个一二吧,不然以后如何优雅的吹牛B。...2015-03-15