PHP Solutions for Authentication Codes Not Displayed

PHP Solutions for Authentication Codes Not Displayed:
Step 1: Make sure the code is not written incorrectly!!! (You can test it by simply extracting the code you need)
Step 2: Ensure that PHP painting technology extends the opening of the GD library; at php.ini set: extension=php_gd2.dll
Step 3: Clear the cache: PHP's three functions about the cache (very important):
ob_get_contents() - Returns the contents of the output buffer
ob_flush() - Flushes the contents of the output buffer
ob_clean() - Empty (erase) output buffer
ob_end_flush() - Flush out (send out) output buffer content and close buffer
ob_end_clean() - Empty (erase) the buffer and close the output buffer
flush() - Refresh output buffer

When the captcha picture can't be displayed, during the testing phase (comment header("content-type:image/png")), it can display the information of the picture; when you can't find the reason: most likely is the problem with the cache; try adding ob_clean() to the running code;
Step 4: Restart the Apache server and run it again! There may be some amazing discoveries! Thank you! These are my solutions in the running process, I hope you can learn together!
Attached is the code I wrote for your reference:

//Captcha.class.php file name
<?php 

//Define the final image verification code class
final class Captcha{
	//Private Member Properties
	private $code;//Authentication code string
	private $codelen;//Authentication code length
	private $width;  //Canvas width
	private $height;  //Canvas height
	private $img;   //Image Resources
	private $fontsize; //FontSize
	private $fontfile; //Font File

	//Construction method: object initialization
	public function __construct($codelen=4,$width=100,$height=36,$fontsize=20)
	{	
		$this->codelen   = $codelen;
		$this->width     = $width;
		$this->height    = $height;
		$this->fontsize  = $fontsize;
		$this->fontfile  = ".\Public\Home\Fonts\msyh.ttc";
		$this->code      = $this->createCode();
		$this->img       = $this->createImg();
		$this->createBg();     //Add a background color to the canvas
		$this->createText();  //Write string
		$this->line();       //Increase interference line
		$this->outPut();    //Output Image
	}

	//Private Generate Authentication Code Random String
	private function createCode()
	{
		//Generate a random string array
		$arr_str = array_merge(range('a', 'z'),range('A', 'Z'),range(0,9));
		//Shuffle array
		shuffle($arr_str);
		shuffle($arr_str);
		//Randomly specify number of subscripts from an array
		$arr_index = array_rand($arr_str,$this->codelen);
		//Loop the subscript array to construct a random string
		$str ="";
		foreach ($arr_index as $i) {
			$str .=$arr_str[$i];
		}
		//Save Authentication Code String into sess
		$_SESSION['captcha'] = $str;
		return $str;
	}

	//Privately create an empty canvas
	private function createImg()
	{
		return imagecreatetruecolor($this->width, $this->height);
	}

	//Privately assigned canvas background color
	private function createBg()
	{	
		$bgcolor = imagecolorallocate($this->img, mt_rand(0,255), mt_rand(0,200), mt_rand(100,255));
		//Draw a rectangle with a background
		imagefilledrectangle($this->img, 0, 0,$this->width,$this->height, $bgcolor);
	}

	//Private Write Authentication Code String
	private function createText()
	{	
		//Assign colors to text
		$color = imagecolorallocate($this->img, mt_rand(100,255), mt_rand(0,200), mt_rand(100,200));
		//Write string
		imagettftext($this->img, $this->fontsize, -12, 10, 20, $color, $this->fontfile, $this->code);
		//imagestring($this->img, $this->fontsize, 8, 8, $this->code, $color);
	}
	//Private output image
	private function line()
	{
		//Increase interference line
		for($n=1;$n<$this->codelen;$n++){
			$linecolor = imagecolorallocate($this->img, mt_rand(0,200), mt_rand(0,200), mt_rand(0,200));
			imageline($this->img,mt_rand(0,80), mt_rand(0,30), mt_rand(0,100), mt_rand(0,30), $linecolor);
		}
	}
	//Common Output Image
	private function outPut()
	{	
		//Declare the type of output
		header("content-type:image/png");
		//Output Image
		imagepng($this->img);
		//Destroy Image Resources
		imagedestroy($this->img);
	}

}

I have written in MVC design mode; other code can not be uploaded completely; only part of the code used is provided;

<?php 
//LoginController.class.php file name
//Define the final login controller class
final class LoginController{
//Create Authentication Code Class Object
	public function captcha(){
		ob_clean();// Empty (erase) output buffer
	
		//Create a new Captcha class object; since the Captcha class has only one common construction method; when creating a new object, call all methods passed in the construction method;
			$obj=new Captcha();
	}
}
l<!-- login.htm file name -->
<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>login</title>
</head>
<body>
	<img src="?p=Home&c=Login&a=captcha" onclick="this.src='?p=Home&c=Login&a=captcha&'+Math.random()" style="cursor: pointer;">
<!-- onclick Function Trigger Update Verification Code -->	
</body>
</html>

Tags: PHP Apache

Posted on Tue, 10 Sep 2019 18:26:36 -0700 by metrostars