tags:

views:

242

answers:

3

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

+5  A: 
$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.

Charles
+1 spot on, *and* very quick.
karim79
I am curious, I thought the size limit of the session was based soley on your memory, is there other factors? I ask because I store an insane amount of data in user sessions
jasondavis
I suppose this works because strlen gives number of characters. And each character is 1kb.. correct me if im wrong.
Rahul
@Rahul - each character is one *byte*.
karim79
I don't think there is an actual size *limit* to the session, but it will take longer during session creation to unserialize if there's a large amount of data.
Charles
I stand corrected
Rahul
There will be two factors limiting session size: the amount of memory available to the individual script, and the amount of available space used to store the serialised session between requests (typically a temp file on disk in the case of Apache - although *nix will probably cache those in free memory). It will be the former (php memory limit) that will run out first
iAn
+4  A: 

If you are using Apache, take a look into your APACHE_ROOT/tmp folder and look for files named sess_***********.

Otherwise take the script from here and call it using array_size($_SESSION). This might differ slightly from the exact value (depending on compression/optimizations done by your PHP module).

Marcel J.
Keep in mind that some configurations may store sessions in places other than /tmp. You'll need to examine your PHP configuration to be sure.
Charles
A: 

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());
GZipp