#native_company# #native_desc#

Custom Session Handlers in PHP4 Page 2

By Ying Zhang
on July 30, 2000

Session Handler Functions

Any custom session handler we write will have to provide 6 basic functions,
they get called by the PHP4 session handler so you do not need to worry about
calling them yourself. The nice thing about all this is that your custom
session handler functions are completely transparent, so you can change them
without affec

The functions are are:
  1. sess_open($sess_path, $session_name);
    This function is called by the session handler to initialize
    things. The two parameters passed to it are $sess_path,
    which corresponds to the session.save_path setting in your
    php.ini file, and $session_name which corresponds to
    the session.name setting in your php.ini file. More on
    the duties of this function when we go into specific examples.
  2. sess_close();
    This function is called when to when the page is finished executing
    and the session handler needs to close things off. (Note, do not
    confuse this with sess_destroy(), which is called to kill the
  3. sess_read($key);
    This function is called by the session handler to read the data
    associated with a given session key ($key). This function must
    retrieve and return the session data for the session identified
    by $key. (Note: you do not have to worry about serializing and
    unserializing data, if you do not know what this means, then
    don’t worry about it).
  4. sess_write($key, $val);
    This function is called when the session handler has session data
    to save, which usually happens at the end of your script. It is
    responsible for saving the session data in such a way that it can
    be retrieved later on by sess_read($key).
  5. sess_destroy($key);
    This function is called when a session is destroyed. It is
    responsible for deleting the session and cleaning things up.
  6. sess_gc($maxlifetime);
    This function is responsible for garbage collection. In the case
    of session handling, it is responsible for deleting old, stale
    sessions that are hanging around. The session handler will call
    this every now and then.
So now we know what functions we have to provide, they don’t necessarily
have to be given those names but they have to accept those parameters
(whether you need them or not).

Comment and Contribute

Your comment has been submitted and is pending approval.


Ying Zhang