生成随机验证吗

WBOY
Release: 2016-06-23 13:37:55
Original
788 people have browsed it

我在网上下载了一段随机生成验证码的代码,老是无法显示图片
但是同事可以显示,代码没错,这是为啥?
需要增加什么配置吗?


回复讨论(解决方案)

在php的配置文件中找 extension=php_gd2.dll 去掉前面的;号,再重启就行了

可能你的代码文件有bom头 去掉它

把代码贴出来以供分析。

你可以将header 那句注释掉,看有没有报错。

<?php    session_start();    header("Content-type: image/PNG");    /**     * 配置方案 */    $imageWeight           = 60;    $imageHeight           = 20;    $imageFontRGB          = array('255', '255', '255');    $imageBackgroundRGB    = array('0', '0', '0');    $imagePixelNum         = 500;    $_SESSION["imagecode"] = rand(1000,9999);       /**    * 创建图片并设置颜色 */    $im    = imagecreate($imageWeight, $imageHeight);    $black = imagecolorallocate($im, $imageFontRGB[0], $imageFontRGB[1], $imageFontRGB[2]);    $gray  = ImageColorAllocate($im, $imageBackgroundRGB[0], $imageBackgroundRGB[1], $imageBackgroundRGB[2]);    imagefill($im,0,0,$gray);    /**     * 设置两条干扰线 */    $style = array($black, $black, $black, $black, $black, $gray, $gray, $gray, $gray, $gray);    imagesetstyle($im, $style);    $y1=rand(0, $imageHeight);    $y2=rand(0, $imageHeight);    $y3=rand(0, $imageHeight);    $y4=rand(0, $imageHeight);    imageline($im, 0, $y1, $imageWeight, $y3, IMG_COLOR_STYLED);    imageline($im, 0, $y2, $imageWeight, $y4, IMG_COLOR_STYLED);    /**     * 设置干扰杂点 */    for($i=0;$i<$imagePixelNum;$i++)    {     imagesetpixel($im, rand(0,255), rand(0,255), $black);    }    //将四个数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成    $strx=rand($imageWeight/8, $imageWeight/4);    for($i=0;$i<4;$i++)    {     $strpos=rand(0,$imageHeight/3);     imagestring($im,5,$strx,$strpos, substr($_SESSION["imagecode"], $i, 1), $black);     $strx+=rand(8,12);    }    imagepng($im);    imagedestroy($im);?>
Copy after login

二楼正确 GD2库没开吧

http://www.cnblogs.com/zcy_soft/archive/2011/07/06/2098771.html
给个链接对照着把gd2开了试试

原本2楼的方法我用后可以显示,但回宿舍再次运行就显示不了,我把代码附上

<?php//checkNum.phpsession_start();$num1=$_SESSION[code];function random($len){$srcstr="ABCDEFGHIJKLMNPQRSTUVWXYZ123456789";mt_srand();$strs="";for($i=0;$i< $len;$i++){$strs.=$srcstr[mt_rand(0,33)];}return strtoupper($strs);}$str=random(4); //随机生成的字符串$width = 50; //验证码图片的宽度$height = 25; //验证码图片的高度@header("Content-Type:image/png");$_SESSION["code"] = $str;//echo $str;$im=imagecreate($width,$height);//背景色$back=imagecolorallocate($im,0xFF,0xFF,0xFF);//模糊点颜色$pix=imagecolorallocate($im,187,230,247);//字体色$font=imagecolorallocate($im,41,163,238);//绘模糊作用的点mt_srand();for($i=0;$i<1000;$i++){imagesetpixel($im,mt_rand(0,$width),mt_rand(0,$height),$pix);}imagestring($im, 5, 7, 5,$str,$font);imagerectangle($im,0,0,$width-1,$height-1,$font);imagepng($im);imagedestroy($im);$_SESSION["code"] = $str;?>
Copy after login

运行时就显示一个正方形,里面一个叉.

将 header("Content-Type:image/png"); 这句注释掉就会出现下面错误:
Notice: Use of undefined constant code - assumed 'code' in...

$num1=$_SESSION[code]; 这句报错了,这句没用吧,可以删掉。

或者你也可以在首行加上 error_reporting(E_ALL & ~E_NOTICE); 来屏蔽notice错误。这样也可以解决。


这个就是我运行的效果
上面那段代码你们可以运行吗?运行后有显示验证码吗?

原本下面这个方法是可以用的
extension=php_gd2.dll 去掉前面的
我代码弄成另一个错误的验证码

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!