PHP - Server side variables that will “live” for ever?

↘锁芯ラ 提交于 2019-12-13 13:26:37

问题


I'm looking for a simple way to store a counter in the server memory to allow page load selection, kind of (pseudocode):

if counter is odd then load page-x 
else (even counter)  load page-y
increment counter by 1
store counter in server's memory

Session variables would not help, among multiple users.

I understand this could be achieved storing a field into the database, but this seems a cloggy approach. Was wondering for something faster.

That's why I thought about some server side variables that are kept in memory across sessions....


回答1:


If you have a database connection already open, I would consider using that. I can't imagine that querying one row can give you performance problems under any kind of load.

If you don't want to do that, a simple and fast approach for your "odd/even" check could be using a temporary file.

  • If the file exists, the condition is "odd"

  • If it doesn't exist, the condition is "even"

make sure you build in a check against the race condition of two instances trying to create or remove the same file at the exact same time - you have to make sure the script doesn't crash in that case.

This solution, obviously, survives even a server restart.




回答2:


Use APC, xCache, or memcache to save variables to cache instead of a database. Note however that a server reset will wipe out these values.




回答3:


What you're referring to is A/B testing. You would usually have a cookie that tracks an incoming visitor and then determines whether to serve version A or version B.

The reason you would use a cookie is because, if the user visits the same page and gets an alternative version, they're going to get confused. Using a cookie will put the visitor in either text group A or test group B, and serve the relevant page each time they visit (as long as the cookie lives).




回答4:


For true 50/50 easily, use a file:

$pageToShow = file_get_contents("whatever.dat");

if ($pageToShow == "A") {
    // show page a
    file_put_contents("whatever.dat", "B");
} else {
    // show page b
    file_put_contents("whatever.dat", "A");
}

Obviously create the file with just the letter "A" in it first.

Cheers.




回答5:


What about the Shared Memory extension if that's that simple?

Memcached is generally used for data caching, optionallly distributed. You can start reading from PHP memcache extension's pages.




回答6:


In addition to what was already mentioned, have a look at

  • PHP Dark Arts: Shared Memory Segments and
  • PHP Dark Arts: Semaphores

or in the PHP Manual:

  • Semaphore, Shared Memory and IPC



回答7:


Instead of using a database, you could simple use a cache mecanism (as proposed in the other answer) or simply store your value in a text file. This will be slower than the cache, but has the advantage of persisting even after a server reset.




回答8:


It seems you're trying to do A/B testing, have you looked into Google Website Optimizer, which allows you to do some quite detailed A / B analytics.

You can test out different pages, or page sections and figure out which one leads to more conversions.

I know this answer doesn't directly answer your technical question, but it may address the actual problem you're trying to solve.




回答9:


If it's just by chance, that you want to serve A or B, you could use a simple access time switch:

<?php
if (filemtime(".switch") % 2):
  require "A";
else:
  require "B";
endif;

touch(".switch");

This, obviously, doesn't serve as hard 50/50 solution.



来源:https://stackoverflow.com/questions/4086044/php-server-side-variables-that-will-live-for-ever

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!