Created
March 17, 2024 01:05
-
-
Save xnau/21cb290bc8f0885e8b770bac9db434f4 to your computer and use it in GitHub Desktop.
WordPress MU plugin for initializing a php session early in the load cycle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
* Plugin Name: Participants Database Session initializer | |
* Plugin URI: https://wordpress.org/plugins/participants-database/ | |
* Description: Attempts to initate a php session before headers are sent | |
* Author: xnau webdesign | |
* Version: 0.1 | |
* Author URI: http://xnau.com | |
*/ | |
class PDb_Session_Init { | |
public function __construct() | |
{ | |
if ( ! headers_sent() && session_status() !== PHP_SESSION_ACTIVE ) | |
{ | |
$this->start(); | |
} | |
} | |
private function start() | |
{ | |
$started_here = false; | |
if ( session_start() ) | |
{ | |
$started_here = true; | |
} | |
$phpcookie = ini_get( 'session.name' ); | |
if ( empty( $phpcookie ) ) | |
{ | |
$phpcookie = 'PHPSESSID'; | |
} | |
$sessid = session_id(); | |
setcookie( $phpcookie, $sessid, 0, '/' ); | |
if ( $started_here ) | |
{ | |
session_write_close(); | |
} | |
} | |
} | |
new PDb_Session_Init(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
On some WordPress installs, php sessions can't be started by a regular plugin because other plugins or a theme has sent out the php headers, preventing a session from getting started. This is because starting a session requires adding info to the header, but it can't do that after the headers have been sent.
This MU plugin attempts to avoid this problem by loading and activating the session before any other plugins or the theme can be loaded.
This should be installed in the mu-plugins directory of your WordPress.