【发布时间】:2016-06-04 01:50:06
【问题描述】:
我创建了发送电子邮件的表单。但是当我使用$_SESSION['secure'] 时它不起作用。只随机化 4 位数字是否安全?
generate.php
<?php
session_start();
header('Content-type: image/jpeg');
$text = $_SESSION['secure'];
$font_size = 20;
$img_width = 110;
$img_height = 40;
$img = imagecreate($img_width, $img_height);
imagecolorallocate($img, 255, 255, 255);
$txt_color = imagecolorallocate($img, 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($img, $x1, $y1, $x2, $y2, $txt_color);
}
imagettftext($img, $font_size, 0, 15, 30, $txt_color, 'NautilusPompiliusRegular.ttf', $text);
imagejpeg($img);
?>
contact.php
<?php
session_start();
$_SESSION['secure'] = rand(1000, 9999);
$to = '***';/*Change it to the site owners email.*/
$subject = 'New customer!';
$contact_name = $_POST['contact_name'];
$contact_email = $_POST['contact_email'];
$contact_message = $_POST['contact_message'];
$contact_captcha = $_POST['contact_captcha'];
$headers = 'From: '. $contact_name . "\r\n" . 'Reply-To: '. $contact_email . "\r\n" . 'X-Mailer: PHP/' . phpversion();
$result = "";
if($_SESSION['secure'] === $contact_captcha){
mail($to, $subject, $contact_message, $headers);
$result = "Your message was successfully sent.";
}else{
$result = "Error. Some field values are too long. Try again.";
}
echo '
<!DOCTYPE html>
<html lang="en">...'
<div class="form-group">
<div class="col-sm-2 control-div"><img src="generate.php" /></div>
<div class="col-sm-10">
<input type="text" class="form-control" name="contact_captcha" placeholder="Enter the value from the img." value="" maxlength="5" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-10 col-sm-offset-2">
<input name="submit" type="submit" value="Send" class="btn btn-primary">
</div>
</div>
</div>';
注意:我没有destroy_session(),generate.php 是单独的文件(我在两个文件中都启动了会话)。当我不使用 if/else 语句时,它可以工作。
【问题讨论】:
-
再试一次。 session secure7393 联系验证码 3467。现在它们不同了。
-
你为什么不使用像
recaptcha这样的服务呢? google.com/recaptcha/intro/index.html -
因为我不懂。它必须保护表单免受机器人的侵害,但我认为如果机器人可以在字段中输入值,那么他们也可以在复选框中输入一个简单的值(只需按下它)。
-
那为什么不用像solvemedia.com/publishers/captcha-type-in这样的系统呢?
-
我喜欢 solvemedias 验证码的外观和工作方式,但这里必须是这个验证码(如果我忘记了会话的这个问题,它看起来又好又简单)。