Maximum Size of a PHP Session

Maximum size of a PHP session

You can store as much data as you like within in sessions. All sessions are stored on the server. The only limits you can reach is the maximum memory a script can consume at one time, which by default is 128MB.

(Similar answers: Ideal PHP Session Size? - some useful comments)

Are there limits for session variables?

As @Thariama said, there's no limit on the number of variables; also, there's no limit on the amount of data you can store in a session (I've seen sessions tens of MB in size).

As the size of a session gets larger, you'll run into various quirks though: PHP 5 deserializes the whole session into memory at session_start() (using the default session handler - you can make you own solution, of course); with a 20 MB session and 50 concurrent users, your scripts start to be severely limited by disk access speeds (a.k.a. "script startup is slow as molasses" - the sessions alone would be hogging a GB of RAM); in the end, we dedicated a box to keep as many sessions as possible in its RAM, and the frontend boxes accessed them over NFS (although it helped in our case, this may be overkill for you).

Note that for many concurrent users and session storage on disk, the number of session temporary files may cause problems with filesystem limits (e.g. how many files can be in one directory before you run into problems with stat() performance), or other limits (we once found the hard way that a box was configured to only allow 4096 open files at the same time). None of this is really session-specific, but can be triggered by session handling.

Is there a maximum number of PHP sessions?

Off the top of my head, there should be several limits:

  • The random id generated for each session is of a fixed length, ergo there are only a limited number of random ids available. (I don't know whether PHP will increase the length of the id if it exhausts the number of available ones, I don't think so.) But, that number is ridiculously large, and will likely by far exceed the
  • number of files which can be stored in a single directory, which is limited by the filesystem. Since all sessions are stored as files by default in a single directory, you'll reach this eventually.
  • The size of the disk on which the session data is stored.

I don't think there are any other hard limits on the number of sessions. However, all these factors are much larger than 1000. 1000+ sessions can still be perfectly handled by standard PHP file based sessions. If you notice that is getting a problem, you can exchange the session backend easily. There are pluggable session handlers for memcached or other memory or database based storage systems. You can easily write your own sessions handlers to do whatever you want in any scalable form you need. You can still keep using the standard PHP session functions in your code.

What can be the maximum size for the $_SESSION?

If you have to do this task in stages (and there's a couple of suggestions here to improve the way you do things in a single pass), don't hold the csv file in $_SESSION... that's pointless overhead, because you already have the csv file on disk anyway, and it's just adding a lot of serialization/unserialization overhead to the process as the session data is written.

You're processing the CSV records one at a time, so keep a count of how many you've successfully processed in $_SESSION. If the script times out or barfs, then restart and read how many you've already processed so you know where in the file to restart.

Codeigniter session size limit

To further clarify my comment above, when you elect to save the session data in a database, CodeIgniter doesn't set a cookie (other than the session id of course) but saves all of the information that it would have set in a cookie in your database.

If you have a look at the sess_write in the Session class located in ./system/libraries/, if you have enabled the use of a database, you'll see that it serializes the data using serialize and saves it directly to the database. There is no restriction on length imposed by CodeIgniter when saving to a database.

For your convenience, here's a link to the source code: https://bitbucket.org/ellislab/codeigniter/src/fe2247a927ab/system/libraries/Session.php#cl-252.

The only restriction is set by the field you chose to use to save the data in your database. For more information on the data type storage requirements of MySQL, read this.

php session and cookie values maximum length

$_SESSION you can save unlimited amount of data, no real limit for that...

$_COOKIE is I guess under 4000 bytes...(Includes date, name, value, expiry)

Whats the maximum number of php session files in session.save_path

As far as I know, there's no specific hard-limit on the PHP side. However, most file systems perform badly when they have huge directory entries.

You possibly want to split your session files in subdirectories, as the manual explains:

session.save_path string

session.save_path defines the argument
which is passed to the save handler. If you choose the default files
handler, this is the path where the files are created. See also
session_save_path().

There is an optional N argument to this directive that determines the number of directory levels your session files will be spread
around in. For example, setting to '5;/tmp' may end up creating a
session file and location like
/tmp/4/b/1/e/3/sess_4b1e384ad74619bd212e236e52a5a174If
.

Beware that you first need to create the directory tree yourself. It shouldn't be difficult to write a PHP script to do so.



Related Topics



Leave a reply



Submit