0

I'm pretty sure this question will be marked as not clear at the minimum. But 'i'll try due to the strange behavior I've seen on tests, so i'm asking to skilled guys.

I try to write a plugin for Wordpress that on the PHP side set up a token, that is so sent out as cookie value, and as js var on page header. All is working fine, when after the js flow/code execute an XHR request to an external PHP file to execute some tasks, one is to check against the value of the cookie, and the passed post var via JS XHR needed to control the access at some features.

Since after the response, i need to push some result into a Wordpress widget, i've start to add in the html of this widget, some html line like this:

<div class="myclass">
<img src="/img/button.svg" onerror="this.onerror=null; this.src=\'/img/button64.png\'">
</div>

the problem: i've note that IF the button.svg file not exist, the token cookie value and the passed js var mismatch when page has been loaded, while all work fine, if the src url point to a correct file image that exist.

In theory, the fact that an img src URL isn't correct, should not interfere with the fact that a cookie is setup via php with a value and a js var with same value: the HTML code is part of the widget where the js code is executed, but I can't find any relation with the explained behavior, because the outputted html code still have nothing to do with the js/php code in any way.

Anyone know, how this can happen and why? (admitted that you have understood the problem as I've explained it!)

RESUME AND FOLLOW:

Since last night i've follow coding the plugin, i would like to resume the situation in few words and report another behavior (the same in different element): so i have a php code, that setup a cookie, then a JS var that output on header (by php): all ok, values on cookie and JS var on header are the same when page load, until i add:

<video id="w3vtp_WVO" controls preload="auto" width="640" height="264" poster="animageThatNotExist.png" data-setup="{}">
    <source id="w3vtp_WVS" src="http://localhost/wp49/myfolder/mymedia.webm" type="video/webm">
    <p class="vtp-no-js">
      To view this video please enable JavaScript, and consider upgrading to a web browser that supports HTML5 video
    </p>
  </video>

IF the URL of attribute poster not point to an existing image, then the cookie value and the JS var on header when page load, mismatch. I've check to log the flow, and values are exactly the same on exit the code when these values are setup in php: i've not follow further checks, so no light in my mind about this behavior until now. When the cookie OR the JS var are overwritten, OR the value of one or other is not set? I will check as i can.

Anybody have any idea?

this the code, wrapped into a function (+-) that is fired inside an hook just like this:

 add_action('init', 'w3all_vtp');

function w3all_vtp() { .... ... ..... if (isset($_COOKIE[ LOGGED_IN_COOKIE ])){ // a cookie should exist in WP for a logged in user, so this maybe isn't really needed
      $ps = explode("|", $_COOKIE[ LOGGED_IN_COOKIE ]);
      if( !$ps OR empty($ps) OR preg_match('/[^_\-0-9A-Za-z]/',$ps[3]) ){
         //return;
        echo 'bad cookie'; exit;
      }
      $ck = str_shuffle( substr($ps[3], 0, 9) . md5(microtime()) . md5(substr(NONCE_KEY, 0, 12)) . $current_user->ID );
      $cv_cn = $ck . '|' . LOGGED_IN_COOKIE . '|' . $current_user->ID . '|' . $user_where; // to js: 0 admin // 1 front end // 2 elsewhere
       setcookie( "w3allCKvtp", $cv_cn, 0, '/', COOKIE_DOMAIN, is_ssl(), true );
    // echo $cv_cn . '<br />'.$ck;//exit;
       $umeta = array("w3allCKvtp" => $ck, array("vtp_udata" => array( "last_exec_time" => time(), "exec_times" => 'set the value here' ))); // last_exec_time should be decreased if the curl fail, to reset the count
      update_user_meta( $current_user->ID, 'w3all_vtp', $umeta );
       $w3allWLoc = $user_where == 0 ? $w3allWLoc = '..' : 1;
       define("W3ALLULOC", $w3allWLoc);
       $pjV = "<script type=\"text/javascript\">var w3allCKvtp = '".$ck."'; var w3allWLocvtp = '".$w3allWLoc."'; var w3allUWherevtp = '".$user_where."'; var w3allPURLvtp = '".plugins_url(). "/w3all-plugin/"."'; var w3allPATHvtp = '".WP_PLUGIN_DIR."';</script>"; // global js var, output just after latest meta on header, and before any other enqueued script
       $pjV = serialize($pjV);
       define("W3ALLJVARS", $pjV);
    } }
7
  • how do you set your cookie normally? If the image is on the same url as your code it will have cookies as well so maybe something interferes when 404 is thrown. – Kasia Gogolek Mar 7 '18 at 10:56
  • the cookie value and js var related, are setup by php with setcookie, and assigning to a php var the js code to output the var value on head: the token mismatch when i load the page, even before to send out the xhr request in true, so i will go to edit the above question. The fact remain that if the src image attribute not point to a real image, then the cookie setting mismatch with the one that output on header as js var. – axew3 Mar 7 '18 at 11:31
  • @Kasia Gogolek i have update the above question where: RESUME AND FOLLOW point, in the first main post: maybe now is more clear. – axew3 Mar 9 '18 at 8:38
  • something interferes when 404 is thrown ... i suppose you're right, but how it can interfere remain obscure to me at moment. – axew3 Mar 9 '18 at 9:17
  • not sure, it would help if you shown the snippet that sets the cookie :) – Kasia Gogolek Mar 9 '18 at 9:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.