简体   繁体   English

php,在请求之间共享SESSION,强制session_id

[英]Php, share SESSION between requests, forcing session_id

My goal is to share session between requests, I meant every request could reach a data. 我的目标是在请求之间共享会话,这意味着每个请求都可以到达一个数据。 First I was thinking that simply sharing via filesystem could be good, but I find out this by myself: 首先,我认为仅通过文件系统共享可能会很好,但是我自己发现了这一点:

session_id('0');
session_start();
echo session_id();
var_dump ($_SESSION);
if (!isset($_SESSION['x']))
{
    $_SESSION['x'] = 0;
}
$_SESSION['x']++;
var_dump ($_SESSION);

this way I can see the same from browsers. 这样,我可以从浏览器中看到相同的内容。 My question is, is it a good practice? 我的问题是,这是一种好习惯吗?

EDIT: here is the full working version: 编辑:这是完整的工作版本:

$m = microtime(true);
session_start();
if (session_id() == '0') // this happens when somehow our session id sticks, it should not happen ever, but if so, lets erase it
{
    setcookie (session_name(), '', time() - 3600);
    session_destroy();
    session_write_close();
    echo 'reload'; die;
}
if (!isset($_SESSION['x']))
{
    $_SESSION['x'] = 0;
}
$_SESSION['x']++;
$saveId = session_id();
session_write_close();

// switch to common storage
session_id('0');
session_start();
if (!isset($_SESSION['common']))
{
    $_SESSION['common'] = 0;
}
$_SESSION['common']++;
session_write_close();

// back to our own session
session_id($saveId);
session_start();

echo $_SESSION['x'].'<br>'.(microtime(true) - $m); die;

I dont thing its very time consuming. 我不觉得这很耗时。

It's tricky to know if SESSION is the right place to put this data, but it's worthwhile bearing some things in mind. 知道SESSION是否是放置此数据的正确位置是很棘手的,但值得牢记一些注意事项。

  • SESSION is designed to store data related to an individual user's visit to your site (normally being distinguished the combination of machine and browser, thanks to the session id being stored in a client side cookie). SESSION旨在存储与单个用户访问您的站点有关的数据(由于会话ID存储在客户端cookie中,因此通常区分机器和浏览器的组合)。
  • Default behaviour of the PHP session handler is to: PHP会话处理程序的默认行为是:
    • Store the data in a file on the server. 将数据存储在服务器上的文件中。
    • Block concurrent access to that file. 阻止并发访问该文件。
  • It is possible to have multiple sessions for a given request, but that means ensuring you start and end each session and ensure that you keep track of the session IDs - I'm not entirely sure how you would do this without manually writing data into the client's cookie. 对于一个给定的请求, 可能有多个会话,但这意味着要确保您开始和结束每个会话,并确保跟踪会话ID-我不完全确定如果不手动将数据写入到会话中将如何执行此操作。客户的Cookie。

All in all you'll probably find that your performance using the session will be slower that just checking the existence of the file anyway (which is simpler than using the session, in terms of work done by PHP). 总而言之,您可能会发现使用会话的性能会比仅检查文件是否存在要慢(就PHP的工作而言,这比使用会话更简单)。

That said, if you're writing to that file then you're just going to have concurrency issues that you'll have to solve in much the same way as php sessions do anyway. 就是说,如果您正在写入该文件,那么您将遇到并发问题,无论如何,您都必须以与php会话相同的方式来解决。

I'd say, if you're writing data, then look to your DB. 我想说的是,如果您正在编写数据,请查看您的数据库。 It's what they're designed for. 这就是他们的目的。

If you don't want to write to your primary DB and have good reason for that, then maybe consider something like a memcache DB, or some other secondary storage. 如果您不想写入主数据库并且有充分的理由,那么可以考虑使用诸如内存缓存数据库或其他辅助存储之类的工具。

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

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