Intereting Posts

Изображение php captcha не отображается

Я пытаюсь сделать простой PHP-скрипт для captcha, но я не знаю, почему он не показывает изображение captcha.

вот сценарий: `

<?php session_start(); header("Content-Type: image/png"); $im = imagecreate(110, 20) or die("Cannot Initialize new GD image stream"); $background_color = imagecolorallocate($im, 0, 0, 0); $text_color = imagecolorallocate($im, 233, 14, 91); $possible_no="abcdefghjkmnpqrstuvwxyz23456789,@$"; $len=strlen($possible_no); $random=mt_rand(0,$len); $i=0; while($i<=6){ $captcha .=substr($possible_no, mt_rand(0, strlen($possible_no)-1), 1); $i++; } $a="$captcha"; imagestring($im, 1, 5, 5,"$a", $text_color); imagepng($im); imagedestroy($im); $_SESSION['captcha']=$a; ?>` 

заранее спасибо 🙂

Создайте новый файл (security_image.php) и поместите его в него: возможно, вы захотите, чтобы он отображался по-другому, но вам придется адаптировать то, что вы хотите в нем.

 session_start(); function generate_code(){ $length = '6'; $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '2', '3', '4', '5', '6', '7', '8', '9', ',', '@', '$'); $code = ''; for ($i=0; $i < $length; $i++){ $code .= $chars[rand(0, count($chars)-1)]; } $_SESSION['captcha'] = $code; return $code; } function security_image(){ $code = isset($_SESSION['captcha']) ? $_SESSION['captcha'] : generate_code(); $font = 'content/fonts/comic.ttf'; $width = '110'; $height = '20'; $font_size = $height * 0.75; $image = @imagecreate($width, $height) or die('GD not installed'); $background_color = imagecolorallocate($image, 0, 0, 0); $text_color = imagecolorallocate($image, 233, 14, 91); $textbox = imagettfbbox($font_size, 0, $font, $code); $x = ($width - $textbox[4]) / 2; $y = ($height - $textbox[5]) / 2; imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code); header('Content-Type: image/png'); imagepng($image); imagedestroy($image); } security_image(); 

Затем в вашем изображении html tag поставьте это:

 <img src="security_image.php" alt="security code" /> 

После того, как вы выполнили свой процесс после этого, установите:

 unset($_SESSION['captcha']); // To reset the captcha 

Надеюсь, поможет. Вы должны действительно сделать так, чтобы он восстанавливал все время, которое вы могли бы сделать, изменив $code var var в функции security_image (), чтобы сделать это.