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

发布于 2024-08-16 07:51:57 字数 652 浏览 3 评论 0原文

我想要一些关于在我的网站上存储不同语言的数据的建议。我花了几个小时搜索,似乎找不到我需要的东西。这是我的情况,请告诉我你的想法。

我有一个网站,多个用户可以登录以查看和存储信息。我在 FreeBSD 操作系统上使用 php、Mysql、Apache(尽管在开发过程中现在它在我的装有 Vista 的家庭机器上)。我在 $_SESSION 中存储一些帐户信息,但主要使用带有局部变量的类来存储我需要的数据。我将拥有 2,000 到 6,000 名 Web 应用程序用户,因此我担心性能。我想以多种语言提供此内容。我见过的一些网站提供了一个下拉列表,用户可以在其中选择他们的语言首选项,这是我喜欢的。

我想到了两个选择,但不知道哪个更好,或者是否有更好的方法来实现这一点。一种是将语言特定的数据存储在 $_SESSION 对象中。因此,用户登录后,根据语言首选项,$_SESSION 将使用适当的语言文本填充到整个 Web 应用程序中使用的变量中。这可能意味着我将拥有大约 300 个左右带有字符串数据的变量(无对象)...例如 $_SESSION['My_Title'] =“这是我的网站的标题,采用英语或德语等”。另一种选择是使用常量并在配置文本文件中定义每个常量,并在登录时根据语言首选项集加载该文件。我在某处读到,使用 CONSTANTS 比 $_SESSION 慢一些,但会话会占用更多 RAM。

您可以向我指出任何想法或资源吗?谢谢。

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). I store some account information in the $_SESSION, but mostly use classes with local variables to store the data I need. I'll have anywhere from 2,000 to 6,000 users of the webapp, so I'm concerned about performance. 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. 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. 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.". 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. I read somewhere that using CONSTANTS is somewhat slower than the $_SESSION, but the Session would use up more RAM.

Any ideas, or resources you could point me to? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

压抑⊿情绪 2024-08-23 07:51:57

您放入 $_SESSION 中的数据是基于每个用户的 - 这意味着如果您将相同的数据放入 $_SESSION 中供 10 个用户使用,那么您将获得该数据重复了 10 次。

我不会将应用程序的本地化字符串放在 $_SESSION 中,就个人而言:它是“常量”的东西,对于每个用户都是相同的 - 所以它在一个空间中没有它的位置具体到每个用户。

使用 PHP 常量可能是一个主意;例如:

en.php

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

fr.php

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

使用 PHP 数组是另一种方法;例如,您有 en.php

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

fr.php

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

您应该选择哪一个?我想这主要是一个品味问题,这两个想法之间应该没有太大区别。

或者,另一个完全的想法是使用类似 gettext (另请参阅),这是进行翻译的标准方法,并且(当然)可用来自 PHP

如果我必须选择,我可能会选择基于 gettext 的解决方案,因为它非常标准 - 或者我会使用 Zend_Translate 来自 Zend Framework 的类,至少对于基于 ZF 的项目(顺便说一句,其中有一个适配器是使用 gettext ;-) )

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.

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.

Using PHP constants might be an idea ; for instance :

en.php :

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

And fr.php :

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

Using a PHP array would be another ; for example, you'd have en.php :

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

And 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.

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 ;-) )

美胚控场 2024-08-23 07:51:57

尝试在会话中存储尽可能少的数据。默认情况下,会话数据由 PHP 存储在文件系统中,并且文件系统访问几乎总是很慢。

我使用会话来存储用户的标识符。除此之外,我在将任何内容放入会话中时都会三思而后行。

存储需要在请求之间持久化的数据的可能替代方案是内存数据缓存或数据库(可能在仅存储在内存中的表中)。

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.

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文