简体   繁体   English

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

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

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

functions.php函数.php

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

header.php头文件

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

The html form 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>

The ajax function ( /inc/ajax/testimonial.php ) 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();
}

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

It looks like there is some duplicate request (also judging from the provided access.log).看起来有一些重复的请求(也从提供的 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.如果这是请求,一些重写代码(在 WP 或 .htaccess 中的 mod_rewrite 中)重写“失败”请求并将其发送到主脚本。 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寻找本地主机和服务器之间的差异

  • looking for requests by following each link in the generated html code and checking whether they give the expected response通过跟踪生成的 html 代码中的每个链接并检查它们是否给出预期响应来查找请求

Perhaps you could also write some debugging messages to either the error.log or some other logging facility.也许您还可以将一些调试消息写入 error.log 或其他一些日志记录工具。 That might also help finding this.这也可能有助于找到这一点。

Sorry if this doesn't help you any further...抱歉,如果这不能帮助您进一步...

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

相关问题 PHP Excel 导入到 MYSQL 数据库在本地主机上工作正常,但在在线服务器上不能正常工作 - PHP Excel import to MYSQL database working fine on localhost but not on online server 验证码可以在localhost上正常工作,但不能在我的主机上 - captcha works fine in localhost but not in my hosting 本地主机工作正常,但phpmyadmin不是 - localhost working fine but phpmyadmin is not 目标 class controller 不存在 Laravel 8 在 localhost 一切正常但不在线 - Target class controller does not exist Laravel 8 in localhost everthing working fine but not online 在线时CodeIgniter验证码图片不起作用 - CodeIgniter captcha image not working when online 会话在本地主机上工作正常但在服务器上不工作 - Session in working fine in localhost but not at server Phpmailer 在 localhost 中工作正常但不在服务器中 - Phpmailer working fine in localhost But not in server fullcalendar在localhost中工作正常,但在服务器中工作不正常 - fullcalendar is working fine in localhost but not in server PHP curl_exec()无法在线工作但在localhost上工作正常,因为CURLOPT_POSTFIELDS未将数据附加到http post请求 - PHP curl_exec() not working online but works fine on localhost as CURLOPT_POSTFIELDS not attaching data to http post request 标头在localhost上工作,但在在线托管上不工作 - header is working on localhost but not working on online hosting
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM