How to do a proper redirect?

Hi all.
Please tell me how to do that for the first time, mobile phones have been redirect to the specific page of the same domain.
A man enters site.ru on the phone, and its going straight on site.ru/page

On the page site.ru/page should be a button to go to the home, the person presses this button and it opens site.ru.

How to do a redirect, there are a lot of ways...
But how to undo this redirect if the person is passed with the page?
April 3rd 20 at 17:47
4 answers
April 3rd 20 at 17:49
Solution
Can be stupid to add an anchor.
On the main something like:
if(isMobile() && location.hash !== '#ok')
 location.href = '/page';
visit: <a href="/#ok">Home</a>
But to do that we must not scripts.
What? - Crawford_Haley25 commented on April 3rd 20 at 17:52
Load this code

if(screen.width <= 576 && location.hash !== '#ok') {
 location.href = '/page';
}
- Crawford_Haley25 commented on April 3rd 20 at 17:55
April 3rd 20 at 17:51
But how to undo this redirect if the person is passed with the page?

var_dump($_SERVER);

there is a lot of useful information
If you use the http referer?
if(!isset($_SERVER['HTTP_REFERER'] == ‘site.ru/page’)) {
echo header(‘Location:site.ru/page’);
} - Crawford_Haley25 commented on April 3rd 20 at 17:54
@justen_Barton, you can use http referer, just the right condition to build need
and echo is not necessary, before header(); after header(); always use exit(); - Andres_Crooks commented on April 3rd 20 at 17:57
@Eulalia_Johns74, thank you! Will take into account. - Crawford_Haley25 commented on April 3rd 20 at 18:00
April 3rd 20 at 17:53
We must remember in cookies/session where he went, so as not to make a refund to someone else's page. Or just history.back();
And you can ask a working code? - Crawford_Haley25 commented on April 3rd 20 at 17:56
@justen_Barton, history.back() lol)) - Barrett_Osinski commented on April 3rd 20 at 17:59
@zoila, hung it on a click event - Barrett_Osinski commented on April 3rd 20 at 18:02
@zoila, so if you click off the page site.ru/page to use history.back() go to site.ru and again site.ru the redirect script will work and will spread back to site.ru/page
Or not? - Crawford_Haley25 commented on April 3rd 20 at 18:05
@justen_Barton, history.back () is the analog back in history. This is the only js option, the rest - to the shaman cookies. - Barrett_Osinski commented on April 3rd 20 at 18:08
April 3rd 20 at 17:55
1. Put cookie when you visit /page and add a check for the lack of cookie before redirect
2. Add the query parameter in the url when switching from /to the main page and check this parameter before redirecting (not very nice)
2. To check the Referer header before redirect (less reliable)

Find more questions by tags PHPJavaScript