简体   繁体   English

为 URL 中的 GET 参数设置默认值

[英]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).例如,当他单击一种语言时,他会从https://example.com重定向到https://example.com/?lang=nl ,例如选择比利时(荷兰语 -> 荷兰语 -> nl)。

When a page loads the Javascript code checks if the lang GET-parameter is set.当页面加载 Javascript 代码时,检查是否设置了 lang GET 参数。 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.如果已设置,代码将搜索 HTML 中的所有现场锚标记并将正确的 lang-tag(nl、en、fr、...)附加到该锚标记的 href 属性。

So for example <a href="/about">about us</a> becomes <a href="/about?lang=nl">about us</a> .例如<a href="/about">about us</a>变成了<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.然后我有一个 PHP 函数output($text_tag) ,它检查 lang GET 参数并在数据库中搜索与text_tag和所需语言匹配的文本。

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! output("%hello%")带有$_GET['lang'] == "FR" output("%hello%")将返回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.但问题是,当用户在网站上进行第一次登录时,他只访问了 base-url,并且尚未设置 lang GET 参数。 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.当他点击一个锚标签时,如果没有设置 lang GET-param,就会选择默认语言。

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 .访问https://example.com将带您到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.例如,POST 请求现在经常失败,因为我由于重定向而丢失了发布的数据。

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.因为现在每个第一次访问该站点的用户都会加载该站点,然后立即重新加载以附加 lang GET 参数。 I can imagine this will result in a bad loading performance, user-experience, and worse SEO-results.我可以想象这会导致糟糕的加载性能、用户体验和更糟糕的 SEO 结果。

So, what is the most efficient way to set a default value for a GET-parameter and to make the GET-parameter required?那么,为 GET 参数设置默认值并使 GET 参数成为必需的最有效方法是什么?

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.正如对您的问题的评论中所述,您始终可以在会话中跟踪用户输入并将 (301) 重定向到新页面,并根据最终用户的输入加载您想要的环境变量。

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.但是,您也可以使用 IP 检测库来评估最终用户的位置,并在默认情况下建议或加载其语言/位置的内容。 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.我不打算在这里认可特定的产品,但是如果您给我发消息,我很乐意推荐一个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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