I created a PHP script that reads the data from mysql database and returns an output in json format. The script works fine, but now I want to save the output in my cookie. When I add the function setcookie("Messages",base64_encode(serialize($messages)),157680000000); I have a Server Error 500 : Premature end of script headers.
How can I save the array $messages in the cookie?
----------------- PHP CODE -------------------
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate( "D, d M Y H:i:s" ) . "GMT");
header("Cache-Control: no-cache, no-store, must-revalidate");
header("Pragma: no-cache");
header("Content-type: application/json");
include_once("include/common.php");
if (empty($uname)){
$uname = (!empty($_GET['u'])) ? $_GET['u'] : "";
$uname = (!empty($_POST['u'])) ? $_POST['u'] : $uname;
}
if (empty($limit)){
$limit = (!empty($_GET['limit'])) ? $_GET['limit'] : "";
$limit = (!empty($_POST['limit'])) ? $_POST['limit'] : $limit;
}
$isonline = is_online();
$user_obj = new User();
$user_info = $user_obj->getUserInfo($uname);
$msg_obj = new Message();
$msg_array = $msg_obj->getMessages($user_info[_USERID_COLUMN],$limit);
$messages = array();
$i = 0;
[...]
foreach( $msg_array as $id => $val ){
$messages[$i]['id_message']=$val['id_message'];
$messages[$i]['user']=$val['user'];
$messages[$i]['user_image']=$val['user_image'];
$messages[$i]['posted_time']=time2str($val['posted_time']);
if($options['link'] == "off"){
$messages[$i]['message']=htmlspecialchars_decode(cleaner($val['message']));
}else{
$messages[$i]['message']=htmlspecialchars_decode($val['message']);
}
$messages[$i]['mediatype']=$val['mediatype'];
$messages[$i]['mediaurl']=$val['mediaurl'];
$i++;
}
array_reverse($messages);
if(ceckCookie()){
setcookie("Messages",base64_encode(serialize($messages)),157680000000);
}
if( !$isonline && !empty($_COOKIE["Messages"]) ){
echo($_COOKIE["Messages"]);
}else{
echo(json_encode($messages));
}
is the data you are trying to save is huge ??
$_COOKIE
can store under 4000 bytes...(Includes date, name, value, expiry) ..