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?
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
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?
<?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.
<?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.
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.