我刚刚开始学习php,尝试使用它为联系表单创建验证脚本,但即使用户输入了正确的数字,它也总是返回错误:
<?php
session_start ();
$_SESSION['safe']=rand(1000,9999);
if(isset($_POST['nazwa']) && isset ($_POST['dane']) &&isset($_POST['message']))
{
$name = $_POST['nazwa'];
$email = $_POST['dane'];
$message = $_POST['message'];
if (!empty($name) && !empty($email) && !empty($message)) {
if(strlen ($name)>50||strlen($email)>50||strlen($message)>1000) {echo 'Character limit exceeded '; } else
{
$from = 'z formularza nazwastrony';
$to = 'roman.pliszka@yahoo.com';
$subject = 'wiadomosc ze strony';
$body = "From: $name\n E-Mail: $email\n Message:\n $message";
if (!isset($_POST['check'])) {
$_SESSION['safe']=rand(1000,9999);
} else {
if ($_SESSION['safe'] == $_POST['check']) {
if ($_POST['submit'])
{
if (@mail ($to, $subject, $body, $from))
{
echo '<p>Message sent</p>';
} else
{
echo '<p>An error occurred</p>';
}
}
}else {
echo 'Incorrect number';
$_SESSION['safe']=rand(1000,9999);
}
}
}
} else {
echo 'Please fill all of the fields';
}
}
?>
该脚本使用generate.php生成图像:
<?php
session_start();
header('Content-type: image/jpeg');
$text = $_SESSION['safe'];
$font_size = 30;
$image_width = 100;
$image_height = 40;
$image = imagecreate ($image_width, $image_height);
imagecolorallocate($image, 255, 255, 255);
$text_color = imagecolorallocate($image, 0, 0, 0);
for ($x=1; $x<50; $x++) {
$x1 = rand(1, 100);
$y1 = rand(1, 100);
$x2 = rand(1, 100);
$y2 = rand(1, 100);
imageline($image, $x1, $y1, $x2, $y2, $text_color);
}
imagettftext($image, $font_size, 0, 15, 30, $text_color, 'font.ttf', $text);
imagejpeg($image);
?>
基本上,在所有情况下,我得到&#34;不正确的数字&#34;。我的猜测是php会话更改生成另一个随机数,但我无法正确编写此代码。脚本和html表单中的所有名称都是相同的。我会感激任何帮助。
编辑:这是一个包含用户输入的表单片段:
<image src="generate.php" />
<input type="text" name="check">
答案 0 :(得分:1)
显而易见的问题是你的陈述的顺序。您应该在会话中生成新的随机代码之前处理POST数据。否则,您始终创建的新代码不是用户发送的代码。我会稍微改变一下:
<?php
session_start();
// PROCESS POST DATA - RETURN CORRESPONDING MESSAGE
function processPostData() {
$code="";
// CHECK IF POST DATA WAS SENT
if (isset($_POST["check"])) {
$code=$_POST["check"];
} else {
return false;
}
// CHECK CODE
if ($code != $_SESSION["safe"]) {
return "<p>Incorrect number</p>";
}
// CHECK NON-EMPTY
if (empty($_POST['nazwa']) || empty($_POST['dane']) || empty($_POST['message'])) {
return "<p>Please fill all of the fields</p>";
} else{
// GET POST DATA
$name = $_POST['nazwa'];
$email = $_POST['dane'];
$message = $_POST['message'];
}
// CHECK MAX LENGTH
if(strlen ($name)>50||strlen($email)>50||strlen($message)>1000) {
return "<p>Character limit exceeded</p>";
}
/* VALIDATE DATA HERE: email (look for good validation scripts) */
// SEND EMAIL
if (@mail ($to, $subject, $body, $from)) {
return "<p>Message sent</p>";
} else {
return "<p>An error occurred</p>";
}
}
// PROCESS POST DATA - IF SET
echo processPostData();
// GENERATE NEW CODE EVERY TIME -! AFTER PROCESSING POST DATA
$_SESSION["safe"]=rand(1000,9999);
?>
PS。问题出现在你的代码的第3行 - 最后移动它应该有效。不过,我认为使用合适的API会更安全。我使用:Google's reCaptcha
答案 1 :(得分:0)
我无法看到表单的代码,但似乎您使用了错误的变量来在验证码上生成文本。尝试替换
$text = $_SESSION['bezpieczny'];
与
$text = $_SESSION['safe'];