首頁 後端開發 php教程 解决php session验证码不现实的有关问题

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

Jun 13, 2016 pm 12:16 PM
function quot rand

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

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

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

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

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

<?php ini_set(&#39;display_errors&#39;, &#39;Off&#39;);	header("Content-type:image/png");	session_start();		$_SESSION[&#39;code&#39;] = &#39;null&#39;;			//初始化	$width = &#39;58&#39;;	$height = &#39;22&#39;;	//$code = &#39;&#39;;	$tmptext =&#39;&#39;;	$bordercolor= &#39;&#39;;	for($i=0;$i<4;$i++)	{		$tmptext = rand(0,9);		$code .= $tmptext;	}	$_SESSION[&#39;code&#39;] = $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(&#39;imagecreate&#39;) && function_exists(&#39;imagecolorset&#39;) && function_exists(&#39;imagecopyresized&#39;)			&& function_exists(&#39;imagecolorallocate&#39;) && function_exists(&#39;imagesetpixel&#39;) 			&& function_exists(&#39;imagechar&#39;)  &&  function_exists(&#39;imagecreatefromgif&#39;)			&& function_exists(&#39;imagepng&#39;)		)	{		$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=&#39;&#39;;		$y=&#39;&#39;;		$text_color = &#39;&#39;;		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();



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

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱門文章標籤

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

如何解決 golang 中的 'undefined: rand.Seed” 錯誤? 如何解決 golang 中的 'undefined: rand.Seed” 錯誤? Jun 25, 2023 am 08:34 AM

如何解決 golang 中的 'undefined: rand.Seed” 錯誤?

function是什麼意思 function是什麼意思 Aug 04, 2023 am 10:33 AM

function是什麼意思

如何在 Golang 平行處理中同步隨機數產生? 如何在 Golang 平行處理中同步隨機數產生? Jun 03, 2024 pm 02:53 PM

如何在 Golang 平行處理中同步隨機數產生?

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决

MySQL.proc表的作用與功能詳解 MySQL.proc表的作用與功能詳解 Mar 16, 2024 am 09:03 AM

MySQL.proc表的作用與功能詳解

'enumerate()'函數在Python中的用途是什麼? 'enumerate()'函數在Python中的用途是什麼? Sep 01, 2023 am 11:29 AM

'enumerate()'函數在Python中的用途是什麼?

php rand函數產生相同隨機數怎麼解決 php rand函數產生相同隨機數怎麼解決 Mar 23, 2023 am 09:17 AM

php rand函數產生相同隨機數怎麼解決

Vue.use函數的用法與作用 Vue.use函數的用法與作用 Jul 24, 2023 pm 06:09 PM

Vue.use函數的用法與作用

See all articles