简体   繁体   English

用于存储数据的会话或常量,在这种情况下更好

[英]Sessions or Constants for storing data, which is better in this situation

I would like some advice on storing data for different languages on my website. 我想要一些有关在我的网站上存储不同语言的数据的建议。 I've spent hours searchin and can't seem to find what I need. 我已经花了几个小时搜寻,似乎找不到我需要的东西。 Here's my situation, so let me know what you think. 这是我的情况,所以让我知道您的想法。

I have a website that multiple users can log into to view and store information. 我有一个网站,多个用户可以登录以查看和存储信息。 I use php, Mysql, Apache, on a FreeBSD operating system (although during development right now its on my home box with Vista). 我在FreeBSD操作系统上使用php,Mysql,Apache(尽管现在在开发过程中它在我的Vista盒中)。 I store some account information in the $_SESSION, but mostly use classes with local variables to store the data I need. 我将一些帐户信息存储在$ _SESSION中,但大多数情况下都使用带有局部变量的类来存储所需的数据。 I'll have anywhere from 2,000 to 6,000 users of the webapp, so I'm concerned about performance. 我将拥有2,000至6,000个Web应用程序的用户,因此我担心性能。 I want to make this available in multiple languages. 我想以多种语言提供此功能。 Some sites I've seen provide a dropdown list the user can select their language preference in, which I like. 我见过的一些网站提供了一个下拉列表,用户可以在其中选择自己喜欢的语言。

I've got two options I thought of, but have no clue as to which is better, or if there is a better way to accomplish this. 我有两种选择,但是不知道哪个更好,或者是否有更好的方法来实现。 One would be to store the language specific data in the $_SESSION object. 一种方法是将语言特定的数据存储在$ _SESSION对象中。 So, the user would log in, and based on the language preference the $_SESSION would be populated with the appropriate language text into the variables used throughout the webapp. 因此,用户将登录,并且根据语言首选项,将使用适当的语言文本将$ _SESSION填充到整个Web应用程序使用的变量中。 This could mean I would have around 300 or so variables with string data (no objects)...such as $_SESSION['My_Title'] = "This is the title to my website, in english, or german, etc.". 这可能意味着我将有大约300个左右的带有字符串数据的变量(没有对象)...例如$ _SESSION ['My_Title'] =“这是我网站的标题,用英语或德语,等等。” The other option would be to use CONSTANTS and define each CONSTANT in a config text file and load that file upon login based on the language preference set. 另一个选择是使用CONSTANTS并在配置文本文件中定义每个CONSTANT,并在登录时根据语言首选项集加载该文件。 I read somewhere that using CONSTANTS is somewhat slower than the $_SESSION, but the Session would use up more RAM. 我在某处读到,使用CONSTANTS的速度比$ _SESSION慢,但是Session会占用更多的RAM。

Any ideas, or resources you could point me to? 您有什么想法或资源可以指向我吗? Thanks. 谢谢。

The data you put into $_SESSION is on a per-user basis -- which means if you put the same data in $_SESSION for 10 users, then you'll have that data duplicated 10 times. 放入$_SESSION的数据是基于每个用户的-这意味着,如果将10个用户的相同数据放入$_SESSION ,则该数据将重复10次。

I would not put the localized string of your application in $_SESSION , personnaly : it's something that is "constant", the same for every users -- so it doesn't have its place in a space that's specific to each user. 我不会将应用程序的本地化字符串放在$_SESSION ,这很麻烦:这是“常量”的,对每个用户来说都是相同的-因此,它在每个用户专用的空间中没有位置。


Using PHP constants might be an idea ; 使用PHP常量可能是一个主意; for instance : 例如 :

en.php : en.php

define('LANG_TITLE', 'The title of the site');
define('LANG_WElCOME', 'Welcome on my site');

And fr.php : fr.php

define('LANG_TITLE', 'Le titre du site');
define('LANG_WELCOME', 'Bienvenue sur mon site');


Using a PHP array would be another ; 使用PHP数组是另一种; for example, you'd have en.php : 例如,您将拥有en.php

$lang = array(
    'title' => "The title of the site", 
    'welcome' => "Welcome on my site", 
);

And fr.php : fr.php

$lang = array(
    'title' => "Le titre du site", 
    'welcome' => "Bienvenue sur mon site", 
);


Which one should you choose ? 您应该选择哪一个? I suppose it's mainly a matter of taste, and there shouldn't be much of a difference between those two ideas. 我想这主要是一个品味问题,这两个想法之间应该没有太大的区别。


Or, another totally idea would be to use something like gettext ( see also ), which is a standard way of doing translations, and is (of course) usable from PHP . 或者,另一个完全的想法是使用诸如gettext之类的东西( 另请参见 ),这是进行翻译的标准方法,并且(当然)可以从PHP使用

If I had to choose, I might go for a solution based on gettext, as it's pretty standard -- or I'd use the Zend_Translate classes from Zend Framework, at least for a ZF-based project (btw, amongst those, there is one adapter which is using gettext ;-) ) 如果必须选择的话,我可能会选择基于gettext的解决方案,因为这很标准-或者我会使用Zend Framework中的Zend_Translate类,至少用于基于ZF的项目(顺便说一句,一个使用gettext的适配器;-))

Try to store as few data as possible in your session. 尝试在会话中存储尽可能少的数据。 By default the session data is stored in you file system by PHP and file system access is almost always slow. 默认情况下,会话数据通过PHP存储在文件系统中,并且文件系统访问几乎总是很慢。

I use session to store an identifier for the user. 我使用会话存储用户的标识符。 Apart from that I think twice about putting anything into the session. 除此之外,我会三思而后行。

Possible alternatives for storing data that needs to be persistent between requests are memory data chaches or your database (maybe in a table that is only stored in memory). 存储数据请求或数据库(可能在仅存储在内存中的表中)可能是在请求之间持久存储数据的替代方法。

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

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