Redirect via COOKIE

0

Hello

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

User Accessed -> Page A (CRIOU THE COOKIE)

User is logged out.

When the user returns to the site, it should be redirected exactly to the page where it was previously, ie page A.

Any suggestions? Thanks!

    
asked by anonymous 14.03.2016 / 23:17

1 answer

2

On all pages that will have this feature, you must add a command to write the URI in 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 above code will loop redirects, then: how to know if the user was already on the site or if he just arrived? After all, I just need to redirect it if it has just arrived.

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

The simplest solution I can think of would be to use HTTP_REFERER of PHP. So the complete 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 code above works as follows: if the user came to the page through a reference (links pointing to your site, regardless of whether these links are internal or external), it just points to the new URI in the cookie named client_uri . If it arrived on the site and came from no reference, it checks if there is a cookie called client_uri and, if it exists, it redirects the user to the URI written in the cookie, if it does not exist, then it 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 this in a number of other ways, some even more efficient, but with a little more ingenuity. This not is the best way to do what you want, after all, you have problems that even Bacco left in commentary on your question: and if the guy left the site and when he comes back, he wants to go to the main URL and not where it was when it left? It also has the problem of the $_SERVER['HTTP_REFERER'] variable 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.

    
15.03.2016 / 01:21