【问题标题】:Form doesn't work with captcha表单不适用于验证码
【发布时间】: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 验证码的外观和工作方式,但这里必须是这个验证码(如果我忘记了会话的这个问题,它看起来又好又简单)。

标签: php forms session captcha


【解决方案1】:

嗯,首先你应该检查你是否有 $_POST,因为当你提交帖子时,你会改变会话值

所以你的代码应该看起来像

session_start();
if($_POST) {
    $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{
        $_SESSION['secure'] = rand(1000, 9999); // Change capcha because it is wrong
        $result = "Error. Some field values are too long. Try again.";
    }
} else {
   $_SESSION['secure'] = rand(1000, 9999);
}
// Show the html after

【讨论】:

  • 我理解你。但是您的代码不会改变结果。无论如何,谢谢。
【解决方案2】:

由于您尚未粘贴所有 html,因此我不是 100% 认为您已将表单包装在 &lt;form method="post" action="?"&gt;...&lt;/form&gt; 中。

@Martin Andreev 是正确的,但您需要检查 $_POST 参数,否则它每次都会重新生成 $_SESSION。

session_start();
if(isset($_POST['contact_captcha']) && isset($_SESSION['secure'])) {
    $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."
    }
}

$_SESSION['secure'] = rand(1000, 9999);
//html

如果您没有得到任何结果,则 var_dump() $_POST 和 $_SESSION 如果会话为空,您可能需要查看您的服务器设置和浏览器上的 cookie。

编辑:删除 === 并使其 == 这可能是一个严格的类型问题:

if($_SESSION['secure'] == $contact_captcha){

当您使用 var_dump 时,它们需要具有相同的类型才能使用 ===

即$_POST['contact_captcha'] 是一个字符串?在 var_dump() 上有哪些类型?

var_dump($_SESSION['secure']);
string(1) "1"

var_dump($contact_captcha);
int(1)

【讨论】:

  • $_SESSION['secure'] 和$contact_captcha 是相等的,但是提交后$_SESSION['secure'] 改变了它的值,所以表单永远不会发送。
  • &lt;form class="form-horizontal" role="form" method="post" action="contact.php"&gt; 我有什么。
  • 如果它改变了它的值,那么你需要查看在页面上调用了多少次contact.php文件,打开开发者工具栏,查看调用了多少次contact.php文件.您还可以将生成时间存储在会话中,即 $_SESSION['time'] = time();如果值不同,您就知道它被多次调用。
  • 在contact.php中。
  • 我收到了 1456160935。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-11-10
  • 1970-01-01
  • 2018-10-01
  • 2019-08-29
  • 2018-09-07
  • 2016-01-27
  • 1970-01-01
相关资源
最近更新 更多