970_90

Wednesday, 25 February 2015

How to store array in Cookie and retrieve array from cookie

How to store array in Cookie and retrieve array from cookie

How to store array in Cookie
We need to json_encode the array then save the data with setcookie function.
$arrayData=array(
    'web technology experts',
    'web technology experts Notes',
    'web technology experts Notes  php technology'    
);
setcookie('cookieData', json_encode($arrayData));


How to Retrieve array in Cookie
As we have encoded the array above, So we need to json_decode the array before use.
$arrayData = json_decode($_COOKIE['cookieData']);


How to set time in cookies
$arrayData=array(
    'web technology experts',
    'web technology experts Notes',
    'web technology experts Notes  php technology'    
);
setcookie('cookieData', json_encode($arrayData), time()+3600);// set time for 1 hour [1 hour=3600 seconds]


 


2 comments:

  1. How to create persistent cookie in php?

    To create persistent cookie you need to mention time, how log it should exist. For Example:
    setcookie( 'cookieName', 'CookieValue', strtotime( '+1 year' ) );

    ReplyDelete
  2. Thanks for sharing wonderful information about PHP. The post was really interesting to read.
    PHP Web Development Company in Indore

    ReplyDelete