Redirect via COOKIE

Asked

Viewed 463 times

0

Hello !

My client needs the user to be redirected to the page he was accessing before ending the previous session. For example:

User Accessed -> Page A (CREATED COOKIE)

User logged out.

When the user returns to the site, he or she should be redirected exactly to the page where he or she was previously, i.e., page A.

Any suggestions? Thank you!

  • 2

    It would be nice to post already what you tried to do, and what difficulty you found, and better specify the structure of the site and under what conditions it is to happen redirecting. If it is just to have a "guess" of how to do, I think that besides being outside the scope of the site, and to make it worse, it ends up depending on the opinion of each one. For example, what happens if a person enters through a link to a given page? When they want to go back home, how do they do it? (and so on, explaining clearly how it is to work)

1 answer

2

On all pages that will have this feature, you should add a command to save the URI to the cookie, like this:

setcookie('client_uri', $_SERVER['REQUEST_URI'], (time() + (30 * 24 * 3600)));

The above command will store the current URI in a cookie named client_uri that will be valid for 30 days.

To redirect the user to the page he was on, do this:

header("Location: //{$_SERVER['HTTP_HOST']}{$_COOKIE['client_uri']}");

And now you have a problem, because only the code above will make a loop of redirects, so: how to know if the user was already on the site or if it just arrived? After all, I only need to redirect him if he’s just arrived.

At first use the sessions PHP seems to be a solution, because they are destroyed when the browser is closed (unless the client uses the option to continue where it left off in the browser settings), but there is another problem: what if it just closes the tab and goes back to the site? The Session will still be there and it will not be redirected to the page that was previously.

The simplest solution I can think of would be to use the HTTP_REFERER PHP. Then the full code would look like this:

<?php

if (!isset($_SERVER['HTTP_REFERER'])) {
    if (isset($_COOKIE['client_uri']) && !empty($_COOKIE['client_uri'])) {
        header("Location: //{$_SERVER['HTTP_HOST']}{$_COOKIE['client_uri']}");
    } else {
        setcookie('client_uri', $_SERVER['REQUEST_URI'], (time() + (30 * 24 * 3600)));
    }
} else {
    setcookie('client_uri', $_SERVER['REQUEST_URI'], (time() + (30 * 24 * 3600)));
}

The above code works as follows: if the user has reached the page through a reference (links pointing to your site, regardless of whether these links are internal or external), it just arrow the new URI in the cookie called client_uri. If it arrived on the site and did not come from any reference, it checks if there is a cookie called client_uri and, if it exists, redirects the user to the URI recorded in the cookie, if it does not exist, then keeps the user on the page and creates a cookie called client_uri.

It’s the simplest solution I can think of and you could do it in several other ways, some even more efficient, but with a little more ingenuity (complication). That one nay is the best way to do what you want, after all, have problems that even Bacco left in comment on your question: what if the guy left the site and when he comes back, he wants to go to the main URL and not where he was when he left? There is also the variable problem $_SERVER['HTTP_REFERER'] that some browsers may not send or send with some manipulation.

Anyway, I believe that with this you already have some basis to start developing in the way that best suits your needs.

  • 1

    The answer is good, but I find it too risky to answer without the author clarifying a few points before. Took my +1 by the care of explaining the main problems (besides using Location: correctly, with the full name).

  • you are a genius friend! I will try this way and give you a feedback... +1

Browser other questions tagged

You are not signed in. Login or sign up in order to post.