我正在尝试在我的页面上创建自己的验证码以便登录,但是这个php代码不会在我的网页上显示我的验证码图像。所以任何建议都会很好。 这是我的captcha.php
<?php
session_start();
// generate random number and store in session
$randomnr = rand(1000, 9999);
$_SESSION['randomnr2'] = md5($randomnr);
//generate image
$im = imagecreatetruecolor(100, 38);
//colors:
$white = imagecolorallocate($im, 255, 255, 255);
$grey = imagecolorallocate($im, 128, 128, 128);
$black = imagecolorallocate($im, 0, 0, 0);
imagefilledrectangle($im, 0, 0, 200, 35, $black);
// ------------- your fontname -------------
// example font http://www.webpagepublicity.com/free-fonts/a/Anklepants.ttf
$font = 'Anklepants.ttf';
//draw text:
imagettftext($im, 35, 0, 22, 24, $grey, $font, $randomnr);
imagettftext($im, 35, 0, 15, 26, $white, $font, $randomnr);
// prevent client side caching
header("Expires: Wed, 1 Jan 2015 00:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
header("Cache-Control: no-store, no-cache, must-revаlidate");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
//send image to browser
header ("Content-type: image/PNG");
imagegif($im);
imagedestroy($im);
?>
和我的html代码显示验证码图像,它是试用代码。
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>books Collection</title>
</head>
<body>
<div >
<form>
<img src="captcha.php" /></br>
<img src="captcha.png" /></br>
<input type="text" name="answer" placeholder="Enter captcha here" />
<input type="submit" value="CHECK" />
</form>
</div>
</body>
</html>
答案 0 :(得分:0)
您的ttf
字体肯定存在问题。尝试使用其他字体文件或字体大小太大或太小。我用另一种字体Akashi.ttf
测试了你的脚本,它运行完美。这是屏幕截图:
答案 1 :(得分:0)
试试这个
<img src="<?= 'captcha.php'; ?>">
或
<img src="<?php echo 'captcha.php'; ?>">