The term Session in web development refers to a general method of storing semi-permanent data, such as user logins, user interactions, ecommerce shopping carts, and many other examples. PHP has a system called $_SESSION, which is exceptionally simple to use, but has same major draw backs, including security issues, and intermittent support across server environments. A few months ago, Eric Mann introduced WP_Session; a new library that provides a session data storage system that has the simplicity of $_SESSIONs but without many of the drawbacks.
WP_Session allows you to store data in exactly the same way as you would with $_SESSION. For example, when using $_SESSION, you might do something like this:
1 2 3 4 5 6 7 8 9 10
$_SESSION['cart_items'] = array( array( 'item_id' => 43, 'item_name' => 'My Product Name' ), array( 'item_id' => 22, 'item_name' => 'My Second Product Name' ) );
$cart_items = ! empty( $_SESSION['cart_items'] ) ? $_SESSION['cart_items'] : false;
With WP_Session, you can perform the exact same type of actions with a nearly identical syntax:
1 2 3 4 5 6 7 8 9 10 11 12
global $wp_session; $wp_session['cart_items'] = array( array( 'item_id' => 43, 'item_name' => 'My Product Name' ), array( 'item_id' => 22, 'item_name' => 'My Second Product Name' ) );
$cart_items = ! empty( $wp_session['cart_items'] ) ? $wp_session['cart_items'] : false;
Eric has made the implementation of WP_Session extremely simple with his WP_Session Manager plugin, available for free from WordPress.org.
I recently used Eric’s WP_Session class in Easy Digital Downloads with excellent results. Because I wanted to integrate it directly into my plugin, without relying on the installation of a second plugin, I included the WP_Session library as part of EDD and then wrote a wrapper class to handle the setting and retrieval of session data. You can see my wrapper class on Github.
When I want to store some session data in Easy Digital Downloads, such as when a customer adds an item to the shopping cart, I simply do this:
1 2 3 4 5 6 7 8 9 10 11 12
$cart = array( array( 'item_id' => 43, 'item_name' => 'My Product Name' ), array( 'item_id' => 22, 'item_name' => 'My Second Product Name' ) ); EDD()->session->set( 'edd_cart', $cart );
And when I want to retrieve the session data:
EDD()->session->get( 'edd_cart' );
When Easy Digital Downloads was first written, it relied on $_SESSION for cart storage, as well as several other pieces of session data necessary for the plugin to operate. This resulted in a rather large number of support tickets from users that experienced problems due to the lack fo support for $_SESSION on their servers. By switching to WP_Session, we reduced the number of tickets related to session data problems by probably 90% or more.
Have you used WP_Session? What was your experience? I am personally a big advocate for it and would strongly encourage you to give it a test.
To read more about why Eric created WP_Session, and the types of problems it solves, read his original proposal.