Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can echo size of SESSION?

Tags:

php

session

I dont know if my sessions are too big. Is there a way to see the size of a session. Thanks, Rahul

like image 448
Rahul Avatar asked Sep 06 '09 18:09

Rahul


2 Answers

$size_of_session_estimate = strlen( serialize( $_SESSION ) );

Now, this is just an estimate, as the serialize handler is not used to serialize sessions, but it also won't be too far off.

That being said, unless you're storing a silly amount of data in the session, you probably don't need to worry about this.

like image 151
Charles Avatar answered Sep 30 '22 01:09

Charles


This:

echo strlen(session_encode());

will give you the amount of disk space used by $_SESSION (assuming session.save_handler is the default value files), since session_encode() returns a string identical to the string stored in the session file.

It will also give a better indication of the amount of memory used, since session_encode() adds less metadata than serialize().

On a default Apache setup, you can see the session data as stored on disk with:

session_write_close();
echo file_get_contents(sys_get_temp_dir() . 'sess_' . session_id());
like image 44
GZipp Avatar answered Sep 30 '22 01:09

GZipp