繁体   English   中英

Captcha 在 localhost 上工作正常,但不能在线

[英]Captcha is working fine on localhost , but not online

我在我的 wordpress 主题中的 ajax 表单上使用 simple-php-captcha( https://github.com/claviska/simple-php-captcha ) 脚本,当它在本地主机上时它工作正常但是当我将它上传到在线主机,验证码不匹配,一切正常,验证码图像加载,会话被创建,但图像中显示的验证码与提交表单时不同。

函数.php

require_once( get_template_directory() . '/libs/captcha/simple-php-captcha.php' );
require_once( get_template_directory() . '/inc/ajax/testimonial.php' );

头文件

session_start();
$_SESSION['captcha'] = simple_php_captcha(); 

html表单

<form action="<?php echo admin_url("admin-ajax.php"); ?>" class="dw-ajax-form dw-form" method="post" id="send_testimonial">
    <input type="text" name="name" placeholder="name">
    <input type="text" name="job" placeholder="company / job">
    <input type="text" name="email" placeholder="Email address">
    <textarea type="textarea" name="comment" placeholder="your opinion about us"></textarea>

    <div class="block captcha-image">
        <img src="<?php echo $_SESSION['captcha']["image_src"]; ?>" alt="<?php echo $_SESSION['captcha']["code"]; ?>">
    </div>

    <input type="text" name="captcha" placeholder="enter the code above" autocomplete="off">

    <input type="hidden" name="action" value="send_testimonial">
    <?php wp_nonce_field( 'send_testimonial', 'send_testimonial_nonce' ); ?>

    <input type="submit" value="send"> <span class="msg" style="margin-right:15px;"></span>
</form>

ajax 函数( /inc/ajax/testimonial.php )

<?php
/**
 * Testimonial Form Ajax Callbacks
 *
 * @package Wordpress
 * @subpackage Learnfiles-shop Theme
 * @author Dornaweb.com
 */

add_action( 'wp_ajax_send_testimonial', 'dw_send_testimonial' );
add_action( 'wp_ajax_nopriv_send_testimonial', 'dw_send_testimonial' );
function dw_send_testimonial() {
    global $wpdb;
    $message = '';

    $name           = strip_tags( htmlspecialchars( $_POST["name"] ) );
    $job            = strip_tags( htmlspecialchars( $_POST["job"] ) );
    $email          = strip_tags( htmlspecialchars( $_POST["email"] ) );
    $comment        = strip_tags( htmlspecialchars( $_POST["comment"] ) );

    /* captcha */
    $captcha_input  = strtolower( strip_tags( htmlspecialchars( $_POST["captcha"] ) ) );
    $captcha_code = strtolower( $_SESSION['captcha']['code'] );

    /** Validation **/
    if( !$_SESSION['captcha'] || !is_array( $_SESSION['captcha'] ) )
        die( '<span class="error">Somethings wrong</span>' );

    /******************************* IT ALWAYS GIVES ME THIS ERROR WHEN ONLINE , BUT IT WORKS ON LOCALHOST( i also tried it with "!=" operator ) **************/
    if( $captcha_code !== $captcha_input )
        die( '<span class="error">The entered code doesnt match</span>' );
    /**********************************************************************************************************************************************************/

    if (  !isset( $_POST['send_testimonial_nonce'] ) || ! wp_verify_nonce( $_POST['send_testimonial_nonce'], 'send_testimonial' ) )
        die('<span class="error">Somethings wrong</span>');

    if( empty( $comment ) )
        die('<span class="error">Please enter your comment</span>');

    if( empty( $name ) )
        die('<span class="error">please enter your name</span>');

    if( !empty( $email ) && !filter_var($email, FILTER_VALIDATE_EMAIL) )
        die('<span class="error">the entered email doesnt look like an email address</span>');

    if( empty( $name ) && empty( $comment ) )
        die('<span class="error">please fill the form</span>');

    /* send testimonial */
    $testimonial = array(
        'post_title'  => $name,
        'post_status' => 'pending',
        'post_type'   => 'testimonials',
        'post_author' => 1
    );

    $post_id = wp_insert_post( $testimonial );

    update_field( 'job', $job, $post_id );
    update_field( 'email', $email, $post_id );
    update_field( 'comment', $comment, $post_id );

    // form is valid
    if( empty( $message ) )
        $message = '<span class="success">Your comment submitted! thank you.</span>';

    echo $message;
    wp_die();
}

编辑:我在这里运行了一个测试(抱歉页面是波斯语): http ://test.dornaweb.ir/,页面中间有一个表单,当你点击它时,它会显示一个var_dump() of $_SESSION['captcha'] ,如你所见,图中显示的代码与var_dump data中的代码不同,就像表单提交时$_SESSION领先一步之类的,奇怪的是,当我在本地主机上使用完全相同的主题时,没有任何问题!!

看起来有一些重复的请求(也从提供的 access.log 判断)。 这可能是由服务器上丢失/无法访问的文件引起的(在本地主机上存在/可访问,因此不会在那里引起问题)。 如果这是请求,一些重写代码(在 WP 或 .htaccess 中的 mod_rewrite 中)重写“失败”请求并将其发送到主脚本。 然后,在主脚本中,会话数据被新的验证码覆盖......

这些问题有时很难发现。 开始于:

  • 寻找本地主机和服务器之间的差异

  • 通过跟踪生成的 html 代码中的每个链接并检查它们是否给出预期响应来查找请求

也许您还可以将一些调试消息写入 error.log 或其他一些日志记录工具。 这也可能有助于找到这一点。

抱歉,如果这不能帮助您进一步...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM