簡體   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