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 ?


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


To get the raw cookies and their length:

$rawCookies = isset($_SERVER['HTTP_COOKIE']) ? $_SERVER['HTTP_COOKIE'] : null; $rawLength = strlen($rawCookies); echo $rawLength;




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 ?>


Refer from these resources:<br /><a href="http://extraconversion.com/data-storage/characters/characters-to-bytes.html" rel="nofollow">http://extraconversion.com/data-storage/characters/characters-to-bytes.html</a><br /><a href="https://stackoverflow.com/questions/7568949/measure-string-size-in-bytes-in-php" rel="nofollow">Measure string size in Bytes in php</a><br /><a href="https://mothereff.in/byte-counter" rel="nofollow">https://mothereff.in/byte-counter</a>

And my screenshot that get cookie size using Firebug. <a href="https://i.stack.imgur.com/pTcoC.jpg" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/pTcoC.jpg" data-original="https://i.stack.imgur.com/pTcoC.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

I use my code from @Dino but change something such as strlen to mb_strlen to support unicode.<br /> It becomes:

$data = $_COOKIE['user']; $name = 'user'; if (!is_scalar($data)) { $data = serialize($data); } $size = mb_strlen($data)+mb_strlen($name); echo 'Cookie name length : ' . mb_strlen($name) . "<br>\n"; echo 'Cookie content length : ' . mb_strlen($data) . "<br>\n"; echo 'Cookie size : ~' . ($size) . ' Bytes<br>'."\n";

Which is almost close to the size appears in Firebug. I really don't understand why the size must (n*8/1024) if we refer from the resources above it just 1 character 1 byte except unicode so I have to use mb_strlen instead of strlen.


  • Hovercard not working correctly with the hover function. Any ideas?
  • Store array in cookie
  • Cookie multiple values
  • Losing session in Coldfusion
  • Radio button show hover between two radio button using jquery
  • Type mismatch: cannot convert from Connection to Connection
  • angularjs ng-bind-html html input part missing
  • Strings appear exact, but they do not match?
  • Zend Framework 2, Module Redirect
  • java.lang.IllegalStateException: getInputStream() has already been called for this request + Struts2
  • Why does PHP appear to evaluate this condition incorrectly?
  • Whats the best way of persisting data to Isolated Storage on Windows Phone 7?
  • Contact form problem - I do receive messages, but no contents (blank page)
  • How can I prevent the need to copy strings passed to a avr-gcc C++ constructor?
  • allocating memory to an array of string
  • SAVE attribute needed for Fortran variables when only the C_LOC address is returned to a C program?
  • Stop Bash Script if Hive Fails
  • How to add git credentials to the build so it would be able to be used within a shell code?
  • Change multiple background-images with jQuery
  • Is there a javascript serializer for JSON.Net?
  • Android screen density dpi vs ppi
  • DirectX11 ClearRenderTargetViewback with transparent buffer?
  • Change an a tag attribute in JavaScript based on screen width
  • Display Images one by one with next and previous functionality
  • Upload files with Ajax and Jquery
  • retrieve vertices with no linked edge in arangodb
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • embed rChart in Markdown
  • Change div Background jquery
  • apache spark aggregate function using min value
  • Is it possible to post an object from jquery to bottle.py?
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • To Get the radio button value in ruby on rails
  • java string with new operator and a literal