Home > Backend Development > PHP Tutorial > How to refresh the page in PHP when clicking the browser's back button to return to the page?

How to refresh the page in PHP when clicking the browser's back button to return to the page?

WBOY
Release: 2023-03-02 07:32:01
Original
2536 people have browsed it

Everything I search on the Internet is ASP. How to prevent the back button from being cached in PHP?

Reply content:

Everything I search on the Internet is ASP. How to prevent the back button from being cached in PHP?

  1. Unable to prevent browsing behaviors such as history.back()

  2. You can consider setting the page to have no cache

    <code>$nocache = array(
        'Expires' => '0',
        'Cache-Control' => 'no-store,private, post-check=0, pre-check=0, max-age=0',
        'Pragma' => 'no-cache'
    );
    foreach($nocache as k => $v)
        header($k.': '.$v);
    </code>
    Copy after login
  3. If you really want to refuse to go back, you can use the following method to simulate it. Consider making a jump, so that back once will automatically jump to the current page, which is similar to refreshing

    <code>header('Location: index.php');</code>
    Copy after login

Take the home page pagination link as an example:
<a href="/index.php?page=3" onclick="page(3);return false;">3</a>
Search engine crawler /index.php?page=3 will be accessed according to href to obtain the data on page 3, which is beneficial to SEO. Users can also access /index.php?page normally by right-clicking in the browser and selecting "Open in new tab" =3.
If the user clicks the link directly on the page, the click event is triggered, and the local data is loaded and rendered by JS through AJAX, and the location.hash is set to
/index.php#/page/3.
location.hash = "#/page/3";The browser can remember the location.hash history by itself. We only need to listen to the location.hash change event
hashchange(supports IE8, does not support IE7/6) to realize user The effect of reloading the page when clicking the browser's back button.

<code>$(window).on("hashchange", function(){
    alert(location.hash); //输出#/page/2
    var arr = location.hash.split("/"); // ["#", "page", "2"]
    if(arr[1] == "page") {
        page(arr[2]); //AJAX局部加载第2页数据
        //location.href = "/index.php?page="+arr[2]+"&"+new Date().getTime(); //直接访问第2页
    }
});</code>
Copy after login
The above "hyperlink design with equal emphasis on SEO and experience" should be able to meet the needs of the poster, but it requires a certain transformation cost.

The page is loaded and js refreshes the page location.href = location.href

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template