简体   繁体   中英

Reload page if tab reopened

How do I reload the page if the tab is closed and reopened in browser ?

<?php
    $sql = "SELECT x FROM users WHERE id='1' LIMIT 1";
    $query = mysqli_query($connect, $sql);
    $row = mysqli_fetch_row($query);
    $x = $row[0];

    // value for x here is 50

    $sql = "UPDATE users SET x='100' WHERE id='1'";
    $query = mysqli_query($connect, $sql);
?>
<!doctype html>
<html>
    <head>
        <title>Page Title</title>
    </head>
    <body>
        <p>Page content</p>
        <script>
            alert('<?php echo $x; ?>');
        </script>
    </body>
</html>

First the alert says 50

Then I close the tab pressing Ctrl + W

Then when I reopen the tab pressing Ctrl + Shift + T , the alert says 50 again instead of 100

What do I do to get the updated value?

Normally if you reopen the same page within a very short time the web page will be loaded from the cache. Try to clear the cache and reload.

If you want your site to always load from the server without using the cache. You can add the following in your php script. But do not use this unless this is really necessary since the caching mechanism are in place to improve the user experience.

<?php
header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0");
header("Cache-Control: post-check=0, pre-check=0", false);
header("Pragma: no-cache");
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM