PHP的setcookie类似于ASP.Net Cookie的子项 [英] PHP setcookie to resemble ASP.Net Cookie subkeys

查看:96
本文介绍了PHP的setcookie类似于ASP.Net Cookie的子项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.Net在饼干使用'子'的概念。即你可以写用一个cookie

ASP.Net has the concept of using 'subkeys' in cookies. i.e. You can write a cookie with

Response.Cookies("userInfo")("userName") = "patrick"
Response.Cookies("userInfo")("lastVisit") = "today"

这将创建一个cookie,看起来像

This would create a cookie which looks like

Name:   userInfo
Value:  userName=patrick:lastVisit=today

有没有在PHP中一个本地方法来读/写饼干像上面吗?

Is there a native method in PHP to read/write cookies like the above one?

我要读/写在PHP中的cookie可以通过ASP.Net与子读

I need to read/write a cookie in PHP which can be read by ASP.Net with subkeys

推荐答案

要编写这样一个cookie:

To write such a cookie:

$userInfo = array(
     'userName'  => 'patrick'
    ,'lastLogin' => 'today');

$userInfo = str_replace('&', ':', http_build_query($userInfo));
setrawcookie('userInfo', $userInfo);

要解析的cookie回一个数组:

to parse the cookie back into an array:

$userInfo = parse_str(str_replace(':', '&', $_COOKIE['userInfo'));

这篇关于PHP的setcookie类似于ASP.Net Cookie的子项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆