从参数php获取cookie内容

时间:2015-03-19 03:18:18

标签: php cookies

我在set

上有这个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名称“megamitch_server_status”中获取usr的值?

任何帮助,想法,建议将不胜感激。谢谢!

1 个答案:

答案 0 :(得分:0)

这应该适合你:

(这里我只使用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"];