php 使用 html 按钮更改站点语言

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15068132/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:29:57  来源:igfitidea点击:

Change language of site with a html button

phphtmlvariablesbutton

提问by kensil

In PHP, I want to change the language (English, German, etc) of the site when clicking a button. Is this the right way to approach that problem?

在 PHP 中,我想在单击按钮时更改站点的语言(英语、德语等)。这是解决这个问题的正确方法吗?

<?php 
  $language;
  if ($language == "en") {
    include("headerEn.php");
  } else {
    include("header.php");
  } 
?>
<a href="index.php"><?php $language = "en"; ?>
<img src="images/language/languageNO.png"></a>

<a href="index.php"><?php $language = "no"; ?>
<img src="images/language/languageEN.png"></a>

What is the best way to change the language of the site and have it persist when the user returns?

更改网站语言并使其在用户返回时保持不变的最佳方法是什么?

回答by NullPoiиteя

you can do this by

你可以这样做

<a href="index.php?language=en">
<a href="index.php?language=no">

and get the languages and store them in cookie and include file according to cookie like

并获取语言并将它们存储在 cookie 中并根据 cookie 包含文件,例如

if ( !empty($_GET['language']) ) {
    $_COOKIE['language'] = $_GET['language'] === 'en' ? 'en' : 'nl';
} else {
    $_COOKIE['language'] = 'nl';
}
setcookie('language', $_COOKIE['language']);

and than

然后

if ( $_COOKIE['language'] == "en") {
   include("headerEn.php");
} else {
   include("header.php");
} ?>

回答by Edwin Alex

To give a solution without changing your approach, You can do like this.

要在不改变方法的情况下提供解决方案,您可以这样做。

<?php 
if(isset($_GET['language']))
  $language = $_GET['language'];
else
  $language = "";

if ($language == "en") {
   include("headerEn.php");
} else {
   include("header.php");
} ?>

<a href="index.php?language = en"><img src="images/language/languageNO.png">      </a>
<a href="index.php?language = no"><img src="images/language/languageEN.png"></a>

If you want to keep the selection, you can store this value in Database or Session.

如果要保留选择,可以将此值存储在数据库或会话中。

回答by fedorqui 'SO stop harming'

It is always good to have a default value, so that you never end up being in a no-language site.

有一个默认值总是好的,这样你就永远不会进入一个没有语言的网站。

$language = $_REQUEST["language"];
$default_header="myheaderXXX.php";

switch ($language) {
    case "en":
      include("headerEn.php");
      break;

    case "no":
      include("header.php");
      break;

    default:
      include($default_header);
}

And then create links like this:

然后创建这样的链接:

<a href="index.php?language=en">
<a href="index.php?language=no">

回答by Tarun Kumar

You can implement the same code like this . I have editted your code .

您可以像这样实现相同的代码。我已经编辑了你的代码。

<?php 
$language; ?>
<?php if ($language == "en") : ?>
    <?php include("headerEn.php"); ?>
     <a href="index.php"><?php $language = "en"; ?><img src="images/language/languageNO.png"></a> 
<?php else: ?>
    <?php include("header.php"); ?>
     <a href="index.php"><?php $language = "no"; ?><img src="images/language/languageEN.png"></a>
<?php endif; ?> 

this will solve your problem .

这将解决您的问题。

回答by Chittaranjan Sethi

Try to save this $languagevalue to sessionvariable. When the page reloaded check that the session variable is set or not.

尝试将此$language值保存到会话变量。当页面重新加载时,检查会话变量是否设置。

If set use that $language

如果设置使用那个 $language

NOTE:

笔记:

$language = $_GET['language'];

回答by s.lenders

You can't change a variable within PHP by html. PHP is serversided HTML is clientsided

您不能通过 html 更改 PHP 中的变量。PHP 是服务器端 HTML 是客户端

You can use GET variablesto change it though. It is the easiest way to do it.

不过,您可以使用GET 变量来更改它。这是最简单的方法。