Skip to content

Commit

Permalink
fix for language redirect break in FF
Browse files Browse the repository at this point in the history
  • Loading branch information
jekuer committed Jan 20, 2022
1 parent 6090242 commit 7413338
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion assets/css/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* PHP Microsite Boilerplate
* +++++++++++++++++++++++++
*
* Version: 2.0.6
* Version: 2.0.7
* Creator: Jens Kuerschner (https://jenskuerschner.de)
* Project: https://github.com/jekuer/php-microsite-boilerplate
* License: GNU General Public License v3.0 (gpl-3.0)
Expand Down
2 changes: 1 addition & 1 deletion assets/js/all.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion assets/js/all.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 6 additions & 6 deletions assets/js/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,26 +87,26 @@ function adjustLanguage() {
if (activeLanguage != null && activeLanguage != '') {
if (languageCookie != null && languageCookie != '') {
if (languageCookie != activeLanguage) {
if (document.referrer.indexOf(window.location.host) === -1) {
// Redirect if possible.
if (document.getElementById('same_site_referrer').innerHTML === '1') {
// Update cookie if on same domain.
setCookie('language_select', activeLanguage, 30);
} else {
// Redirect if not and possible.
var linkToLanguage_lookup = document.querySelector('link[hreflang="'+languageCookie+'"]');
if (linkToLanguage_lookup != null && linkToLanguage_lookup != '') {
var linkToLanguage = linkToLanguage_lookup.href;
if (linkToLanguage !== null && linkToLanguage !== undefined && linkToLanguage != '') {
window.location = linkToLanguage;
}
}
} else {
// Update cookie.
setCookie('language_select', activeLanguage, 30);
}
}
} else {
// Set cookie.
setCookie('language_select', activeLanguage, 30);
var browserLanguage = navigator.language || navigator.userLanguage;
browserLanguage = browserLanguage.substring(0, 1);
if (document.referrer.indexOf(window.location.host) === -1 && browserLanguage != null && browserLanguage != '' && browserLanguage != activeLanguage) {
if (document.getElementById('same_site_referrer').innerHTML !== '1' && browserLanguage != null && browserLanguage != '' && browserLanguage != activeLanguage) {
var linkToLanguage = document.querySelector('link[hreflang="'+browserLanguage+'"]');
if (linkToLanguage !== null && linkToLanguage !== undefined && linkToLanguage != '') {
linkToLanguage = linkToLanguage.href;
Expand Down
2 changes: 1 addition & 1 deletion config.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

// Current version of the website.
// Will be used to control css and js caching.
$version_nr = '2.0.6';
$version_nr = '2.0.7';

// Multilanguage
$language['default'] = 'en'; // Use a key from the array below.
Expand Down
6 changes: 3 additions & 3 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* PHP Microsite Boilerplate
* +++++++++++++++++++++++++
*
* Version: 2.0.6
* Version: 2.0.7
* Creator: Jens Kuerschner (https://jenskuerschner.de)
* Project: https://github.com/jekuer/php-microsite-boilerplate
* License: GNU General Public License v3.0 (gpl-3.0)
Expand Down Expand Up @@ -117,7 +117,7 @@
/*if (isset($_COOKIE['language_select'])) {
$cookie_lang = make_safe($_COOKIE['language_select']);
if ($cookie_lang != $language['active']) {
if ($prev_url_to_parse == '') {
if ($same_site_referrer == false) {
// Redirect if possible.
if (isset($pages[$cookie_lang][$the_page->id])) {
$lang_redirect_url = $the_page_url;
Expand Down Expand Up @@ -145,7 +145,7 @@
setcookie('language_select', $language['active'], time() + (86400 * 30), "/", $tmp_domain); // 86400 = 1 day.
// Compare to browser language.
$browser_lang = substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2);
if ($prev_url_to_parse == '' and $browser_lang != $language['active']) {
if ($same_site_referrer = false and $browser_lang != $language['active']) {
if (isset($pages[$browser_lang][$the_page->id])) {
// This indicates that the user is new to the page and his browser language could be supported by one of the translations.
// You could now offer him a redirect or highlight the language switcher.
Expand Down
2 changes: 2 additions & 0 deletions lib/url_parsing.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,13 @@ function parse_the_url($url) {
$url_parts_all_prev = array();
$url_parts_prev = array();
$prev_url_to_parse = '';
$same_site_referrer = false;
if (isset($_SERVER['HTTP_REFERER'])) {
$prev_url_to_parse = filter_var($_SERVER['HTTP_REFERER'], FILTER_SANITIZE_URL);
$prev_url_to_parse_arr = parse_url($prev_url_to_parse);
if (isset($prev_url_to_parse_arr['host']) and isset($_SERVER['HTTP_HOST'])) {
if ($_SERVER['HTTP_HOST'] == $prev_url_to_parse_arr['host'] and isset($prev_url_to_parse_arr['path'])) {
$same_site_referrer = true;
$prev_url_to_parse = $prev_url_to_parse_arr['path'];
$url_parts_all_prev = parse_the_url($prev_url_to_parse);
} else {
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "php-microsite-boilerplate",
"version": "2.0.6",
"version": "2.0.7",
"description": "An extremely simple and lightweight PHP framework/boilerplate to spin-up microsites at high speed. Optimized for Performance, Security, SEO, and Directus CMS.",
"keywords": [
"css",
Expand Down
1 change: 1 addition & 0 deletions templates/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@

</head>
<body itemscope itemtype="http://schema.org/WebSite">
<?php echo '<div id="same_site_referrer" style="display:none;">' . $same_site_referrer . '</div>' // a workaround to have the referrer domain even, if Firefox does not support it ?>

<!-- Google Tag Manager (noscript) -->
<?php
Expand Down

0 comments on commit 7413338

Please sign in to comment.