首页 > 解决方案 > 如何从数组中按名称获取 cookie 值

问题描述

我有一个 cookie,我在其中存储了一组值。现在,当我尝试通过 cookie 的名称访问信息时,它不断给我错误“非法字符串偏移”。我不明白这个错误在我的情况下意味着什么。

我究竟做错了什么?

为什么我不能使用 cookie 的名称访问详细信息?

这是我的代码:

    if (isset($_POST['save_details'])) {
        if (isset($_COOKIE['historyDetails'])) {



            $read_cookie=json_decode($_COOKIE['historyDetails']);


            $cookieValue=$read_cookie;
        }

        $details['totalsalary_cookie'] = $total_salary;
        if (isset($extra)) {
            foreach($extra as $e){
                $details=explode("#",$e);
                $name=$details;
                $details['extras_cookie']= $name;
            }
        }
        $details['date_cookie']= date("d/m/y");
        $details['time_cookie']= date("h:i:sa");
        $cookieValue =$details;
        setcookie("historyDetails", json_encode($cookieValue), $one_week);



    }





    echo "<ul>";
    echo "<li>Salary details: Total salary is BD $total_salary";
    if (count($extra_names)>0) {
        echo "<li>Extras:</li>";
        echo "<ol type = "."1".">";
        for ($i=0; $i <count($extra_names) ; $i++) {
            echo "<li>$extra_names[$i]</li>";
        } echo "</ol>";
    }
    echo "</ul>";








 die("</body></html>");
}elseif (isset($view_history)) {
  //echo $_COOKIE["historyDetails"];
    //print_r($_COOKIE);

?>
<table align="center" border="1">
<tr>
<th>Total Salary</th>
<th>Date</th>
<th>Time</th>
<th>Extras</th>
</tr>
<?php
if(isset($_COOKIE["historyDetails"])){
  echo "<tr>";
  echo "<td align=center>".$_COOKIE["historyDetails"]["totalsalary_cookie"]."</td>";
  echo "<td align=center>".$_COOKIE["historyDetails"]["date_cookie"]."</td>";
  echo "<td align=center>".$_COOKIE["historyDetails"]["time_cookie"]."</td>";
  if(isset($_COOKIE["historyDetails"])){
    echo "<td align=center>";
    echo "<ul>";

      $emp=explode("#",$_COOKIE["historyDetails"]["extras_cookie"]);
      echo "<li>".$emp."</li>";
 
    echo "</ul>";
    echo "</td>";
  }
else{
  echo "<td align=center>"."No extras Found!"."</td>";
}
  echo "</tr>";

}
die();
}
?>

有什么帮助吗?

标签: php

解决方案


在不知道您尝试在 cookie 中设置的内容的情况下,我不得不即兴测试此代码。在我的测试中,cookie 的内容是 urlencoded,这就是为什么 - 结合值是 json 编码的事实,我怀疑你无法直接访问 cookie 中保存的值。

<?php 

    $_POST['save_details']=true;
    
    $cname='historyDetails';

    $one_week=strtotime( 'now + 1 week' );
    $total_salary=mt_rand(150000,250000);
    $extra=array(
        'banana'    =>  'yellow#curvy#healty',
        'snow'      =>  'cold#white#fun'
    );
    
    

    if ( isset( $_POST['save_details'] ) ) {
    
        if( isset( $_COOKIE[ $cname ] ) ) {
            $cookieValue=json_decode( $_COOKIE[ $cname ] );
        }

        $details['totalsalary_cookie'] = $total_salary;
        
        if( isset( $extra ) ) {
            foreach( $extra as $e ){
                $details=explode("#",$e);
                $name=$details;
                $details['extras_cookie']= $name;
            }
        }
        $details['date_cookie']= date("d/m/y");
        $details['time_cookie']= date("h:i:sa");
        $cookieValue=$details;
        
        
        setcookie( $cname, json_encode( $cookieValue, JSON_HEX_APOS ), $one_week );
    }
    
        
    if( isset( $_COOKIE['historyDetails'] ) ){
        $json=json_decode( urldecode( $_COOKIE[ $cname ] ) );
        printf('<pre>%s</pre>',print_r($json,true));
    }
    
?>

这会产生一个像这样的 cookie 字符串:

%7B%220%22%3A%22cold%22%2C%221%22%3A%22white%22%2C%222%22%3A%22fun%22%2C%22extras_cookie%22%3A%5B%22cold%22%2C%22white%22%2C%22fun%22%5D%2C%22date_cookie%22%3A%2220%5C%2F03%5C%2F21%22%2C%22time_cookie%22%3A%2201%3A10%3A27pm%22%7D

但是当使用显示的代码解码时会产生:

stdClass Object
(
    [0] => cold
    [1] => white
    [2] => fun
    [extras_cookie] => Array
        (
            [0] => cold
            [1] => white
            [2] => fun
        )

    [date_cookie] => 20/03/21
    [time_cookie] => 01:09:25pm
)

从那里您应该会发现从 cookie 访问项目很容易:

echo $json->extras_cookie[0], $json->date_cookie; //etc

推荐阅读