Using PHP Sessions in WordPress to Enhance Site Functionality

By default, WordPress does not support the use of methods other than cookies to keep users logged in. This is a very good strategy to ensure the performance and page opening speed of your WordPress site. WordPress is focused on content management and publishing, and using cookies to keep users logged in is sufficient in most cases. But sometimes, we need to save some data between two or more page requests. At this time, using Session is very simple and convenient, such as usingWordPress development applicationWhen the user submits the form, we need to prompt the user with the result, which is a message flash (Flash).

Start a Session when WordPress initializes

Starting a Session in WordPress is actually very simple. You only need to add the following code to the theme’s functions.php or plug-in. The following code mounts the function that starts the session to the initialization hook of WordPress. We set the priority of the Action to 1 to ensure that we have started the Session before other functions are started.

add_action( 'init', function ()
{
   if ( ! session_id() ) {
      session_start();
   }
}, 1 );

Destroy session data when user logs in or out

As mentioned at the beginning of this article, WordPress does not use Session to save user login data, and certainly does not clean up user Session data when the user logs in or out. In order to prevent the user’s Session data from disappearing when the user logs out or logs in to another account, we need to destroy the Session data for the user when the user logs in and out. We use WordPress’s ‘wp-login’ and ‘wp-logout’ hooks to help users automatically destroy session data when logging in or logging out.

add_action('wp_logout', 'destroy_session');
add_action('wp_login', 'destroy_session');

function destroy_session() {
    session_destroy ();
}

Using Session to save and get data

After adding the above code, we can use PHP’s $_SESSION variable to add and obtain Session data.

Save data to Session:

$_SESSION['message'] = "保存数据成功";

Get data in Session:

if(isset($_SESSION['message'])) {
    $value = $_SESSION['message'];
} else {
    $value = '';
}

Save Session data in the MySQL database to avoid some problems with Session

By default, PHP will save the Session in a temporary file. When multiple requests need to access the Session, PHP will block user requests due to file locks, causing performance problems. Saving Session data in the database can avoid this problem. There is aSession pluginIt can help us achieve this transformation. We only need to install and enable this plug-in.

Of course, in addition to MySQL, we can also save the Session in Redis or Memcached. The method is also very simple. Just add the following code to functions.php.

// 保存 Session 到 redis 中
ini_set("session.save_handler","redis");
ini_set("session.save_path","tcp://127.0.0.1:6379");

// 保存 Session 到 Memcache 中
ini_set("session.save_handler","memcache");
ini_set("session.save_path","tcp://10.1.1.1:11211");

To sum up, although WordPress does not use Session by default, it is very simple to use Session in WordPress. In fact, some large WordPress plug-ins use Session to implement some functions, such as the popular WordPressE-commerce plugin WooCommerce, interested friends can study the following.

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *