Why different php session value in Chrome and FireFox?

Viewed 105

set a session value to 10 by ajax. In Chrome it will set back to ZERO. Why?? my.php

<?php
session_start();
$_SESSION["temp"] = 0; ?>


<div id="t">$_SESSION["temp"] = <?=$_SESSION["temp"]?> <br></div>
<br><input type="button" value="Plus 5" onclick="javascript:plusn(5);">
<script>
    function plusn(n) {
          $.ajax({
              url: 'myajax.php?n='+n,
              type: 'get',
              async: false,
              success:function(result){
                  $("#t").append(result)
              }
          });
    }
    
    $(document).ready(function() {
        plusn(10); //initial add 10
    })
</script>

myajax.php

<?php
session_start();
$_SESSION["temp"] += $_GET["n"];
exit('$_SESSION["temp"] = '.$_SESSION["temp"].'<br>');
?>

place here: https://cmbody.com/my.php Please use different browsers to check it. Chrome, Edge and Android cell phones get the wrong result; Firefox and iPhone get the correct results. P.S. same condition using COOKIE

2 Answers

Here's the culprit:

<link rel="shortcut icon" href="#" />

The browser tries to load the favicon from the same URL, as instructed. For that, it performs an additional request to the main PHP script. The differences happen because Firefox uses the cached page and Chrome-based browsers fetch a fresh copy:

Firefox

Chrome

It works correctly when I send it with a 1000 millisecond delay but this is not a correct solution I will continue to investigate.

setTimeout(()=>{
        plusn(10);
    },1000)
Related