How to set up MySQL-based PHP sessions using the Stanford Web Application Toolkit

From Web Services Wiki

Jump to: navigation, search



Your application makes use of PHP's session mechanism, but it loses session information between requests. Since Stanford has multiple web servers, different requests may be directed to different servers, and session information is often lost as a result.


Use a MySQL-based storage mechanism for your sessions to ensure that session information does not get lost between requests.

Connect to the database

Read How to configure and access MySQL using the Stanford Web Application Toolkit for more information about accessing MySQL through the toolkit. Please note that only groups, departments, and services have access to MySQL at this time. Individual user accounts are not supported.

Using a configuration file

Using a configuration file to enable MySQL-based sessions using SWAT is extremely simple. Set up a configuration file (or edit an existing one) containing the following snippet and enter your own database login information.

# StanfordApp YAML configuration file
  name: my_database
  username: my_username
  password: my_password
  use encryption: no
  use mysql sessions: yes

Initialize your StanfordApp object using this configuration file. MySQL-based sessions will automatically be configured for you.

// Include StanfordApp
// Initialize StanfordApp
$app = new StanfordApp("my_config.yaml");
// MySQL-based sessions are automatically enabled

Manual configuration

Manually configuring MySQL-based sessions is also simple. Simply configure StanfordDatabase and then call setup_mysql_sessions.

// Include StanfordDatabase
// Initialize StanfordDatabase
$db = new StanfordDatabase();
// Set up the database
// Enable MySQL-based sessions
if($db->setup_mysql_sessions() == true) {
  // MySQL-based sessions are now enabled


The test script failed. What should I do?

Make sure that your database login information is correct. Try logging into phpMyAdmin or through a shell to verify. If the script still doesn't work, make sure that your database account has sufficient access to create a new table. In the absence of an existing table called php_sessions, the toolkit attempts to create one automatically. You or an administrator with privileged access to the database may create the table manually in an attempt to remedy the problem:

CREATE TABLE php_sessions (
     sessionid varchar(40) BINARY NOT NULL DEFAULT '',
     expiry int(10) UNSIGNED NOT NULL DEFAULT '0',
     value text NOT NULL,
     PRIMARY KEY  (sessionid)
 ) TYPE=MyISAM COMMENT='Sessions';

Verifying the change

Call the using_mysql_sessions function to verify that MySQL-based sessions have been configured.

// First, initialize and configure StanfordDatabase
$db = new StanfordDatabase("username", "password", "database");
// Set up MySQL-based sessions
// Check status
if($db->using_mysql_sessions() == true) {
  echo "<p>Success!</p>";
else {
  echo "<p>Failure</p>";
Personal tools