我试图通过以下网站创建一个简单的PHP验证码:
http://php.detailsinn.com/create-captcha-in-your-php-application/
我正在完成教授的教程,但最终Captcha图像无论如何都没有显示。
我检查了php错误日志文件,并使用error_reporting检查错误是什么,但没有显示错误。这是脚本需要一些帮助。
myform.php
<script src="https://unpkg.com/text-encoding@0.6.4/lib/encoding-indexes.js"></script>
<script src="https://unpkg.com/text-encoding@0.6.4/lib/encoding.js"></script>
captcha.php
<?php session_start();
error_reporting(E_ALL);
if(isset($_POST['txtName'])){
$error = '';
if(isset($_POST['txtCaptcha']) and $_POST['txtCaptcha'] !=''){
if($_SESSION['captcha_text'] == $_POST['txtCaptcha']){
// Action of form...
echo $_POST['txtName'].' is successfully processed';
exit;
}else{
$error = '<font color="red">Sorry Incorrect captcha entered...</font>';
}
}else{
$error = '<font color="red">You have not entered captcha.</font>';
}
}
?>
<html>
<head><title>Captcha</title></head>
<body>
<?php if(isset($error)){ echo $error; } ?>
<form action="myform.php" method="post">
<input type="text" name="txtName" value="" placeholder="Enter your name" />
<br /><br />
<img src="captcha.php" /> <input type="text" name="txtCaptcha" value="" placeholder="Enter the number you see in the image" />
<br /><br/>
<input type="submit" value="Submit Data" />
</form>
</body>
</html>
答案 0 :(得分:0)
当我们即时绘制图像时,我们必须告诉浏览器我们发送给它的内容,在您的情况下
header ("Content-type: image/jpeg");
它变成了:
....
header ("Content-type: image/jpeg");
imagejpeg($captcha_image);
imagedestroy($captcha_image);
您也可以验证状态
/* Attempt to open */
$captcha_image = imagecreatefromjpeg("captcha.jpg");
/* See if it ok */
if($captcha_image)
{
}else{
// failed to open
$captcha_image = imagecreatetruecolor(150, 30);
}