Multiple Sessions Running Consecutively with PHP
While implementing a inventory control system using PHP, I realized that I needed a way to allow my users to run multiple copies of the same program at the same time. To accomplish this, I needed a unique identifier for each session.
Each session can be assigned a unique session name using the “session_name” function. There are two steps to creating a unique session name.
1. If the variable “$mysession” does not exist, create a session name
using the program name and timestamp. I originally used “mktime”, but changed to “microtime” to avoid duplicate timestamps.
//Start a PHP session to preserve variables. if ( empty($mysession) ) { $micro = microtime(); $micro = str_replace(" ","",$micro); // strip out the blanks $micro = str_replace(".","",$micro); // strip out the periods $mysession = "po_maint" . $micro; } session_name($mysession); session_start();
2. The variable “$mysession” must be passed from page to page. It
cannot be a session variable because this variable is being used before the session is activated. It cannot be a cookie because mutiple programs would
try to overwrite the “mysession” cookie. It can be passed as a hidden
field in each form or appended to the URL.