存档

文章标签 ‘验证码’

图片验证码confirmCode类使用说明

2020年12月9日 评论已被关闭

我们需要如下:demo.html+ajax/doConfirmCode.php+demoSave.php+confirmCode.php(php类,不提供)+confirmCode.js(js类,不提供)

我们先看看demo.html的代码

<!doctype html>
<html>
<head>
	<meta charset="utf-8">
	<title>验证码Demo</title>
	<style>
	* {box-sizing:border-box;}
	#code{border:#ababab solid 1px;width:100px;height:30px;line-height:30px;vertical-align:middle;border-radius:6px;}
	#img{border:1px solid red;cursor:pointer;vertical-align:middle;border-radius:6px;width:100px; height:30px; display: inline-block;}
	#button{cursor:pointer;vertical-align:middle;width:100px;height:30px;line-height:30px;background:#000;color:#fff;display:inline-block;text-align:center;border-radius:6px;border:none;margin-left:30px;}
	#box{width:90%;margin:0 auto;margin-top:100px;}
	</style>
</head>
	
<body>
	<div id="box">
		<input type="text" id="code" value="" />
		<img id="img" />
		<input id="button" type="button" value="保存提交" />
	</div>

	<script type="text/ecmascript" src="static/jquery-1.12.4.min.js"></script>
	<script type="text/ecmascript" src="static/confirmCode.js"></script>
	<script type="text/ecmascript">
		$(function(){
			var yzm = new confirmCode({
				//基本配置
				input:'#code',//必填,验证码输入框id或css选择器,比如: '#code'或者'.input_captcha'
				input_maxlength:4,//可以不填,验证码长度, 默认是4(js会自动将控件的maxlength改为input_maxlength,并且input_onblur_check的参数也会先判断长度然后来处理)
				input_first_auto_focus:false,//可以不填,验证码输入框第一次加载是否自动获取焦点并清空值,默认false
				img:'#img',//必填,显示验证码的容器id或css选择器,比如: '#imgcode'或者'.img_captcha'
				img_loading:{
					first_unautoload_default_img:'',//可以不填,填写了代表验证码第一次默认不自动加载所设置的默认图片,可以用远程地址:https://static.vancdn.com/public/img/yzm/click2load.png
					first_load_delay:2000,//第一次延迟加载的毫秒,默认0(如果first_unautoload_default_img设置为非空,则无效)
					loading_img:'static/loading.png',//可以不填,加载验证码过程中的默认图片,可以用远程地址:https://static.vancdn.com/public/img/yzm/loading.png
					loading_fail_img:'static/error.png',//可以不填,加载验证码失败的图片,可以用远程地址:https://static.vancdn.com/public/img/yzm/error.png
					loading_fail_func:function(msg){//验证加载失败的回调函数,如果不操作请申明空function
						alert('验证码加载失败:'+msg+';在此进行相关书写');
					}
				},
				get_url:'ajax/doConfirmCode.php',//必填,获取验证码的url
				
				//第一种情况:在提交保存数据的js代码中验证验证码的合法性(验证长度之后,是否进行js的md5验证)
				//第二种情况:如果开启了input_onblur.open,则先走js的md5验证再走input_onblur.server_check_url(如果设置了的话)
				verify_by_js_md5:true,//可以不填,默认false
				
				//input控件onblur的监控处理
				input_onblur:{
					open:true,//是否在input的onblur时交互检查,总开关(当设置为true的时候,如果verify_by_js_md5==true,那么先走js的md5验证,再走服务端验证)
					server_check_url:'ajax/doConfirmCode.php',//启用服务器端验证时请求的url(如果没设置,则不走服务端验证)
					success:function(){//验证成功的回调函数,如果不操作请申明空function
						alert('onblur验证成功;在此进行相关书写');
					},
					fail:function(msg){//验证失败的回调函数,如果不操作请申明空function
						alert('onblur验证失败:'+msg+';在此进行相关书写');
					}
				},
				
				console_test:true,//是否调试console日志,默认false
			});
			
			//适合保存的时候检查验证码是否有效,根据yzm.verify_save()返回值进行相关操作
			$("#button").on('click',function(){
				//这里仅仅只做验证码的逻辑处理,自己其他业务自己处理
				if(yzm.verify_save()==true){
					
					//在这里获取到输入框的值和key
					var yzmValue=yzm.getYzm();
					alert("yzmkey:"+yzmValue.yzmkey+",yzmvalue:"+yzmValue.yzmkey);
					
					alert('验证码验证成功,书写自己需要的代码,比如请求ajax...');
				}
				else {
					alert('验证码验证失败,书写自己需要的代码,比如return false');
				}
			});
		});
	</script>
</body>
</html>

再看看ajax/doConfirmCode.php

<?php
header('content-type:text/plain; charset=utf-8');
//本代码基本可以不用重写,除非遇到参数不一致
switch($_POST['action']){
	case 'get':
		echo json_encode(Myzm()->get(
			array(
				'width'=>100,//图片宽度px,默认100
				'height'=>30,//图片高度px,默认30
				'textNum'=>4,//验证码(字母+数字)数量,默认4
				'timeout'=>600,//验证码有效期秒,默认600
			)
		));
	break;
	case 'onblurcheck':
		echo json_encode(Myzm()->check(
			array(
				'yzmcode'=>$_POST['yzmcode'],//用户输入的code
				'yzmkey'=>$_POST['yzmkey'],//验证码的key
				'textNum'=>4,//验证码长度,默认为4
				'delyzmkey'=>false,//是否删除key,默认为true,特别注意:ajax执行check时不删除key, 待form提交后再删除
			)
		));
	break;
	default:break;
}

在看看demoSave.php

<?php
header('content-type:text/plain; charset=utf-8');
/***示例检查验证码的代码-开始***/
$yzmCheck=Myzm()->check(
	array(
		'yzmcode'=>$_POST['yzmcode'],//用户输入的code
		'yzmkey'=>$_POST['yzmkey'],//验证码的key
		'textNum'=>4,//验证码长度,默认为4
		'delyzmkey'=>true,//是否删除key,默认为true,特别注意:ajax执行check时不删除key, 待form提交后再删除
	)
);
if($yzmCheck['status']=='error'){
	echo $yzmCheck['errorStr'];//通过$yzmCheck['errorStr']进行自己的相关操作
}
/***示例检查验证码的代码-结束***/

nginx配置反向代理使用discuz的时候无法加载验证码和安全问答

2018年6月22日 没有评论

今天配置nginx反向代理的时候,发现无法加载验证码和安全问答问题

经过研究和谷歌搜索,发现原来discuz会在头部判断用户来源

所以解决办法是在nginx的虚拟主机配置中加入:

proxy_pass_header User-Agent;

proxy_set_header Host $host;

这样才能正确加载discuz中的验证码和安全问题

如果你也遇到了在nginx中无法加载discuz中的验证码,请使用上方同样的方法,我列举一个虚拟主机的配置例子:

server {
listen 80;
server_name bbs.aaa.com;
location / {
proxy_pass http://bbs.aaa.com:82;
proxy_redirect http://bbs.aaa.com:82/ /;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_pass_header User-Agent;
proxy_set_header Host $host;

}
access_log logs/bbs.aaa.com.access.log;
}

php生成文件夹版验证码

2015年7月14日 没有评论

1,文件夹yzm.png

2,文件:confirmcode.class.php

3,文件:create.php

4,字体:font.ttf

5,文件:如何调用.php

——————————-

confirmcode.class.php:

<?php
header(‘Content-type:image/png’);
class imageCaptcha{
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;
public function imageCaptcha(){
$this->textNum=4;
$this->fontSize=15;
$this->fontFamily=”;
$this->textLang=’en’;
$this->noisePoint=0;
$this->noiseLine=0;
$this->distortion=false;
$this->showBorder=false;
}
public function set_show_mode($w,$h,$num,$fc,$fz,$ff_url,$lang,$bc,$m,$n,$b,$border){
$this->width=$w;
$this->height=$h;
$this->textNum=$num;
$this->fontColor=sscanf($fc,’#%2x%2x%2x’);
$this->fontSize=$fz;
$this->fontFamily=$ff_url;
$this->textLang=$lang;
$this->bgColor=sscanf($bc,’#%2x%2x%2x’);
$this->noisePoint=$m;
$this->noiseLine=$n;
$this->distortion=$b;
$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=floor($this->fontSize*2.5);
}
$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=’ABCDEFGHJKLMNPQRSTVWXYabcdehkmnprsvwxy35678′;
for($i=0;$i<$this->textNum;$i++){
$string=$string.’,’.$str[mt_rand(0,42)];
}
break;
}
return substr($string,1);
}
public function createText(){
$text_array=explode(‘,’,$this->randText($this->textLang));
$this->textContent=join(”,$text_array);
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,8+$i*floor($this->fontSize*1.3),3+floor($this->height*0.75),$this->randFontColor,$this->fontFamily,$text_array[$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();
$this->createNoisePoint();
$this->createNoiseLine();
if($this->distortion!=false){
$this->distortionText();
}
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!=false){
imagedestroy($this->distortionImage);
}
return $this->textContent;
}
}
$code_obj=new imageCaptcha();
$code_obj->set_show_mode(’85’,’30’,’4′,”,’15’,’font.ttf’,’en’,’#fcfefb’,mt_rand(5,10),mt_rand(1,3),false,false);
$code=$code_obj->createImage();
$f=fopen(‘code.txt’,”w”);
fwrite($f,strtolower($code));
fclose($f);
?>

——————————–

create.php:

<?php
//die(“closed”);
header(‘content-type:text/plain; charset=utf-8’);
set_time_limit(0);
for($i=1;$i<=20;$i++){
mkdir(‘yzm.png/’.$i);
for($k=1;$k<=50;$k++){
//获取图片
ob_start();
readfile(‘http://test.shiwufenzhong.com/create_yzm/confirmcode.class.php’);
$img=ob_get_contents();
ob_end_clean();
//获取验证码
$fp=fopen(‘code.txt’,”r”);
$code=fread($fp,1024);
fclose($fp);
//保存图片
$md5_code=hash(‘md5’,$code);
$md5_picname=substr(hash(‘md5’,$code.$i.$k),8,16);
$fp=fopen(‘yzm.png/’.$i.’/’.$md5_picname.’.png’,”w”);
fwrite($fp,$img);
fclose($fp);
//生成php文件夹
mkdir(‘yzm.png/’.$i.’/php’);
//生成php文件
$fp=fopen(‘yzm.png/’.$i.’/php/’.$k.’.php’,”w”);
fwrite($fp,”<?php \$yzm=array(‘pic’=>'”.$md5_picname.”‘,’key’=>'”.$md5_code.”‘);?>”);
fclose($fp);
}
}
die(“<h1>done …</h1>”);
?>

————————–

如何调用.php:

<?php
$yzmpngPath=mt_rand(1,20).’/’;
require(‘yzm.png/’.$yzmpngPath.’php/’.mt_rand(1,50).’.php’);
$session_id=$_POST[‘sessionid’];
if($session_id){
if(!preg_match(‘/^[a-zA-Z0-9]{26}$/’,$session_id)){exit();}
session_id($session_id);session_start();
}
else {session_start();$session_id=session_id();}
//请求超过3次的时候,以后每次请求间隔需要10秒
$new_session_n=intval($_SESSION[‘vanpeopleYZM’][‘requestNums’])+1;
if($new_session_n>4){
$time_line=$_SERVER[‘REQUEST_TIME’]-intval($_SESSION[‘vanpeopleYZM’][‘requestTime’]);
if($time_line<10){
echo json_encode(array(‘status’=>’error’,’errorStr’=>’速度过快,’.(10-$time_line).’秒后重试’,’fastCounterTime’=>10-$time_line,’sessionid’=>$session_id));
exit();
}
else {$new_session_n=1;}
}
$_SESSION[‘YZM’]=array(
‘key’=>$yzm[‘key’],’requestTime’=>$_SERVER[‘REQUEST_TIME’],’requestNums’=>$new_session_n
);
echo json_encode(
array(
‘status’=>’ok’,’sessionid’=>$session_id,’pic’=>$yzmpngPath.$yzm[‘pic’],’key’=>$yzm[‘key’]
)
);exit();
?>

 

分类: php学习 标签:

discuz论坛地址栏带端口号的解决方法【nginx+apache验证码不显示】

2014年12月24日 没有评论

我们往往会使用nginx+apache来作为服务器环境,nginx使用80端口,Apache使用82端口,那么当我们安装discuz的时候,在加入了nginx映射到Apache端口后,往往discuz的链接地址栏上会出现端口号,如82

那么我们如何解决这个问题呢?

x2寻找文件source/class/class_core.php,寻找:$_G[‘siteport’]

将此行注释即可,或者强制在此文件的头部,加上:$_SERVER[‘HTTP_HOST’]=”www.域名.com”;

到此就可以解决discuz论坛地址栏带端口号的解决方法【nginx+apache反射】,但是x2.5是修改:

source/class/discuz/discuz_application.php

————————–

但是你会发现另外一个问题,就是验证码不能正确显示,解决办法如下:

直接在config/config_global.php下增加$_SERVER[‘HTTP_HOST’]=”www.域名.com”;即可解决所有问题

研究半天实在搞不定,原来还有此绝招~

 

php生成验证码类【还不错】

2014年12月11日 没有评论

<?php
session_start();
header(‘Content-type:image/png’);
$serverTime = $_SERVER[‘REQUEST_TIME’];
if ($_SESSION[‘confirmCode’][‘errorTimes’] > 8 && $serverTime – $_SESSION[‘confirmCode’][‘time’] < 20) {
$_SESSION[‘confirmCode’][‘code’] = ‘whatabadman!’;
$img_file = ‘yzmBad.gif’;
$fp = fopen($img_file, ‘rb’);
$content = fread($fp, filesize($img_file));
fclose($fp);
echo $content;
} else {
if ($_SESSION[‘confirmCode’][‘errorTimes’] > 8) {
$_SESSION[‘confirmCode’][‘errorTimes’] = 1;
} else {
$_SESSION[‘confirmCode’][‘errorTimes’] = intval($_SESSION[‘confirmCode’][‘errorTimes’]) + 1;
}
}
class imageCaptcha
{
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;
public function imageCaptcha()
{
$this->textNum = 4;
$this->fontSize = 14;
$this->fontFamily = ”;
$this->textLang = ‘en’;
$this->noisePoint = 0;
$this->noiseLine = 0;
$this->distortion = false;
$this->showBorder = false;
}
public function set_show_mode($w, $h, $num, $fc, $fz, $ff_url, $lang, $bc, $m, $n, $b, $border)
{
$this->width = $w;
$this->height = $h;
$this->textNum = $num;
$this->fontColor = sscanf($fc, ‘#%2x%2x%2x’);
$this->fontSize = $fz;
$this->fontFamily = $ff_url;
$this->textLang = $lang;
$this->bgColor = sscanf($bc, ‘#%2x%2x%2x’);
$this->noisePoint = $m;
$this->noiseLine = $n;
$this->distortion = $b;
$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 = floor($this->fontSize * 2.5);
}
$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 = ‘ABCDEFGHJKLMNPQRSTVWXYabcdehkmnprsvwxy35678’;
for ($i = 0; $i < $this->textNum; $i++) {
$string = $string . ‘,’ . $str[mt_rand(0, 42)];
}
break;
}
return substr($string, 1);
}
public function createText()
{
$text_array = explode(‘,’, $this->randText($this->textLang));
$this->textContent = join(”, $text_array);
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, $text_array[$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();
$this->createNoisePoint();
$this->createNoiseLine();
if ($this->distortion != false) {
$this->distortionText();
}
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 != false) {
imagedestroy($this->distortionImage);
}
return $this->textContent;
}
}
$code_obj = new imageCaptcha();
$code_obj->set_show_mode(’85’, ’30’, ‘4’, ”, ’14’, ‘font/’ . rand(1, 6) . ‘.ttf’, ‘en’, ‘#fcfefb’, mt_rand(5, 10), mt_rand(1, 3), false, false);
$_SESSION[‘confirmCode’][‘code’] = $code_obj->createImage();
$_SESSION[‘confirmCode’][‘time’] = $serverTime;

?>

分类: php学习 标签:
css.php