Home > Article > Backend Development > How to refresh the page in PHP when clicking the browser's back button to return to the page?
Everything I search on the Internet is ASP. How to prevent the back button from being cached in PHP?
Everything I search on the Internet is ASP. How to prevent the back button from being cached in PHP?
Unable to prevent browsing behaviors such as history.back()
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>
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>
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>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