Captcha在localhost上可以正常工作,但不能在线运行 [英] Captcha is working fine on localhost , but not online

查看:157
本文介绍了Captcha在localhost上可以正常工作,但不能在线运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using simple-php-captcha( https://github.com/claviska/simple-php-captcha ) script on an ajax form in my wordpress theme, it works fine when it's on localhost but when i upload it on an online host , the captcha codes don't match , every thing works fine , captcha image loads , sessions get created but the captcha code displayed in the image is not the same as when the form is submitted.

functions.php

functions.php

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

header.php

header.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)

The ajax function ( /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() $ _ SESSION ['captcha'] 如您所见,图像中显示的代码与 var_dump 数据中的代码不同,这就像表单提交$ _SESSION时向前迈出了一步。像那样,奇怪的是,当我在本地主机上使用完全相同的主题时,一切正常!

edited : I've run a test here ( sorry the page is in farsi ) : http://test.dornaweb.ir/ , there is a form in the middle of the page that when you click it , it shows you a var_dump() of $_SESSION['captcha'] , as you can see , the code shown in the image is different is from the code in var_dump data , it's like when the form submits the $_SESSION is one step ahead or something like that , the weird thing is when i use the exact same theme on localhost nothing goes wrong!!

推荐答案

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

It looks like there is some duplicate request (also judging from the provided access.log). This might be caused by a missing/inaccessible file on the server (which is there/accessible on localhost, thus not causing problems there). If this is requested, some rewrite-code (either within WP or mod_rewrite in .htaccess) rewrites the "failing" request and sends it to the main script. Then, in the main script, the session data gets overwritten with a new captcha...

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

These issues are sometimes hard to spot. Start at:


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

  • looking for differences between localhost and server

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

looking for requests by following each link in the generated html code and checking whether they give the expected response

也许您还可以将一些调试消息写入error.log或其他日志记录工具。

Perhaps you could also write some debugging messages to either the error.log or some other logging facility. That might also help finding this.

对不起,如果这对您没有帮助...

Sorry if this doesn't help you any further...

这篇关于Captcha在localhost上可以正常工作,但不能在线运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆