PHP将会话变量分为值组的简单好方法吗?

序列化后,我想将会话的一部分保存到数据库表的大文本字段中。当前将会话变量放入数组似乎可行,但是我想知道是否有更好的方法来确切定义要保存的数据。如果我需要同时将会话分为多个组,同时还要将数据集存储为普通数组,那么我真的不想最终像4D数组那样结束。

例如,如果我想将$ _SESSION [“ client_gender”]保存到数据库,但不保存所有其他会话内容,例如用户登录状态。我将客户端数据集放入一个数组中,以便可以序列化并只保存客户端数组:$ _SESSION [“ clients”] [“ client_gender”]。

$_SESSION["clients"]["group_1"]["client_gender"] = $_POST["client_gender"]; //some input from the user

function save_client(){
global $db;

if(isset($_SESSION["client_id"]) && $_SESSION["clients"]){
    $clients = serialize($_SESSION["clients"]);
    $query = "UPDATE clients SET clients = '{$clients}' WHERE id = {$_SESSION["client_id"]}"; //I know it's a bit confusing that a column has the same name as the table
    $result = mysqli_query($db,$query);
}
}

// sometimes later when we want to use that client gender
function load_client(){
if(isset($_SESSION["client_id"])){
    $client = find_client_by_id($_SESSION["client_id"]); //just returns one row with a specific id from the clients table
    $_SESSION["clients"] = unserialize($client["clients"]);
}
}
echo $_SESSION["clients"]["group_1"]["client_gender"];

是否有另一种方式可以将某些数据标记为“客户端”,而将其他数据标记为“ group_1”,这些数据可能重叠也可能不重叠?他们还需要能够作为一个组进出数据库,而不必专门将每个值插入数据库并返回到会话。

dashewan333 回答:PHP将会话变量分为值组的简单好方法吗?

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3005407.html

大家都在问