So my question is simple. All I would like to know is how to redirect the user to a specific link when he refreshes the page (is that even possible)? I need it for a WordPress project. Thanks in advance.
Asked
Active
Viewed 147 times
1
-
there is really no way to know if they hit refresh button or are leaving the page. There are ways to detect it on the page load if it was a refresh. – epascarello Sep 19 '19 at 17:17
-
Now that I think about it. It's logic, so there is no way I guess... – root Sep 19 '19 at 17:19
-
1https://stackoverflow.com/questions/5004978/check-if-page-gets-reloaded-or-refreshed-in-javascript – epascarello Sep 19 '19 at 17:23
2 Answers
0
Its simple, you have to use sessions for this purpose. At the start of the page u have to set a session variable when the user visits the page for the first time. Once the user visits that page that session variable will get set, the on the same page u have to check if that session variable is set or not, if its set that means user is trying to reload the site and that time u can redirect him.
Given below is the code
// Starting session
session_start();
if(isset($_SESSION["visited"])){
// redirect the user
}
else{
$_SESSION["visited"] = true;
}
Note* Also make sure that u destroy the session when user visits some other page of the site. Other wise page will always gets redirected untill session is destroyed.

Akshay Naik
- 553
- 3
- 17