A 10 hour period for cache purging and nonce lifespan
-
Hello,
I was using Disk: Enhanced as the method for the page caching, and after around 24 hours I was getting nonce invalid issues. I cleared the cache and it returned to normal.
I’m assuming there might have been an issue where the cached page had expired nonces.
To help at this not happening I’ve placed this code inside a custom plugin of mine:// Schedule the cache purge to run every 10 hours add_action( 'wp', function() { if ( ! wp_next_scheduled( 'my_purge_w3tc_cache' ) ) { wp_schedule_event( time(), 'every_10_hours', 'my_purge_w3tc_cache' ); } } ); // Define the "every_10_hours" time interval add_filter( 'cron_schedules', function( $schedules ) { $schedules['every_10_hours'] = array( 'interval' => 36000, // 10 hours in seconds 'display' => __( 'Every 10 hours' ), ); return $schedules; } ); function my_purge_w3tc_cache() { if ( function_exists( 'w3tc_flush_all' ) ) { w3tc_flush_all(); } }
The code creates a cron job to purge the cache every 10 hours.
My question is, even using the regular cache lifespan built in the plugin, isn’t invalid nonces a matter of probability.
Lets say a nonce lasts 24 hours, with my 10 hour interval this scenario could occur:
Nonce |————————|————————|————————|
Purge |———-|———-|———-|———-|———-|———-|———-|
After the third purge, the cache will be left with the first generated nonce, but after less than 4 hours the nonce will no longer be valid and the cache will remain with the invalid nonce for another 6 hours or more.
What is the solution to this?
EDIT: Another option would be to purge the page and reload in case the user failed a nonce:function wp_verify_nonce_failed_clean_cache($nonce, $action, $user, $token){ global $post; $current_page_id = $post->ID; if($current_page_id && !$user->ID){ w3tc_flush_post( $current_page_id ); wp_redirect( get_permalink() ); exit; } } add_action( 'wp_verify_nonce_failed', 'wp_verify_nonce_failed_clean_cache', 10, 4 );
Thank you.
- The topic ‘A 10 hour period for cache purging and nonce lifespan’ is closed to new replies.