Organizational Research By

Surprising Reserch Topic

calculate Cookie Size in PHP


I wanted to read the cookie and calculate its length on server side using php, but can't find any direct method to do so. So how to achieve this task ?

asked May 28, 2015 in PHP by rajesh
0 votes
38 views



Related Hot Questions

3 Answers

0 votes

what about this ?

setcookie("user", "Dino babu kannampuzha", time() + 3600);

if (isset($_COOKIE["user"])) {
  $data = $_COOKIE["user"];
  $serialized_data = serialize($data);
  $size = strlen($serialized_data);
  echo 'Length : ' . strlen($data);
  echo "
"
; echo 'Size : ' . ($size * 8 / 1024) . ' Kb'; }

// Output

Length : 21
Size : 0.232 Kb
answered May 28, 2015 by rajesh
0 votes

To get the raw cookies and their length:

$rawCookies = isset($_SERVER['HTTP_COOKIE']) ? $_SERVER['HTTP_COOKIE'] : null;
$rawLength  = strlen($rawCookies);
echo $rawLength;
answered May 28, 2015 by rajesh
0 votes

Not sure if this is what you want but you can try this

$start_memory = memory_get_usage();
$cookie = $_COOKIE['YourCookie'];
echo memory_get_usage() - $start_memory-PHP_INT_SIZE * 8;


    php
    setcookie("TestCookie", '10');
    $start = memory_get_usage();
    $cookie = $_COOKIE['TestCookie'];
    echo memory_get_usage() - $start;  //116 bytes   
  ?>

 

answered May 28, 2015 by rajesh

...