阅读codeigniter cookie

时间:2015-01-22 21:35:50

标签: php cookies

我正在制作一些采用向导格式的网页,我想记住上一页,所以我将数据存储在cookie中。默认的codeigniter cookie ci_session

一旦我收到用户输入,我将其保存在cookie中并转到下一步

$posted_identifier = $this->input->post('new_account');
    $this->session->set_userdata('identity'.rand(), $posted_identifier);

最后,我的cookie看起来像

a:13:{s:10:"session_id";s:32:"6cb4cae70007f6e2b58ba147c57c7368";s:10:"ip_address";s:7:"0.0.0.0";s:10:"user_agent";s:72:"Mozilla/5.0 (Windows NT 6.3; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0";s:13:"last_activity";i:1421961876;s:9:"user_data";s:0:"";s:8:"identity";s:7:"jjkjlkm";s:13:"identity11027";b:0;s:13:"identity25967";s:3:"uio";s:13:"identity25434";s:3:"789";s:13:"identity10013";b:0;s:13:"identity29325";b:0;s:12:"identity4963";b:0;s:13:"identity23334";b:0;}59166fb25addef85e48447ae8dde7b9e 

我试过这个

$json =  $this->input->cookie('ci_session', TRUE);
var_dump(json_decode($json,TRUE));

但它给了我null。不是json格式的cookie,我如何读取名为identity或至少包含identity的名称的值?。

1 个答案:

答案 0 :(得分:1)

它不是JSON而是序列化字符串。尝试:

print_r(unserialize($cookie_string));

应该导致

Array
(
[session_id] => 6cb4cae70007f6e2b58ba147c57c7368
[ip_address] => 0.0.0.0
[user_agent] => Mozilla/5.0 (Windows NT 6.3; WOW64; rv:35.0) Gecko/20100101 Firefox/35.0
[last_activity] => 1421961876
[user_data] =>
[identity] => jjkjlkm
[identity11027] =>
[identity25967] => uio
[identity25434] => 789
[identity10013] =>
[identity29325] =>
[identity4963] =>
[identity23334] =>
)