从参数 php 获取 cookie 内容

get cookie content from an argument php

我准备了这个 cookie

    $id = "1"
    $password = "test";
    $cookie_name = "megamitch_server_status";
    $cookie_time = (3600 * 24 * 30); // 30 days
    setcookie ($cookie_name, 'usr='.$id.'&hash='.$password, time() + $cookie_time);

如何在该 cookie 名称中获取 usr 的值 "megamitch_server_status"?

如有任何帮助、想法和建议,我们将不胜感激。谢谢!

这应该适合你:

(这里我只是用preg_match_all()提取数据)

<?php

    preg_match_all("/usr=(.*)&hash=(.*)/", $_COOKIE["megamitch_server_status"], $matches);
    echo "usr: " . $matches[1][0];
    echo "hash: " . $matches[2][0];

?>

输出:

usr: 1
hash: test

或者,如果您愿意,可以像这样存储您的 cookie:

setcookie ($cookie_name . "[usr]", $id, time() + $cookie_time);
setcookie ($cookie_name . "[hash]", $password, time() + $cookie_time);

然后您可以像这样访问它们:

echo $_COOKIE["megamitch_server_status"]["usr"];
echo $_COOKIE["megamitch_server_status"]["hash"];