How does check_ajax_referer () work?

Smart Wordpress users say plugin developers should use nonce in every AJAX request that is sent from the page back to the wordpress blog (admin -ajax.php).

This is done by (1) creating nonce on the server side, through

$nonce = wp_create_nonce  ('my-nonce');

... (2) makes this nonce available to Javascript code that sends AJAX requests. For example, you can do this as follows:

function myplg_emit_scriptblock() {
    $nonce = wp_create_nonce('myplg-nonce');
    echo "<script type='text/javascript'>\n" .
        " var WpPlgSettings = {\n" .
        "   ajaxurl : '" . admin_url( 'admin-ajax.php' ) . "',\n" .
        "   nonce : '" . $nonce . "'\n" .
        " };\n" .
        "</script>\n";
}
add_action('wp_print_scripts','myplg_emit_scriptblock');

... and then (3) the jjavascript ajax logic refers to this global variable.

    var url = WpPlgSettings.ajaxurl +
        "?action=my-wp-plg-action&" +
        "nonce=" + WpPlgSettings .nonce +
        "docid=" + id;

    $.ajax({type: "GET",
            url: url,
            headers : { "Accept" : 'application/json' },
            dataType: "json",
            cache: false,
            error: function (xhr, textStatus, errorThrown) {
                ...
            },
            success: function (data, textStatus, xhr) {
                ...
            }
           });

... and finally (4) checking the received nonce in the server-side logic.

add_action( 'wp_ajax_nopriv_skydrv-hotlink', 'myplg_handle_ajax_request' );
add_action( 'wp_ajax_skydrv-hotlink', 'myplg_handle_ajax_request' );
function myplg_handle_ajax_request() {
    check_ajax_referer( 'myplg-nonce', 'nonce' );  // <<=-----
    if (isset($_GET['docid'])) {
        $docid = $_GET['docid'];
        $response = myplg_generate_the_response($docid);
        header( "Content-Type: application/json" );
        echo json_encode( $response ) ;
    }
    else {
        $response = array("error" => "you must specify a docid parameter.");
        echo json_encode( $response ) ;
    }

    exit;
}

But how does verification work?

+2
source share
2 answers

AJAX, . :

function check_ajax_referer( $action = -1, $query_arg = false, $die = true ) {
    if ( $query_arg )
        $nonce = $_REQUEST[$query_arg];
    else
        $nonce = isset($_REQUEST['_ajax_nonce']) ? $_REQUEST['_ajax_nonce'] : $_REQUEST['_wpnonce'];

    $result = wp_verify_nonce( $nonce, $action );

    if ( $die && false == $result ) {
        if ( defined( 'DOING_AJAX' ) && DOING_AJAX )
            wp_die( -1 );
        else
            die( '-1' );
    }

    do_action('check_ajax_referer', $action, $result);

    return $result;
}

wp_verify_nonce - false, false $die, wp_die( -1 );.


check_ajax_referer() -1 AJAX. , $die $do_check:

$do_check = check_ajax_referer( 'myplg-nonce', 'nonce', false ); 

, AJAX WordPress: register, enqueue localize JavaScript, wp_enqueue_scripts wp_print_scripts.
. wp_enqueue_scripts() wp_print_styles().

+13

, "nonce" , , . , php .

" , , die ('- 1'), .

0

All Articles