简体   繁体   中英

Set default value for GET-parameter in URL

I have a multi-language website. I have a own implementation of the multi-lang system: In the header the user can select the flag matching his language. When he clicks a language he gets redirected from https://example.com to https://example.com/?lang=nl when he for example selects Belgium (Dutch -> Nederlands -> nl).

When a page loads the Javascript code checks if the lang GET-parameter is set. If it is set, the code searches for all the on-site anchor-tags in the HTML and appends the correct lang-tag (nl, en, fr,...) to the href-attribute of that anchor-tag.

So for example <a href="/about">about us</a> becomes <a href="/about?lang=nl">about us</a> . This way the user always stays in the same 'language bubble'.

I then have a PHP function output($text_tag) which checks the lang GET-parameter and searches in the database for the text matching the text_tag and the required language.

Eg:

+----+----------+------+------------+
| Id | text_tag | lang |    text    |
+----+----------+------+------------+
|  1 | %hello%  | EN   | Hello!     |
|  2 | %hello%  | NL   | Goedendag! |
|  3 | %hello%  | FR   | Bonjour!   |
+----+----------+------+------------+

output("%hello%") with $_GET['lang'] == "FR" will for example return Bonjour! .

This system works perfectly fine. It probably isn't the most effective one but changing this is out of question.

But the problem is that when the user makes his first entry on the site he just visits the base-url and a lang GET-parameter is not yet set. This resulting in words on the first page he visits not getting translated accordingly. When he clicks on an anchor-tag a default language gets chosen when the lang GET-param is not set.

So I want to solve the problem that a default parameter should be set when he makes his first entry on the site.

I tried two solutions:

Server-side:

 if (!isset($_GET['lang'])) {
        $url = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
        $url .= (parse_url($url, PHP_URL_QUERY) ? '&' : '?') . 'lang=nl';

        header("Location: " . $url);
    }

This gets the job done. Visiting https://example.com will take you to https://example.com?lang=nl . But the problem is that this messes up a lot. POST-requests for example now often fail because I lose the posted data due to the redirect.

So I tried a client-side approach. Client-side:

$(document).ready(function() {
    //findGetParameter: https://stackoverflow.com/a/5448595/6533037
    if (findGetParameter("lang") == null) {
        let url = new URL(window.location.href);
        url.searchParams.append("lang", "nl");

        window.location.href = url;
    }
}

This also works fine and doesn't cause any issues with requests or anything. But I am searching for a more efficient method. Because now EVERY user who visits the site for the first time loads the site, and then immediately gets a reload to have the lang GET-parameter appended. I can imagine this will result in a bad loading performance, user-experience, and worse SEO-results.

So, what is the most efficient way to set a default value for a GET-parameter and to make the GET-parameter required?

Not a direct answer, but I'm going to suggest an alternate approach. As mentioned in the comments to your question, you can always track user-input in a session and redirect (301) to a new page and load the environment variables you desire based on the end-user's input.

But, alternately you can use IP detection libraries to evaluate the end-users location and suggest or load by default the content for their language/location. There are a large number of libraries and services that provide this functionality.

I realize this does not answer your question directly, but I have found that using such libraries have saved me a tremendous amount of time/headache in getting the end-user to the 'alternate' content designed.

I'm not going to endorse a specific product here, but if you message me, I'd be glad to suggest one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM