【发布时间】:2018-03-14 22:07:52
【问题描述】:
我试图通过以下网站创建一个简单的 PHP 验证码:
http://php.detailsinn.com/create-captcha-in-your-php-application/
我完全按照教程所教的内容进行操作,但最终验证码图像仍然没有显示。
我检查了 php 错误日志文件并使用 error_reporting 检查错误是什么,但没有显示错误。这是脚本需要一些帮助。
myform.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>
captcha.php
<?php
error_reporting(E_ALL);
// Generate Random Number
$random_number = rand(1,9).rand(1,9).rand(1,9);
$_SESSION['captcha_text'] = $random_number;
// Insert random number into image
$captcha_image = imagecreatefromjpeg("captcha.jpg");
$font_color = imagecolorallocate($captcha_image, 0, 0, 0);
imagestring($captcha_image, 5, 20, 5, $random_number, $font_color);
imagejpeg($captcha_image);
imagedestroy($captcha_image);
?>
【问题讨论】: