验证码常用于登陆页面、留言页面、注册页面,验证码的原理很简单:利用GD库创建一个图片,图片当然要加上必要的干扰码,然后在服务器端存入SESSION,等用户提交的时候判断session是否相同。
代码如下 |
复制代码 |
<?php
/*
* Captcha Class base on PHP GD Lib
* @author Design
* @version 1.0
* @copyright js8.in 2010
* @demo
* include('captchaClass.php');
* $captchaDemo=new Captcha();
* $captchaDemo->createImage();
*/
class Captcha{
//@定义验证码图片高度
private $height;
//@定义验证码图片宽度
private $width;
//@定义验证码字符个数
private $textNum;
//@定义验证码字符内容
private $textContent;
//@定义字符颜色
private $fontColor;
//@定义随机出的文字颜色
private $randFontColor;
//@定义字体大小
private $fontSize;
//@定义字体
private $fontFamily;
//@定义背景颜色
private $bgColor;
//@定义随机出的背景颜色
private $randBgColor;
//@定义字符语言
private $textLang;
//@定义干扰点数量
private $noisePoint;
//@定义干扰线数量
private $noiseLine;
//@定义是否扭曲
private $distortion;
//@定义扭曲图片源
private $distortionImage;
//@定义是否有边框
private $showBorder;
//@定义验证码图片源
private $image;
//@Constructor 构造函数
public function Captcha(){
$this->textNum=4;
$this->fontSize=16;
$this->fontFamily='c:\windows\fontsSIMYOU.ttf';//设置中文字体,可以改成linux的目录
$this->textLang='en';
$this->noisePoint=30;
$this->noiseLine=3;
$this->distortion=false;
$this->showBorder=false;
}
//@设置图片宽度
public function setWidth($w){
$this->width=$w;
}
//@设置图片高度
public function setHeight($h){
$this->height=$h;
}
//@设置字符个数
public function setTextNumber($textN){
$this->textNum=$textN;
}
//@设置字符颜色
public function setFontColor($fc){
$this->fontColor=sscanf($fc,'#%2x%2x%2x');
}
//@设置字号
public function setFontSize($n){
$this->fontSize=$n;
}
//@设置字体
public function setFontFamily($ffUrl){
$this->fontFamily=$ffUrl;
}
//@设置字符语言
public function setTextLang($lang){
$this->textLang=$lang;
}
//@设置图片背景
public function setBgColor($bc){
$this->bgColor=sscanf($bc,'#%2x%2x%2x');
}
//@设置干扰点数量
public function setNoisePoint($n){
$this->noisePoint=$n;
}
//@设置干扰线数量
public function setNoiseLine($n){
$this->noiseLine=$n;
}
//@设置是否扭曲
public function setDistortion($b){
$this->distortion=$b;
}
//@设置是否显示边框
public function setShowBorder($border){
$this->showBorder=$border;
}
//@初始化验证码图片
public function initImage(){
if(empty($this->width)){$this->width=floor($this->fontSize*1.3)*$this->textNum+10;}
if(empty($this->height)){$this->height=$this->fontSize*2;}
$this->image=imagecreatetruecolor($this->width,$this->height);
if(empty($this->bgColor)){
$this->randBgColor=imagecolorallocate($this->image,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
}else{
$this->randBgColor=imagecolorallocate($this->image,$this->bgColor[0],$this->bgColor[1],$this->bgColor[2]);
}
imagefill($this->image,0,0,$this->randBgColor);
}
//@产生随机字符
public function randText($type){
$string='';
switch($type){
case 'en':
$str='ABCDEFGHJKLMNPQRSTUVWXY3456789';
for($i=0;$i<$this->textNum;$i++){
$string=$string.','.$str[mt_rand(0,29)];
}
break;
case 'cn':
for($i=0;$i<$this->textNum;$i++) {
$string=$string.','.chr(rand(0xB0,0xCC)).chr(rand(0xA1,0xBB));
}
$string=iconv('GB2312','UTF-8',$string); //转换编码到utf8
break;
}
return substr($string,1);
}
//@输出文字到验证码
public function createText(){
$textArray=explode(',',$this->randText($this->textLang));
$this->textContent=join('',$textArray);
if(empty($this->fontColor)){
$this->randFontColor=imagecolorallocate($this->image,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
}else{
$this->randFontColor=imagecolorallocate($this->image,$this->fontColor[0],$this->fontColor[1],$this->fontColor[2]);
}
for($i=0;$i<$this->textNum;$i++){
$angle=mt_rand(-1,1)*mt_rand(1,20);
imagettftext($this->image,$this->fontSize,$angle,5+$i*floor($this->fontSize*1.3),floor($this->height*0.75),$this->randFontColor,$this->fontFamily,$textArray[$i]);
}
}
//@生成干扰点
public function createNoisePoint(){
for($i=0;$i<$this->noisePoint;$i++){
$pointColor=imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
imagesetpixel($this->image,mt_rand(0,$this->width),mt_rand(0,$this->height),$pointColor);
}
}
//@产生干扰线
public function createNoiseLine(){
for($i=0;$i<$this->noiseLine;$i++) {
$lineColor=imagecolorallocate($this->image,mt_rand(0,255),mt_rand(0,255),20);
imageline($this->image,0,mt_rand(0,$this->width),$this->width,mt_rand(0,$this->height),$lineColor);
}
}
//@扭曲文字
public function distortionText(){
$this->distortionImage=imagecreatetruecolor($this->width,$this->height);
imagefill($this->distortionImage,0,0,$this->randBgColor);
for($x=0;$x<$this->width;$x++){
for($y=0;$y<$this->height;$y++){
$rgbColor=imagecolorat($this->image,$x,$y);
imagesetpixel($this->distortionImage,(int)($x+sin($y/$this->height*2*M_PI-M_PI*0.5)*3),$y,$rgbColor);
}
}
$this->image=$this->distortionImage;
}
//@生成验证码图片
public function createImage(){
$this->initImage(); //创建基本图片
$this->createText(); //输出验证码字符
if($this->distortion){$this->distortionText();} //扭曲文字
$this->createNoisePoint(); //产生干扰点
$this->createNoiseLine(); //产生干扰线
if($this->showBorder){imagerectangle($this->image,0,0,$this->width-1,$this->height-1,$this->randFontColor);} //添加边框
imagepng($this->image);
imagedestroy($this->image);
if($this->distortion){imagedestroy($this->$distortionImage);}
return $this->textContent;
}
}
?>使用方法:
<?php
//session_start();
header("Content-type:image/png");
include('captcha5_class.php');
$captcha5=new Captcha();
//@设置验证码宽度
//$captcha5->setWidth(200);
//@设置验证码高度
//$captcha5->setHeight(50);
//@设置字符个数
$captcha5->setTextNumber(5);
//@设置字符颜色
//$captcha5->setFontColor('#ff9900');
//@设置字号大小
//$captcha5->setFontSize(25);
//@设置字体
$captcha5->setFontFamily('c:\windows\fonts\STXINGKA.TTF');
//@设置语言
$captcha5->setTextLang('cn');
//@设置背景颜色
//$captcha5->setBgColor('#000000');
//@设置干扰点数量
//$captcha5->setNoisePoint(600);
//@设置干扰线数量
//$captcha5->setNoiseLine(10);
//@设置是否扭曲
//$captcha5->setDistortion(true);
//@设置是否显示边框
$captcha5->setShowBorder(true);
//输出验证码
$code=$captcha5->createImage();
//$_SESSION['captchaCode']['content']=$code;
//$_SESSION['captchaCode']['time']=microtime();
?>
|
为php环境的朋友分享一下关于php需要禁止一些危险函数,朋家可以根据自己的需求来禁止一下不用的功能函数。
phpinfo()
功能描述:输出 PHP 环境信息以及相关的模块、WEB 环境等信息。
危险等级:中
passthru()
功能描述:允许执行一个外部程序并回显输出,类似于 exec()。
危险等级:高
exec()
功能描述:允许执行一个外部程序(如 UNIX Shell 或 CMD 命令等)。
危险等级:高
system()
功能描述:允许执行一个外部程序并回显输出,类似于 passthru()。
危险等级:高
chroot()
功能描述:可改变当前 PHP 进程的工作根目录,仅当系统支持 CLI 模式
PHP 时才能工作,且该函数不适用于 Windows 系统。
危险等级:高
scandir()
功能描述:列出指定路径中的文件和目录。
危险等级:中
chgrp()
功能描述:改变文件或目录所属的用户组。
危险等级:高
chown()
功能描述:改变文件或目录的所有者。
危险等级:高
shell_exec()
功能描述:通过 Shell 执行命令,并将执行结果作为字符串返回。
危险等级:高
proc_open()
功能描述:执行一个命令并打开文件指针用于读取以及写入。
危险等级:高
proc_get_status()
功能描述:获取使用 proc_open() 所打开进程的信息。
危险等级:高
error_log()
功能描述:将错误信息发送到指定位置(文件)。
安全备注:在某些版本的 PHP 中,可使用 error_log() 绕过 PHP safe mode,
执行任意命令。
危险等级:低
ini_alter()
功能描述:是 ini_set() 函数的一个别名函数,功能与 ini_set() 相同。
具体参见 ini_set()。
危险等级:高
ini_set()
功能描述:可用于修改、设置 PHP 环境配置参数。
危险等级:高
ini_restore()
功能描述:可用于恢复 PHP 环境配置参数到其初始值。
危险等级:高
dl()
功能描述:在 PHP 进行运行过程当中(而非启动时)加载一个 PHP 外部模块。
危险等级:高
pfsockopen()
功能描述:建立一个 Internet 或 UNIX 域的 socket 持久连接。
危险等级:高
syslog()
功能描述:可调用 UNIX 系统的系统层 syslog() 函数。
危险等级:中
readlink()
功能描述:返回符号连接指向的目标文件内容。
危险等级:中
symlink()
功能描述:在 UNIX 系统中建立一个符号链接。
危险等级:高
popen()
功能描述:可通过 popen() 的参数传递一条命令,并对 popen() 所打开的文件进行执行。
危险等级:高
stream_socket_server()
功能描述:建立一个 Internet 或 UNIX 服务器连接。
危险等级:中
putenv()
功能描述:用于在 PHP 运行时改变系统字符集环境。在低于 5.2.6 版本的 PHP 中,可利用该函数
修改系统字符集环境后,利用 sendmail 指令发送特殊参数执行系统 SHELL 命令。
危险等级:高
禁用方法如下:
打开/etc/php.ini文件,
查找到 disable_functions ,添加需禁用的函数名,如下:
phpinfo,eval,passthru,exec,system,chroot,scandir,chgrp,chown,shell_exec,proc_open,proc_get_status
随机密码很简单,但是有很多可循地方,都是由一些特殊的字符串组成,下面我们来看看下面三种方法哦。
方法一:
1、在 33 – 126 中生成一个随机整数,如 35,
2、将 35 转换成对应的ASCII码字符,如 35 对应 #
3、重复以上 1、2 步骤 n 次,连接成 n 位的密码
该算法主要用到了两个函数,mt_rand ( int $min , int $max )函数用于生成随机整数,其中 $min – $max 为 ASCII 码的范围,这里取 33 -126 ,可以根据需要调整范围,如ASCII码表中 97 – 122 位对应 a – z 的英文字母,具体可参考 ASCII码表; chr ( int $ascii )函数用于将对应整数 $ascii 转换成对应的字符。
代码如下 |
复制代码 |
function create_password($pw_length = 8)
{
$randpwd = '';
for ($i = 0; $i < $pw_length; $i++)
{
$randpwd .= chr(mt_rand(33, 126));
}
return $randpwd;
}
// 调用该函数,传递长度参数$pw_length = 6
echo create_password(6);
|
方法二:
1、预置一个的字符串 $chars ,包括 a – z,A – Z,0 – 9,以及一些特殊字符
2、在 $chars 字符串中随机取一个字符
3、重复第二步 n 次,可得长度为 n 的密码
代码如下 |
复制代码 |
function generate_password( $length = 8 ) {
// 密码字符集,可任意添加你需要的字符
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
$password = '';
for ( $i = 0; $i < $length; $i++ )
{
// 这里提供两种字符获取方式
// 第一种是使用 substr 截取$chars中的任意一位字符;
// 第二种是取字符数组 $chars 的任意元素
// $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
$password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
}
return $password;
}
|
使用PHP开发应用程序,尤其是网站程序,常常需要生成随机密码,如用户注册生成随机密码,用户重置密码也需要生成一个随机的密码。随机密码也就是一串固定长度的字符串,这里我收集整理了几种生成随机字符串的方法,以供大家参考。
方法一:
1、在 33 – 126 中生成一个随机整数,如 35,
2、将 35 转换成对应的ASCII码字符,如 35 对应 #
3、重复以上 1、2 步骤 n 次,连接成 n 位的密码
该算法主要用到了两个函数,mt_rand ( int $min , int $max )函数用于生成随机整数,其中 $min – $max 为 ASCII 码的范围,这里取 33 -126 ,可以根据需要调整范围,如ASCII码表中 97 – 122 位对应 a – z 的英文字母,具体可参考 ASCII码表; chr ( int $ascii )函数用于将对应整数 $ascii 转换成对应的字符。
代码如下 |
复制代码 |
function create_password($pw_length = 8)
{
$randpwd = '';
for ($i = 0; $i < $pw_length; $i++)
{
$randpwd .= chr(mt_rand(33, 126));
}
return $randpwd;
}
// 调用该函数,传递长度参数$pw_length = 6
echo create_password(6);
|
方法二:
1、预置一个的字符串 $chars ,包括 a – z,A – Z,0 – 9,以及一些特殊字符
2、在 $chars 字符串中随机取一个字符
3、重复第二步 n 次,可得长度为 n 的密码
代码如下 |
复制代码 |
function generate_password( $length = 8 ) {
// 密码字符集,可任意添加你需要的字符
$chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
$password = '';
for ( $i = 0; $i < $length; $i++ )
{
// 这里提供两种字符获取方式
// 第一种是使用 substr 截取$chars中的任意一位字符;
// 第二种是取字符数组 $chars 的任意元素
// $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
$password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
}
return $password;
}
|
一款比较完美的php防sql注入代码,很多初学者都有被sql注入的经验吧,今天我们来分享你一款比较完整的sql防注入代码,有需要的同学可以参考一下/
代码如下 |
复制代码 |
<?
/*************************
说明:
判断传递的变量中是否含有非法字符
如$_POST、$_GET
功能:
防注入
*************************/
//要过滤的非法字符
$ArrFiltrate=array("'","or","and","union","where");
//出错后要跳转的url,不填则默认前一页
$StrGoUrl="";
//是否存在数组中的值
function FunStringExist($StrFiltrate,$ArrFiltrate){
foreach ($ArrFiltrate as $key=>$value){
if (eregi($value,$StrFiltrate)){
return true;
}
}
return false;
}
//合并$_POST 和 $_GET
if(function_exists(array_merge)){
$ArrPostAndGet=array_merge($HTTP_POST_VARS,$HTTP_GET_VARS);
}else{
foreach($HTTP_POST_VARS as $key=>$value){
$ArrPostAndGet[]=$value;
}
foreach($HTTP_GET_VARS as $key=>$value){
$ArrPostAndGet[]=$value;
}
}
//验证开始
foreach($ArrPostAndGet as $key=>$value){
if (FunStringExist($value,$ArrFiltrate)){
echo "<script language='javascript'>alert('传递的信息中不得包含{',or,and,union}等非法字符请您把他们换成{‘,OR,AND,UNION}');</script>";
if (empty($StrGoUrl)){
echo "<script language='javascript'>history.go(-1);</script>";
}else{
echo "<script language='javascript'>window.location='".$StrGoUrl."';</script>";
}
exit;
}
}
/***************结束防止PHP注入*****************/
?> |
SQL注入攻击是黑客攻击网站最常用的手段。如果你的站点没有使用严格的用户输入检验,那么常容易遭到SQL注入攻击。SQL注入攻击通常通过给站点数据库提交不良的数据或查询语句来实现,很可能使数据库中的纪录遭到暴露,更改或被删除。
为了防止SQL注入攻击,PHP自带一个功能可以对输入的字符串进行处理,可以在较底层对输入进行安全上的初步处理,也即Magic Quotes。(php.ini magic_quotes_gpc)。如果magic_quotes_gpc选项启用,那么输入的字符串中的单引号,双引号和其它一些字符前将会被自动加 上反斜杠。
但Magic Quotes并不是一个很通用的解决方案,没能屏蔽所有有潜在危险的字符,并且在许多服务器上Magic Quotes并没有被启用。所以,我们还需要使用其它多种方法来防止SQL注入。
许 多数据库本身就提供这种输入数据处理功能。例如PHP的MySQL操作函数中有addslashes()、 mysql_real_escape_string()、mysql_escape_string()等函数,可将特殊字符和可能引起数据库操作出错的字 符转义。那么这三个功能函数之间有什么却别呢?下面我们就来详细讲述下。
虽然国内很多PHP程序员仍在依靠addslashes防止SQL注入,还是建议大家加强中文防止SQL注入的检查。addslashes的问题在 于黑客 可以用0xbf27来代替单引号,而addslashes只是将0xbf27修改为0xbf5c27,成为一个有效的多字节字符,其中的0xbf5c仍会 被看作是单引号,所以addslashes无法成功拦截。
当然addslashes也不是毫无用处,它是用于单字节字符串的处理,多字节字符还是用mysql_real_escape_string吧。
另外对于php手册中get_magic_quotes_gpc的举例:
代码如下 |
复制代码 |
if (!get_magic_quotes_gpc()) {
$lastname = addslashes($_POST[‘lastname’]);
} else {
$lastname = $_POST[‘lastname’];
}
|
最好对magic_quotes_gpc已经开放的情况下,还是对$_POST[’lastname’]进行检查一下。
再说下mysql_real_escape_string和mysql_escape_string这2个函数的区别:
mysql_real_escape_string 必须在(PHP 4 >= 4.3.0, PHP 5)的情况下才能使用。否则只能用 mysql_escape_string ,两者的区别是:mysql_real_escape_string 考虑到连接的当前字符集,而mysql_escape_string 不考虑。
总结一下:
* addslashes() 是强行加;
* mysql_real_escape_string() 会判断字符集,但是对PHP版本有要求;
* mysql_escape_string不考虑连接的当前字符集。
dz中的防止sql注入就是用addslashes这个函数,同时在dthmlspecialchars这个函数中有进行一些替换$string = preg_replace(/&((#(d{3,5}|x[a-fA-F0-9]{4}));)/, &1,这个替换解决了注入的问题,同时也解决了中文乱码的一些问题
标签:[!--infotagslink--]