【发布时间】:2019-03-28 22:39:31
【问题描述】:
编辑 - 2:请参阅 Sam 的案例解决方案。旧方法以及问题本身我仍然会留在这里,但是基于 Sam 的解决方案构建了一个新的解决方案,并且它可以在我保留的项目的 github 的存储库中获得
编辑 - 1:查看方法 destroy(id) 以了解解决方法
目前,我的系统在某些情况下需要使用新值更新其他用户会话。当前案例现在需要在执行某个操作时更改某些用户的会话文件中的单个值。 在我的项目中,我创建了一个实现 SessionHandlerInterface 的类 SessionHandlerCustom,并且我已经实现了一个为每个用户创建一个带有 Id 的自定义会话的逻辑。我可以访问自定义目录中的文件,但奇怪的是我不能对这些会话文件使用 file_put_contents 或 file_get_contents。我尝试使用 Session 函数,通过使用 SessionHandlerCustom 中提供的 read() 函数,我能够使用它的 SessionId 从用户会话中获取所有内容。
SessionFileGetValueByHashCode 的工作方法是从 Session 中获取内容的方法,并且使用一些 Key(通常是我想要在文件中的字段名称),它将获得确切的字符串那把钥匙和它的价值。 第二种方法根本不起作用,它实际上会更改 Session 中的值,但实际上并没有。我曾尝试直接操作文件但没有成功,并尝试使用 SessionHandlerCustom->write() 方法但没有效果。
谁能帮我解释一下改变/操纵其他用户会话的正确方法是什么?
系统示例:记录了一个普通用户,同时管理员使用管理功能并更改该用户的某些值。如果该值不是每次都从数据库中获取的值,则当前登录用户的会话变量需要更改其值。
如果你好奇,系统是开源的,你可以在这里访问它: https://github.com/msiqueira-dev/InfraTools
\*
This should be in the class that implements SessionHandlerInterface. This will actually delete the users session and force it to reload the whole session when it makes another request. The $SavePath variable is variable create and stored with the directory folder of the Session.
*/
public function destroy($id)
{
$file = $this->SavePath . "/sess_" . $id;
if (file_exists($file))
unlink($file);
return true;
}
SessionFileGetValueByHashCode
//The &$Value will be filled with the whole $Key and its values presented in the Session File. For example: SomeValue|s:0:"";
public function SessionFileGetValueByHashCode(&$Value, $Application, $SessionId, $Key)
{
$Value = NULL;
if(isset($Application) && !empty($Application) && isset($SessionId) && !empty($SessionId) && isset($Key) && !empty($Key))
{
$file = SESSION_PATH . $Application . "/sess_" . $SessionId;
if(file_exists(($file)))
{
$str = $this->InstanceSessionHandlerCustom->read($SessionId);
$start = strpos($str, $Key);
$end=$start;
while($str[$end] != '"')
{
$Value .= $str[$end];
$end++;
}
$Value .= '"';
$end++;
while($str[$end] != '"')
{
$Value .= $str[$end];
$end++;
}
$Value .= '"';
if($Value != NULL)
return Config::RET_OK;
}
}
return Config::RET_ERROR;
}
SessionFileUpdateValueByHashCode
public function SessionFileUpdateValueByHashCode($Application, $SessionId, $OldValue, $NewValue)
{
if(isset($Application) && !empty($Application) && isset($SessionId) && !empty($SessionId)
&& isset($OldValue) && !empty($OldValue) && isset($NewValue) && !empty($NewValue))
{
$file = SESSION_PATH . $Application . "/sess_" . $SessionId;
if(file_exists(($file)))
{
$str = $this->InstanceSessionHandlerCustom->read($SessionId);
$str = str_replace($OldValue, $NewValue, $str, $count);
if($count > 0)
{
echo $str . "<br>";
if($this->InstanceSessionHandlerCustom->write($SessionId, $str))
return Config::RET_OK;
}
}
}
return Config::RET_ERROR;
}
【问题讨论】: