Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

calculate Cookie Size in PHP

Tags:

php

cookies

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 ?

like image 865
blackhole Avatar asked Oct 25 '25 07:10

blackhole


2 Answers

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 "<br/>";
  echo 'Size : ' . ($size * 8 / 1024) . ' Kb';
}

// Output

Length : 21
Size : 0.232 Kb
like image 90
Dino Babu Avatar answered Oct 27 '25 21:10

Dino Babu


To get the raw cookies and their length:

$rawCookies = isset($_SERVER['HTTP_COOKIE']) ? $_SERVER['HTTP_COOKIE'] : null;
$rawLength  = strlen($rawCookies);
echo $rawLength;
like image 36
Sverri M. Olsen Avatar answered Oct 27 '25 22:10

Sverri M. Olsen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!