⑴ 在线等 php 的验证码 我图片是生成了 但是 session里面是空的 是什么原因啊急等高手解答
试试输出$number_len这个值看看
⑵ PHP如何生成加减算法方式的验证码
<?php
namespace mobile\components;
/**
* @author fenghuo
*
* 改造的加减法验证类
* 使用示例 VerifyCode::get(1,2);
* 验证示例 VerifyCode::check($code);
*/
class VerifyCode
{
/**
* php验证码
*/
public static function get($one,$two,$prefix = '', $font_size = 28)
{
//文件头...
ob_get_clean();
header("Content-type: image/png;charset=utf-8;");
//创建真彩色白纸
$width = $font_size*5;
$height = $font_size+1;
$im = @imagecreatetruecolor($width, $height) or die("建立图像失败");
//获取背景颜色
$background_color = imagecolorallocate($im, 255, 255, 255);
//填充背景颜色
imagefill($im, 0, 0, $background_color);
//获取边框颜色
$border_color = imagecolorallocate($im, 200, 200, 200);
//画矩形,边框颜色200,200,200
imagerectangle($im,0,0,$width - 1, $height - 1,$border_color);
//逐行炫耀背景,全屏用1或0
for($i = 2;$i < $height - 2;$i++) {
//获取随机淡色
$line_color = imagecolorallocate($im, rand(200,255), rand(200,255), rand(200,255));
//画线
imageline($im, 2, $i, $width - 1, $i, $line_color);
}
//设置印上去的文字
$firstNum = $one;
$secondNum = $two;
$actionStr = $firstNum > $secondNum ? '-' : '+';
//获取第1个随机文字
$imstr[0]["s"] = $firstNum;
$imstr[0]["x"] = rand(2, 5);
$imstr[0]["y"] = rand(1, 4);
//获取第2个随机文字
$imstr[1]["s"] = $actionStr;
$imstr[1]["x"] = $imstr[0]["x"] + $font_size - 1 + rand(0, 1);
$imstr[1]["y"] = rand(1,5);
//获取第3个随机文字
$imstr[2]["s"] = $secondNum;
$imstr[2]["x"] = $imstr[1]["x"] + $font_size - 1 + rand(0, 1);
$imstr[2]["y"] = rand(1, 5);
//获取第3个随机文字
$imstr[3]["s"] = '=';
$imstr[3]["x"] = $imstr[2]["x"] + $font_size - 1 + rand(0, 1);
$imstr[3]["y"] = 3;
//获取第3个随机文字
$imstr[4]["s"] = '?';
$imstr[4]["x"] = $imstr[3]["x"] + $font_size - 1 + rand(0, 1);
$imstr[4]["y"] = 3;
//文字
$text = '';
//写入随机字串
for($i = 0; $i < 5; $i++) {
//获取随机较深颜色
$text_color = imagecolorallocate($im, rand(50, 180), rand(50, 180), rand(50, 180));
$text .= $imstr[$i]["s"];
//画文字
imagechar($im, $font_size, $imstr[$i]["x"], $imstr[$i]["y"], $imstr[$i]["s"], $text_color);
}
session_start();
$_SESSION[$prefix.'verifycode'] = $firstNum > $secondNum ? ($firstNum - $secondNum) : ($firstNum + $secondNum);
//显示图片
ImagePng($im);
//销毁图片
ImageDestroy($im);
}
public static function check($code)
{
if(trim($_SESSION[$prefix.'verifycode']) == trim($code)) {
return true;
} else {
return false;
}
}
⑶ 验证码怎么用php实现
<?php
/*
* Filename: authpage.php
*/
srand((double)microtime()*1000000);
//验证用户输入是否和验证码一致
if(isset($HTTP_POST_VARS['authinput']))
{
if(strcmp($HTTP_POST_VARS['authnum'],$HTTP_POST_VARS['authinput'])==0)
echo "验证成功!";
else
echo "验证失败!";
}
//生成新的四位整数验证码
while(($authnum=rand()%10000)<1000);
?>
<form action=authpage.php method=post>
<table>
请输入验证码:<input type=text name=authinput style="width:
80px"><br>
<input type=submit name="验证" value="提交验证码">
<input type=hidden name=authnum value=<? echo $authnum; ?>>
<img src=authimg.php?authnum=<? echo $authnum; ?>>
</table>
</form>
代码二:
<?php
/*
* Filename: authimg.php
* Author: hutuworm
* Date: 2003-04-28
* @Copyleft hutuworm.org
*/
//生成验证码图片
Header("Content-type: image/PNG");
srand((double)microtime()*1000000);
$im = imagecreate(58,28);
$black = ImageColorAllocate($im, 0,0,0);
$white = ImageColorAllocate($im, 255,255,255);
$gray = ImageColorAllocate($im, 200,200,200);
imagefill($im,68,30,$gray);
//将四位整数验证码绘入图片
imagestring($im, 5, 10, 8, $HTTP_GET_VARS['authnum'], $black);
for($i=0;$i<50;$i++) //加入干扰象素
{
imagesetpixel($im, rand()%70 , rand()%30 , $black);
}
ImagePNG($im);
ImageDestroy($im);
?>
⑷ 怎么让php生成的验证码随机数不重复
如果要生成四位数字的验证码,则可以用函数: $srand = rand(1000,9999); 会生成在1000到9999之间的随机数字,如果要生成更多位数的数字,可以更改最孝最大值。
⑸ 请问PHP生成验证码的类怎么写
<?php
classCode{
//1.定义各个成员有宽、高、画布、字数、类型、画类型
private$width;//宽度
private$height;//高度
private$num;//验证码字数
private$imgType;//生成图片类型
private$Type;//字串类型1,2,3三个选项1纯数字2纯小写字母3大小写数字混合
private$hb;//画布
public$codestr;//验证码字串
publicfunction__construct($height=20,$num=4,$imgType="jpeg",$Type=1){
$this->width=$num*20;
$this->height=$height;
$this->num=$num;
$this->imgType=$imgType;
$this->Type=$Type;
$this->codestr=$this->codestr();
$this->zuhe();
}
//2.定义随机获取字符串函数
privatefunctioncodestr(){
switch($this->Type){
case1://类型为1获取1-9随机数
$str=implode("",array_rand(range(0,9),$this->num));
break;
case2://类型为2获取a-z随机小写字母
$str=implode("",array_rand(array_flip(range(a,z)),$this->num));
break;
case3://类型为3获取数字,小写字母,大写字母混合
for($i=0;$i<$this->num;$i++){
$m=rand(0,2);
switch($m){
case0:
$o=rand(48,57);
break;
case1:
$o=rand(65,90);
break;
case2:
$o=rand(97,122);
break;
}
$str.=sprintf("%c",$o);
}
break;
}
return$str;
}
//3.初始化画布图像资源
privatefunctionHb(){
$this->hb=imagecreatetruecolor($this->width,$this->height);
}
//4.生成背景颜色
privatefunctionBg(){
returnimagecolorallocate($this->hb,rand(130,250),rand(130,250),rand(130,250));
}
//5.生成字体颜色
privatefunctionFont(){
returnimagecolorallocate($this->hb,rand(0,100),rand(0,100),rand(0,100));
}
//6.填充背景颜色
privatefunctionBgColor(){
imagefilledrectangle($this->hb,0,0,$this->width,$this->height,$this->Bg());
}
//7.干扰点
privatefunctionganrao(){
$sum=floor(($this->width)*($this->height)/3);
for($i=0;$i<$sum;$i++){
imagesetpixel($this->hb,rand(0,$this->width),rand(0,$this->height),$this->Bg());
}
}
//8.随机直线弧线
privatefunctionhuxian(){
for($i=0;$i<$this->num;$i++){
imageArc($this->hb,rand(0,$this->width),rand(0,$this->height),rand(0,$this->width),rand(0,$this->height),rand(0,360),rand(0,360),$this->Bg());
}
}
//9.写字
privatefunctionxiezi(){
for($i=0;$i<$this->num;$i++){
$x=ceil($this->width/$this->num)*$i;
$y=rand(1,$this->height-15);
imagechar($this->hb,5,$x+4,$y,$this->codestr[$i],$this->Font());
}
}
//10.输出
privatefunctionOutImg(){
$shuchu="image".$this->imgType;
$header="Content-type:image/".$this->imgType;
if(function_exists($shuchu)){
header($header);
$shuchu($this->hb);
}else{
exit("GD库没有此类图像");
}
}
//11.拼装
privatefunctionzuhe(){
$this->Hb();
$this->BgColor();
$this->ganrao();
$this->huxian();
$this->xiezi();
$this->OutImg();
}
publicfunctiongetCodeStr(){
return$this->codestr;
}
}
$a=newCode();
$a->getCodeStr();
?>
⑹ PHP图片中文验证码生成问题
重新写了一下,这里不能print那个字符串..不然会出错
因为启动了session 之前不能有输出
最终$_SESSION['yzm']就是图片上的验证码..
--------------------
<?php
session_start();
$im = imagecreatetruecolor(80,30);
$bg = imagecolorallocate($im,0,0,0);
$te = imagecolorallocate($im,255,255,255);
for($i=0;$i<5;$i++)
{
$te2 = imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));
imageline($im,rand(0,80),0,80,rand(0,30),$te2);
}
for($i=0;$i<200;$i++)
{
imagesetpixel($im,rand()%100,rand()%30,$te2);
}
srand((float) microtime() * 10000000);
$input = array("步","步","高","请","转","到","图","像","我","非","常","高","兴");
$rand_keys = array_rand($input, 4);
$_SESSION['yzm'] = $t = $input[$rand_keys[0]].$input[$rand_keys[1]].$input[$rand_keys[2]].$input[$rand_keys[3]];
imagettftext($im,12,0,10,20,$te,'stxingka.ttf',$t);
header("content-type: image/jpeg");
imagejpeg($im);
?>
⑺ 求 PHP 图片验证码类 给出详细调用方法 谢谢!!!
[code.php]
<?php
/**
*验证码图片
*/
session_start();
Header("Content-type:image/gif");
/*
*初始化
*/
$border=0;//是否要边框1要:0不要
$how=4;//验证码位数
$w=$how*15;//图片宽度
$h=20;//图片高度
$fontsize=10;//字体大小
$alpha="abcdefghijkmnpqrstuvwxyz";//验证码内容1:字母
$number="23456789";//验证码内容2:数字
$randcode="";//验证码字符串初始化
srand((double)microtime()*1000000);//初始化随机数种子
$im=ImageCreate($w,$h);//创建验证图片
/*
*绘制基本框架
*/
$bgcolor=ImageColorAllocate($im,255,255,255);//设置背景颜色
ImageFill($im,0,0,$bgcolor);//填充背景色
if($border)
{
$black=ImageColorAllocate($im,0,0,0);//设置边框颜色
ImageRectangle($im,0,0,$w-1,$h-1,$black);//绘制边框
}
/*
*逐位产生随机字符
*/
for($i=0;$i<$how;$i++)
{
$alpha_or_number=mt_rand(0,1);//字母还是数字
$str=$alpha_or_number?$alpha:$number;
$which=mt_rand(0,strlen($str)-1);//取哪个字符
$code=substr($str,$which,1);//取字符
$j=!$i?4:$j+15;//绘字符位置
$color3=ImageColorAllocate($im,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));//字符随即颜色
ImageChar($im,$fontsize,$j,3,$code,$color3);//绘字符
$randcode.=$code;//逐位加入验证码字符串
}
/*
*如果需要添加干扰就将注释去掉
*
*以下for()循环为绘背景干扰线代码
*/
/*+-------------------------------绘背景干扰线开始--------------------------------------------+*/
for($i=0;$i<5;$i++)//绘背景干扰线
{
$color1=ImageColorAllocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//干扰线颜色
ImageArc($im,mt_rand(-5,$w),mt_rand(-5,$h),mt_rand(20,300),mt_rand(20,200),55,44,$color1);//干扰线
}
/*+-------------------------------绘背景干扰线结束--------------------------------------+*/
/*
*如果需要添加干扰就将注释去掉
*
*以下for()循环为绘背景干扰点代码
*/
/*+--------------------------------绘背景干扰点开始------------------------------------------+*/
/*
for($i=0;$i<$how*40;$i++)//绘背景干扰点
{
$color2=ImageColorAllocate($im,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));//干扰点颜色
ImageSetPixel($im,mt_rand(0,$w),mt_rand(0,$h),$color2);//干扰点
}
*/
/*+--------------------------------绘背景干扰点结束------------------------------------------+*/
//把验证码字符串写入session方便提交登录信息时检验验证码是否正确例如:$_POST['randcode']=$_SESSION['randcode']
$_SESSION['randcode']=$randcode;
/*绘图结束*/
Imagegif($im);
ImageDestroy($im);
/*绘图结束*/
?>
[调用方法]
<SCRIPTLANGUAGE="JavaScript">
<!--
functionreloadcode(){
vard=newDate();
document.getElementById('safecode').src="/code.php?t="+d.toTimeString()
}
//-->
</SCRIPT>
验证码:<inputname="chknumber"type="text"maxlength="4"class="chknumber_input"/><imgsrc='code.php'id="safecode"onclick="reloadcode()"title="看不清楚?点击切换!"></img>
⑻ 用php生成验证码,无法显示
header("content-type:image/png");生成图像后面加这句话在输出到浏览器试试
⑼ php生成的验证码10分钟内有效如何做
把验证码生成的时候写入数据库,包含当前时间。提交的时候做判断用提交的时间减去生成的时间。超过提示无效。
⑽ PHP制作验证码时候只显示一个白色框框
<?php
ob_clean();
//header("Content-type:text/html");
header("Content-type:image/jpeg");//以jpeg格式输出,注意上面不能输出任何字符,否则出错
$w=100;//设置图片宽和高
$h=50;
$fontsize=16;//设置字体大小
$str=Array();//用来存储随机码
$string="";//随机挑选其中4个字符,也可以选择更多,注意循环的时候加上,宽度适当调整
$vcode='';
for($i=0;$i<4;$i++){
$str[$i]=$string[rand(0,strlen($string)-1)];
$vcode.=$str[$i];
}
//echo$vcode;
session_start();//启用超全局变量session
//session_register("vcode");
$_SESSION["vcode"]=$vcode;
$im=imagecreatetruecolor($w,$h);//创建图片画布
$white=imagecolorallocate($im,255,255,255);//第一次调用设置背景色
$black=imagecolorallocate($im,0,0,0);//边框颜色
imagefilledrectangle($im,0,0,$w,$h,$white);//画一矩形填充
imagerectangle($im,0,0,$w-1,$h-1,$black);//画一矩形框
$fontsrc="consola.ttf";
//生成雪花背景
for($i=1;$i<200;$i++){
$x=mt_rand(1,$w-9);
$y=mt_rand(1,$h-9);
$color=imagecolorallocate($im,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
imagechar($im,1,$x,$y,"*",$color);
}
//将验证码写入图案
for($i=0;$i<count($str);$i++){
$x=13+$i*($w-15)/4;
$y=mt_rand(25,$h-15);
$color=imagecolorallocate($im,mt_rand(0,225),mt_rand(0,150),mt_rand(0,225));
//imagechar($im,$fontsize,$x,$y,$str[$i],$color);
imagettftext($im,$fontsize,0,$x,$y,$color,$fontsrc,$str[$i]);
}
imagejpeg($im);//输出图片文件
imagedestroy($im);//销毁释放资源
?>