How can I redirect users to different website variant based on last used variant?

I have 2 variants of my website (say, A & B). They’re stored on the server at /a/ & /b/.

On my home page (/index.html), I wanna give users the option to choose (mostly a button) which variant to visit.

Then, I want to set that variant as the default for the user, so, the next time, the user tries to visit index.html, he/she will be redirected to the last visited variant.

Both the variants will have toggles to switch to the other one. So, if a user switches to the other one (say B from A), I’d like to set the new one (B) as the default so that the user gets redirected to the new one (B) next time he/she visits /index.html.

The redirects are supposed to work only on the /index.html page, so that if the users visit the variant specific webpage by directly typing the URL, they don’t get redirected. However, the preferences should get changed, i.e., if last time the user had visited variant A and now, has used the URL of variant B, the next time he/she visits /index.html, he/she should be redirected to B.

I am making a wild guess by saying that cookies and JavaScript can help me achieve this.

Can someone tell me if and how I can achieve this?

Javascript might work, but a server side script, like PHP, might be better.
Do you have PHP available on the server?

Yes, I do. How can I achieve it using PHP? Can you please guide?

What you’d want to do is in the php of /a/ and /b set a cookie:

// In the code for for variant A
define('COOKIE_EXPIRY', 30 * 24 * 60 * 60); // thirty days, change as needed
setcookie('chosen_variant', 'a', time() + COOKIE_EXPIRY);

// In the code for for variant B
define('COOKIE_EXPIRY', 30 * 24 * 60 * 60); // thirty days, change as needed
setcookie('chosen_variant', 'b', time() + COOKIE_EXPIRY);

(if a and b share a php file you can define COOKIE_EXPIRY there too)

And then in your index.php check if the chosen_variant cookie is set:

// Variants we know - cookie value => path to redirect to
$knownVariants = [
    'a' => '/a/',
    'b' => '/b/',
];

// If there is a variant set and it's a known variant (for example, not 'c') ...
if (isset($_COOKIE['chosen_variant']) && array_key_exists($_COOKIE['chosen_variant'], $knownVariants)) {
    // ... then redirect to the URL defined in $knownVariants
    header('Location: '.$knownVariants[$_COOKIE['chosen_variant']]);
}
// ... otherwise show page as normal - i.e. show the choice between variants a and b
1 Like

I don’t exactly understand what you mean by ‘in the php’ of /a/ and /b/.

I’m sorry, I don’t know php. I just know that my server supports php.

Can you please tell in what file do I have to add that code? If I just have to add it in the directory, what should be the name of the file and how do I run it?

Also, the second code that you mentioned, well, I have index.html file. How do I make it to index.php?

You should look up PHP tutorials and learn the language so you can understand the code and other people don’t have to make the whole work for you.

Well, I tried to add the code as:

<?php
// In the code for for variant A
define('COOKIE_EXPIRY', 30 * 24 * 60 * 60); // thirty days, change as needed
setcookie('chosen_variant', 'a', time() + COOKIE_EXPIRY);
?>

on top on my HTML code and renamed my file from .html to .php and it worked. The cookies were set.

However, when I did the same for my /index.html file to add the redirect code, it doesn’t redirect even if the cookie is set.

I’m trying to run php scripts from HTML page, but, I’m finding anwers saying that I can’t.

Nope, only .php files can contain php. PHP in .html files won’t be executed.

I modified the code a bit after looking on other resources:

Now, my /a/index.php looks like:

<?php
	setcookie("chosen_variant","a",time()+86400*365,"/","domain.tld",1);
?>

<html>
</html>

It’s correctly setting the cookie with the name chosen_variant and with the value a when /a/index.php loads.

However, when /index.php loads, the redirect doesn’t work.

Yeah, I got that by now. Thus, I changed from .html to .php.

1 Like

What is the exact code in /index.php?

<?php
  // Variants we know - cookie value => path to redirect to
$knownVariants = [
    'a' => '/a/',
    'b' => '/b/',
];

// If there is a variant set and it's a known variant (for example, not 'c') ...
if (isset($_COOKIE['chosen_variant']) && array_key_exists($_COOKIE['chosen_variant'], $knownVariants)) {
    header('Location: '.$knownVariants[$_COOKIE['chosen_variant']]);
}
?>

<html>
</html>

When I’m using this file and adding:

<?php
echo "$knownVariants";
?>

I’m getting Array written in the top left of the page.

And what do you see when you put the following in the code:

<?php
var_dump($_COOKIE);

?

If I got you right, I removed the echo part and replaced it with your new code.

I got text like this:

array(3) { [“__cfduid”]=> string(43) “da5e3faf4242fbeae4e3aae98bfa293391555735916” [“cookieconsent_status”]=> string(7) “dismiss” [“chosen_variant”]=> string(1) “a” }

I found this one that works for now:

<?php
if($_COOKIE['chosen_variant'] == "a")
 {header("Location: /a/");}
elseif($_COOKIE['chosen_variant'] == "b")
 {header("Location:/b/");}
else
 {}
?>

This action in itself would have been preventing the relocation from working - as soon as you send something to the browser, you can’t send headers any more.

I added that part just out of curiosity and a hope to see if it works. It wasn’t working even without it.

Weird, it works for me: https://repl.it/repls/WelcomeProfitableFormat

Anyway, your code might work but it will emit notices if the cookie is not set. Better would be:

if (isset($_COOKIES['chosen_variant'])) {
  if ($_COOKIE['chosen_variant'] === 'a') {
    header("Location: /a/");
  } elseif ($_COOKIE['chosen_variant'] === 'b') {
    header("Location: /b/");
  }
}
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.