简体   繁体   English

带有语言和平面文件的PHP会话

[英]PHP Session with a Language and Flat-File

Please excuse my English language skills. 请原谅我的英语能力。 I have a Homepage with a language exchange in the following included common.php 我在以下common.php中有一个可以进行语言交换的主页

    <?php
session_start();
header('Cache-control: private');
if(isSet($_GET['lang']))
{
$lang = $_GET['lang'];
$_SESSION['lang'] = $lang;
setcookie('lang', $lang, time() + (3600 * 24 * 30));
} else if(isSet($_SESSION['lang'])) {
$lang = $_SESSION['lang'];
} else if(isSet($_COOKIE['lang'])) {
$lang = $_COOKIE['lang'];
} else {
$lang = 'de';
}

switch ($lang) {
case 'en': $lang_file = 'lang.en.php'; break;
case 'de': $lang_file = 'lang.de.php'; break;
case 'ru': $lang_file = 'lang.ru.php'; break;
case 'fr': $lang_file = 'lang.fr.php'; break;
default: $lang_file = 'lang.de.php';
} 
include_once 'lang/'.$lang_file;
?>

Now I would like to implement on my website a Mini-Blog with Flat Files and use the following Code 现在,我想在我的网站上实现一个带有平面文件的迷你博客,并使用以下代码

  <?php
   define("DEFAULT_LANGUAGE", "lang=de");
   session_start();
   $languages = ["de", "en", "fr", "ru"];
   if( isset($_GET["lang"]) ){ $lang = filter_input(INPUT_GET, "lang", FILTER_SANITIZE_STRING);
   if( !in_array($lang, $languages) ){ $lang = DEFAULT_LANGUAGE; }
     $_SESSION["language"] = $lang;
   }
   function get_lang() { return isset($_SESSION["language"]) ? $_SESSION["language"] : DEFAULT_LANGUAGE; }
   function get_post_names() {
   static $_cache = [];
   if(empty($_cache)){ $_cache = array_reverse(glob(__DIR__ . '/posts/' . get_lang() . '/*.php')); }
   return $_cache;  
   }
   function print_posts() { $paths = get_post_names();
   foreach( $paths as $path ){ $content = file_get_contents($path);
   echo "<div class='post'>\n";
   echo "<p>{$content}</p>\n";
   echo "</div>\n\n";
     }  
   }
   ?>
   <?php print_posts(); ?>

For the blog posts, there is a folder [posts] in which subfolders (de, en, fr, ru). 对于博客帖子,有一个文件夹[posts],其中包含子文件夹(de,en,fr,ru)。 In these subfolders there are, depending on the language, for example: "examplepost.php" in whose blog posts have been written. 在这些子文件夹中,根据语言,例如:“ examplepost.php”,其博客文章均已撰写。 The language selection works without any problems. 语言选择有效,没有任何问题。 But I want that when a user selects a different language, then the Blog automatically reads the voice corresponding folder of the blog and containing the outputs files. 但是我希望当用户选择其他语言时,博客会自动读取博客的语音对应文件夹并包含输出文件。

The Problem is that there are two Sessions. 问题是有两个会话。 This leads to errors! 这会导致错误! My question would be: How can I merge everything together in a Session or how can I connect the two Sessions without conflicts, so it works both (the language of the site and the output of the files in the folders of the visitors selected language for the Blog posts). 我的问题是:如何将一个会话中的所有内容合并在一起或如何在不冲突的情况下将两个会话连接在一起,所以它既可以工作(网站的语言以及访问者选择的文件夹中文件的输出)博客文章)。

Many thanks in advance to all of you for your help and suggestions! 在此先感谢大家的帮助和建议! Greeting 问候语

You do not need two sessions to hold the same variable ($lang). 您不需要两个会话来保存相同的变量($ lang)。
What you can do is something like this: 您可以执行以下操作:

session_start();
// check/set default language session    
if (!isset($_SESSION["language"])) {
$_SESSION["language"] = DEFAULT_LANGUAGE;
}
if ($isset($_GET['lang']) && in_array($_GET['lang'], $languages) ){
$_SESSION["language"] = $_GET['lang'];
}
// execute more code
$lang_file = 'lang.'.$_SESSION["language"].'.php';
include_once 'lang/'.$lang_file;

andrew I think I have expressed myself wrong. 安德鲁,我想我说错了。 The common.php for the language exchange, is responsible and should be retained. 用于语言交换的common.php是负责任的,应该保留。 I just need a function in the common.php what are the same functions as in the other mentioned Script. 我只需要common.php中的一个功能与其他提到的Script中的功能相同。 It must be possible, that ... 一定有可能...

  • is changed to the selected language on the website 已更改为网站上的所选语言
  • And at the same time also referred to the sub-folder of the blog ("posts"). 并且同时也指博客的子文件夹(“帖子”)。

The Structure 结构

[root]
|_ acp ........ admin-center
|_ css ........ css styles
|_ img ........ graphics
|_ inc ........ includes files
|_ func ....... function files (i.e. common.php)
|_ js ......... js files
|_ lang ....... language files
|_ posts ...... for the Blog Postings 
  |_ [de] [en] [fr] [ru]

In the last Folders are .php files witch, depending on the language selection has been selected via the common.php at the same time with the common.php also the files of the [posts] folder of the blog and displayed to be read. 最后一个文件夹是.php文件,根据语言选择,同时通过common.php和common.php选择了博客的[posts]文件夹中的文件,并显示为可读取。

In short ... 1) depending on the language in the language file redirect - AND - 2) in addition to the sub-folder of the [posts] folder and the contained files to be read and the contents of the files output. 简而言之... 1)取决于语言文件中的语言,重定向[AND]-2)除了[posts]文件夹的子文件夹以及要读取的包含文件和文件内容输出。

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

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