PHP code example of wberredo / nonce

1. Go to this page and download the library: Download wberredo/nonce library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

wberredo / nonce example snippets


// set lifetime for 4 hours
Nonce_Config::set_nonce_lifetime( 4 * HOUR_IN_SECONDS );

// set message showed when showAys is called
Nonce_Config::set_error_message( "Are you sure" );

$nonce_gen = new Nonce_Generator( "default-action" );
$nonce = $nonce_gen->generate_nonce();

// you can also set parameters with set functions
$nonce_gen = new Nonce_Generator();
$complete_url = $nonce_gen
                    ->set_url( "http://github.com/WBerredo" )
                    ->set_action( "default_action" )
                    ->generate_nonce_url();

$nonce_gen = new Nonce_Generator();
$nonceField = $nonce_gen
                    ->set_action( "default_action" )
                    ->generate_nonce_field( "nonce", "referer", "do_not_echo" );
                    
// to print the nonce field you have to set the last param as true
$nonce_gen
    ->generate_nonce_field( "nonce", "referer", "echo" );

Nonce_Generator::show_ays( 'action' );

if ( Nonce_Verifier::verify( $nonce, $defaultAction ) ) {
// if is valid
} else {
// if is not valid
}

if ( Nonce_Verifier::verify_url( $complete_url, $defaultAction ) ) { 
// if is valid
} else {
// if is not valid
}

if ( Nonce_Verifier::verify_admin_referer( $defaultAction ) ) {
// if is valid
} else {
// if is not valid
}

if ( Nonce_Verifier::verify_ajax_referer( $defaultAction ) ) {
// if is valid
} else {
// if is not valid
}

  /**
  * The path to the WordPress tests checkout.
  */
  define( 'WP_TESTS_DIR', '/home/berredo/Documents/repository/wordpress/wordpress-develop/tests/phpunit/' );