E
E
evolgin842020-01-28 10:28:06
URL Handling
evolgin84, 2020-01-28 10:28:06

How to redirect with cookies?

Hello, can you tell me how to do this?
It is necessary that the user enters the page and redirects him further by link1, if he has already been on this page, he will be redirected by link2, etc.
Thank you all in advance

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry, 2020-01-28
@Oldshelf

When going to a page (for example, "index.php"), set a cookie (for example, "MyCook" with the value "test") and redirect to page1.php via:

setcookie("MyCook", "test");
header ('Location: http://sample.domain/page1.php');

.htaccess to redirect to page2.php:
RewriteCond %{REQUEST_URI} ^\/index\.php$ [NC]
RewriteCond %{HTTP_COOKIE} ^.*MyCook=test$ [NC]
RewriteRule (.*) http://sample.domain/page2.php [L]

...or, you can check the cookie and make a redirect through the "header" using the server-side PL:
if ($_COOKIE ["MyCook"]=="test")
 header ('Location: http://sample.domain/page2.php');
else
{
 setcookie("MyCook", "test");
 header ('Location: http://sample.domain/page1.php');
}

PS Similar question: Redirect via htaccess on cookies?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question