解决php session验证码不现实的问题

本人一开始图片老显示不出来,显示的是一个裂了的图片,经查询发现可能由以下几种可能造成

1)在header("Content-type:image/png");前加如下一段代码

ini_set('display_errors', 'Off');
本人就是由上面的问题造成。

附上生成验证码的图片的代码

<?php 
	ini_set('display_errors', 'Off');
	header("Content-type:image/png");
	session_start();
	

	$_SESSION['code'] = 'null';			//初始化
	$width = '58';
	$height = '22';
	//$code = '';
	$tmptext ='';
	$bordercolor= '';
	for($i=0;$i<4;$i++)
	{
		$tmptext = rand(0,9);
		$code .= $tmptext;
	}
	$_SESSION['code'] = $code;

	//以下三句诗让浏览器不缓存
	@header("Expires:-1");
	@header("Cache-Control:no-store,private,posc-check=0,pre-check=0,max-age=0",FALSE);
	@header("Pragma:no-cache");
	
	if(function_exists('imagecreate') && function_exists('imagecolorset') && function_exists('imagecopyresized')
			&& function_exists('imagecolorallocate') && function_exists('imagesetpixel') 
			&& function_exists('imagechar')  &&  function_exists('imagecreatefromgif')
			&& function_exists('imagepng')
		)
	{
		$im = imagecreate($width, $height);
		$backgroundcolor = imagecolorallocate($im, 255, 255, 255);
		$numorder = array(1,2,3,4);
		/**shuffle将数组打乱*/
		shuffle($numorder);
		/**array_flip返回一个被反转的数组,键值被处理值*/
		$numorder = array_flip($numorder);
		$x='';
		$y='';
		$text_color = '';
		for($i=1;$i<=4;$i++)
		{
			$x = $numorder[$i] * 13 + mt_rand(0,4) -2;
			$y = mt_rand(0,3);
			$text_color = imagecolorallocate($im, mt_rand(50,255), mt_rand(50,255), mt_rand(50,255));
			/**将字符画在$im图中,5表示字体的大小 $x+5 */
			imagechar($im, 5, $x+5, $y+3, $code[$numorder[$i]], $text_color);

		}
		
		$linenums = mt_rand(10,32);
		for($i=0;$i<=$linenums;$i++)
		{
			$linecolor = imagecolorallocate($im, 255, mt_rand(0,255), mt_rand(0,255));
			$linex = mt_rand(0,255);
			$liney = mt_rand(0,255);
			imageline($im, $linex, $liney, $linex+mt_rand(0,4)-2, $liney+mt_rand(0,4)-2,  $linecolor);
		}
		
		for($i=0;$i<40;$i++)
		{
			$pointcolor = imagecolorallocate($im, mt_rand(0,255), mt_rand(0,255), mt_rand(0,255));
			imagesetpixel($im, mt_rand(0,$width), mt_rand(0,$height), $pointcolor);
		}
		
		$bordercolor = imagecolorallocate($im, 150, 150, 150);
		imagerectangle($im, 0, 0, $width-1, $height-1, $bordercolor);
		
		imagepng($im);
		imagedestroy($im);
	}
?>



2)变量没初始化


3)在header("Content-type:image/png");前加入

ob_clean();



第二三中方法不确保可以解决问题,仅供参考。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值