简体   繁体   中英

Can echo size of SESSION?

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

$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.

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).

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM