Wednesday 15 January 2014

php - Setting a cookie before page is refreshed/closed -


I'm asking for a little more community again.

I am modifying a Joomla "quiz" component the way I treat it the way I want it. Now what I am trying to do is to change the "refresh" behavior of the component. Originally, there is a time table in each quiz, i.e., at a time when the quiz for the user is not yet accessible, then when the timer meets 0, the quiz gets credited, but the default behavior of this component It creates that whenever the page is refreshed, the timer will reset to the initial set time (let, 10 minutes)

It is a matter of continuing the timer in a refresh case. I want to be able not.

I have not used PHP 3 or 4 years ago, so I lost a little.

To be able to achieve the desired behavior, I am setting a cookie that sets the initial time (when the quiz was opened) and "now" time store (load it to 0 But I want to "update" the cookie to reload on refresh).

With the initial time and now over time, I can calculate how much time has passed and place where the timer was released.

The thing is, I am trying to "update" the JavaScript to "update" the cookie so that I can manage to get refresh time in the cookie. But for some reason, it is not doing anything. Even when I go to see cookie content, the cookie is set even when it is set.

I know that to update the cookie, I have to remove it and then have to set it up, but I have failed.

This sample code is:

  // This cookie (via PHP) is responsible for setting:  quiz- & gt; time_limit); // $ var = implode (',', $ data); If defined ($ _ cookie [$ name]) {/ *, then the timer * /} and {setcookie ($ name, implode (',', $ data, $ end update) ; } Echo $ _COOKIE [$ name]; Echo "
"; Copy $ this- & gt; Quiz-> Time_limit; ? & Gt;

and find out the "Refresh" event (which will run PHP with Javascript):

  window.onbeforeunload = function () {& Lt; ? Php $ name = "progress"; $ List = explosion ($ _ cookie [$ name]); // Delete cookies cookie ($ name, "", time () - 3600); // Update field $ list ['1'] = time (); // Now - Fresh time $ list ['2'] = $ list ['1'] - $ list ['0']; // time_diff // set cookie set againCookie ($ name, implode (',', $ list), time () + 3600); ? & Gt; }   

Can someone indicate what is wrong with this code?

The comment states that, javascript can not run PHP code like that ... but if If you use AJAX then you can Now with the code we posted, if you load your page in the browser and see the code, then your function will look like this:

  window.onbeforeunload () {}   

So there is no wonder that when you are closing your browser, then nothing is happening with your cookies. Now, without looking at your other works, it really does not Is telling what is happening, but you can get PHP and JavaScript Can be used, but in a different fashion. I explain it with an example.

  window.onbeforeunload () {var name = & lt ;? Php $ name = 'Steve'; Echo ($ name); ? & Gt ;; Console.log (name); }   

If you had this code and the page has been loaded in the browser, you will no longer see the PHP code, instead you should see:

  window .onbeforeunload () {var name = 'Steve'; Console.log (name); }   


With that being said, here are some links that you can find useful
  • < / Li>


No comments:

Post a Comment