【问题标题】:Verifying simple-php-captcha() input验证 simple-php-captcha() 输入
【发布时间】:2018-07-20 10:31:18
【问题描述】:

POST 表单工作正常,所有数据都正确提交,$captcha_code 返回正确的输出,$captcha 也是如此。但是,当我检查 $captcha == $captcha_code 是否总是返回失败。有人能解释一下为什么会这样吗?

include 'inc/simple-php-captcha/simple-php-captcha.php';
$_SESSION['captcha'] = simple_php_captcha();
$captcha_code = $_SESSION['captcha']['code'];

if(isset($_POST['register'])) {
  $username = $_POST['username'];
  $email = $_POST['email'];
  $password = $_POST['password'];
  $cpassword = $_POST['cpassword'];
  $captcha = $_POST['captcha'];

  if($captcha == $captcha_code) {
    echo 'captcha success';
  } else {
    echo 'captcha failure';
  }
}

【问题讨论】:

  • 可以使用echo var_dump($captcha." = ".$captcha_code);调试变量
  • 你从这个$_SESSION['captcha'] = simple_php_captcha();生成了一个新的验证码你必须从最后一页获取最后一个验证码,而不是再次生成
  • 你的最后一页是什么?表单页面,请张贴

标签: php variables captcha verification


【解决方案1】:

你需要先在 login.php 中生成验证码

login-form.php

<?php
// start session and generate captcha and it's image
session_start();
include 'inc/simple-php-captcha/simple-php-captcha.php';
$_SESSION['captcha'] = simple_php_captcha();

// render the form
?>

<input type="text" name="email">
<input type="text" name="password">
..
..
<input type="text" name="captcha">
<img src="<?php $_SESSION['captcha']['image_src']; ?>">
<input type="submit">

login-submit.php

$captcha_code = $_SESSION['captcha']['code']; //retrive what code was generated before

if(isset($_POST['register'])) {
  $username = $_POST['username'];
  $email = $_POST['email'];
  $password = $_POST['password'];
  $cpassword = $_POST['cpassword'];
  $captcha = $_POST['captcha'];

  if($captcha == $captcha_code) {    // try matching
    echo 'captcha success';
  } else {
    echo 'capture failure';
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-01-18
    • 2020-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多