在所有URL中设置PHP语言变量

在所有URL中设置PHP语言变量,php,Php,我正在为我的网站选择语言。所有这些都通过变量$language工作,并在cookie中设置: if(isset($_COOKIE["language"])) { $language = $_COOKIE["language"]; } else { $language = 'en'; } 一切正常,但网站的URL保持不变,无论设置何种语言: http://localhost/modules/products/products.php 我的变量$language返回我:enfre

我正在为我的网站选择语言。所有这些都通过变量
$language
工作,并在cookie中设置:

if(isset($_COOKIE["language"]))
{
    $language = $_COOKIE["language"];
}
else
{
    $language = 'en'; 
}
一切正常,但网站的URL保持不变,无论设置何种语言:

http://localhost/modules/products/products.php

我的变量
$language
返回我:
en
fr
es
it
de

在不更改所有链接的情况下,如何在所有URL中插入此变量以获得此结果

http://localhost/modules/products/products.php?lang=en


http://localhost/en/modules/products/products.php
(这将是我最喜欢的解决方案

对于最后一个答案,您需要自己构建所有链接,当查看您当前拥有的链接时,您以默认方式访问文件。 您可以在访问的每个文件中包含一个文件,如下所示:

if(isset($_GET['lang']))
{ 
   header("Location: " .getcwd()."?lang=".$language.");
}

您不需要在所有URL上设置它。您可以使用回退策略来完成此操作。请按以下顺序检查用户的意图:

  • 网址
  • 会话/Cookie
  • 浏览器/标题设置(可选)
  • 当以上各项均未设置时,请使用应用程序的默认设置
  • 实施

    在页面顶部有一个链接来切换语言。这是唯一一个包含语言参数的URL。此URL可称为语言更改器URL

    当您处理语言更改器URL时,将他们选择的语言设置为会话。然后,在每次后续请求中,从会话中读取该语言

    如果它不在会话中,也不在URL中,则返回应用程序的默认语言


    我就这个话题写了一个非常深入的答案-。

    你可以这样做:
    $language=$\u GET[“lang”]??$\u COOKIE[“language”]??“en”

    这将首先尝试从URL获取
    ?lang=en
    ,然后尝试获取cookie值,如果不存在,则使用默认值de
    “en”

    $\u GET
    数组将允许您访问通过url的查询字符串传递的每个值,该字符串是问号后面的部分

    ??
    操作员需要PHP7,此处对此进行了说明


    要允许这种url(),您需要通过web服务器配置重写路径,或者使用某种框架来为您重写路径。

    您的代码似乎不支持多语言url。也许最好的方法是创建脚本,将所选语言代码写入Cookie并重定向回Cookie

    像这样:

    //switch_lang.php
    $language=$\u获取[“lang”]??“en”;
    setcookie(“语言”,$language);
    标题('Location:'.$\u服务器['HTTP\u REFERER']);
    
    我的一个朋友给了我解决方案。在apache配置中激活htaccess文件。编辑您的htaccess文件并添加:

    RewriteEngine On
    RewriteBase /
    
    RewriteRule ^(en|fr|es|it|de)/(.*)$  $2?language=$1 [L,QSA]
    
    然后在php中:

    //Check if the select sends the Post lang
    if (isset($_POST["lang"]))
    {
        $var =substr_replace($_SERVER['REQUEST_URI'], $_POST["lang"], 1,2) ;
        header('Location:http://'.$_SERVER['HTTP_HOST'].$var);
    }
    
    //Redirect to EN laguage if no language is set in the URL, or Cookie
    if (empty($_COOKIE["language"]) && empty($_GET["language"]))
    {
        header('Location:http://'.$_SERVER['SERVER_NAME'].':8000/'.'en/');
    }
    
    //Check if language cookie is here and if it's different from the url
    if (isset($_COOKIE["language"]) && empty($_GET["language"]))
    {
            header('Location:http://'.$_SERVER['SERVER_NAME'].':8000/'.$_COOKIE["language"].'/');
    }
    elseif (isset($_GET["language"]))
    {
        $language = $_GET["language"];
        setcookie ('language', $language, time() + 60*60*24*30, '/', 'localhost');
    }
    else
    {
        $language = 'en';
    }
    
    语言选择器:

            <form action="" method="post">
                <select name="lang" onchange='this.form.submit()'>
                    <option value="en" {if $language == en}selected{/if}>En</option>
                    <option value="fr" {if $language == fr}selected{/if}>Fr</option>
                    <option value="es" {if $language == es}selected{/if}>Es</option>
                    <option value="it" {if $language == it}selected{/if}>It</option>
                    <option value="de" {if $language == de}selected{/if}>De</option>
                </select>
            </form>
    
    
    EN
    Fr
    锿
    信息技术
    扩散系数
    
    退房