I am making a project in school where we need to make a small website on a specific topic, now I want that when the browser's back button is clicked. I come to my home page which I have included in the code provided above, Please help me!
-
The "code provided above" is not there. – jlewkovich Dec 25 '19 at 18:59
-
Additionally, I'm not sure this is possible without using javascript. – jlewkovich Dec 25 '19 at 19:00
-
Does this answer your question? [Is there a way to catch the back button event in javascript?](https://stackoverflow.com/questions/136937/is-there-a-way-to-catch-the-back-button-event-in-javascript) – jlewkovich Dec 25 '19 at 19:00
2 Answers
i dont know if it is true or not , but i tried a simple code now , so when user opened our first page , current page URL will save in your session or cookie . after that in second page when user clicked back button you should save that page URL in session again . when user clicked back you have 2 URL and you can check : if these 2 URL matches it means user is in same page , but else user clicked back button . and you must check this in all your pages or in pages you want .
if ($_SESSION['link1'] === $_SESSION['link2']) {
$backClicked = false;
}else {
$backClicked = true;
header("location: Your Home Page");
}

- 651
- 1
- 9
- 24
There is no way to control the back button in a browser only using HTML
and CSS
. You will have to use JavaScript
.
With JavaScript
you could look to use history.pushState()
or history.replaceState()
to modify the history of the browser. Adding the homepage to the history, so that when the user clicks the back button that is where they will go.
You can read more about the History
API here:
https://developer.mozilla.org/en-US/docs/Web/API/History_API#Interfaces

- 3,481
- 14
- 30