plugins – Strange admin-ajax / CSS / $_SESSION issue

Question

TLDR: I’m generating a random token on a front-end form and storing it in $_SESSION to later retrieve by the ajax script.

THE ISSUE: The code retrieved by the ajax script does not match the one generated by the form. It seems the token script is run 2x although it’s only called once and returns the same code each time on a ajax call. The ajax-side code only changes when the page is refreshed.

  • ABC – The token code on the form (ok)
  • ABC – The token code pulled from the $_Session variable on front (ok)
  • 123 – The code returned form $_Session by the ajax function

THE STRANGE PART: After a ton of digging I found that removing the background-image URL string in a CSS file, in a separate plugin resolves this issue.

CSS Code from the other plugin

#pnsmisc_button{
background-image:url('img/ico-pnsmisc-mail.png');
} 

Loaded using:

wp_enqueue_style( 'pnsmisc', $pluginsurl. '/xxxx/style.css' );

TOKEN GEN FUNCTION

function pnsnote_tokenegn() { 
$tokenraw= md5(rand(0,99));
$_SESSION['pntoken'] = $pnsnotetokenraw;
}
add_action('wp_footer', 'pnsnote_tokenegn');

AJAX JS

$( "#goform" ).click(function() {   
var note = $('#pnsnotetxt').val();
      $.ajax({
          url: ajaxurl, 
          data: {
              'action':'pnsnote_request',
              'note' : note,
              'token' : tokennote
          },
          success:function(data) {
             alert(data);
          },
          error: function(errorThrown){
         
          }
      });
    });
});

PHP AJAX

function pnsnote_request() {
if(isset($_REQUEST)){
$session_token = $_SESSION['pntoken'];
echo $session_token;
die();
}
}

0
Elsnare 3 months 2022-04-17T05:03:09-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse