Title: Patch to start PHP session only when needed
Last modified: June 5, 2023

---

# Patch to start PHP session only when needed

 *  [kkatpcc](https://wordpress.org/support/users/kkatpcc/)
 * (@kkatpcc)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/patch-to-start-php-session-only-when-needed/)
 * This is a follow up to [https://wordpress.org/support/topic/php-session-always-started-even-when-not-needed/](https://wordpress.org/support/topic/php-session-always-started-even-when-not-needed/)
   since that topic is no longer allowing new replies.
 * The following changes to **classes/wp_cassify_plugin.php** is one minimal way
   to make the PHP session only start when actually needed.
 * Update wp_cassify_session_start() as follows, with the changes in bold:
 *     ```wp-block-code
       	public function wp_cassify_session_start( $force = false ) {
   
       		if ( $force || isset( $_COOKIE[ session_name() ] ) ) {
       			if(! session_id() ) {
       				session_start();
       			}
       		}
       	}
       ```
   
 * And then update wp_cassify_grab_service_ticket() as follows, again with the changes
   in bold:
 *     ```wp-block-code
       		if ( (! is_user_logged_in() ) || (! is_user_member_of_blog() ) ) {
       			if (! empty( $service_ticket ) ) {
   
       				// Ensure session is started
       				$this->wp_cassify_session_start( true );
   
       				// Retrieve configuration options from database
       ```
   
 * Note that I’ve only done rudimentary verification of these changes, but it seems
   to work as expected.

Viewing 6 replies - 1 through 6 (of 6 total)

 *  [Jason Partyka](https://wordpress.org/support/users/partyka/)
 * (@partyka)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/patch-to-start-php-session-only-when-needed/#post-16807118)
 * Alternately, can just adjust this block of code to see how authentication is 
   being requested:
 *     ```wp-block-code
       	/**
       	 * Start the php session inside the plugin because session is needed to store callback url.
       	 */	 
       	public function wp_cassify_session_start() {
   
       		if(! session_id() ) {
       			global $wp;
       			$gateway_mode = WP_Cassify_Utils::wp_cassify_get_option( $this->wp_cassify_network_activated, 'wp_cassify_enable_gateway_mode' );
       			if ($gateway_mode && $wp->request == "/login") {
       				session_start();
       			}
       		}
       	}
       ```
   
 *  [Jason Partyka](https://wordpress.org/support/users/partyka/)
 * (@partyka)
 * [2 years, 11 months ago](https://wordpress.org/support/topic/patch-to-start-php-session-only-when-needed/#post-16807384)
 * Forgot to add my adjustment:
 *     ```wp-block-code
       		if(!session_id() && !headers_sent() && (get_current_user_id() > 0)) {
       			session_start();
       			}
       		}
       ```
   
 * Basically adding a check to see if a user is logged in.
 *  Plugin Author [Alain-Aymerick FRANCOIS](https://wordpress.org/support/users/aaf017/)
 * (@aaf017)
 * [2 years, 7 months ago](https://wordpress.org/support/topic/patch-to-start-php-session-only-when-needed/#post-17099754)
 * Hi,
 * Hello,
 * I’m sorry but I have a lot of work at the moment. I haven’t had time to update
   the plugin. Nevertheless, I’m very interested in integrating your patch into 
   the plugin. About starting a session only if necessary.
   Can you send me a patch
   of all the modified files, indicating exactly where the changes have been made?
   I’d be happy to incorporate your work to help the plugin progress.I’ll mention
   your name/pseudo in the README file.
 * Best regards.
 *  Plugin Author [Alain-Aymerick FRANCOIS](https://wordpress.org/support/users/aaf017/)
 * (@aaf017)
 * [2 years, 7 months ago](https://wordpress.org/support/topic/patch-to-start-php-session-only-when-needed/#post-17099760)
 * Just to finish. Have you tested your patch with Gateway mode?
 * => User already connected from another service and automatic CAS connection when
   coming to WordPress.
 *  Thread Starter [kkatpcc](https://wordpress.org/support/users/kkatpcc/)
 * (@kkatpcc)
 * [2 years, 7 months ago](https://wordpress.org/support/topic/patch-to-start-php-session-only-when-needed/#post-17111693)
 * > Can you send me a patch of all the modified files, indicating exactly where
   > the changes have been made?
 * The lack of plugin updates — for proper PHP 8 support, and in general — have 
   forced me to look for a different plugin/solution. Thus, I can’t justify the 
   effort of submitting a formal patch at this point.
 * However, my first comment basically serves this purpose. Please feel free to 
   work that logic into your plugin as you see fit.
 * > Have you tested your patch with Gateway mode?
 * No, I do not use Gateway mode.
 * Overall, the comments from [@partyka](https://wordpress.org/support/users/partyka/)
   may indicate a better approach.
 *  [Charles Fulton](https://wordpress.org/support/users/mackensen/)
 * (@mackensen)
 * [2 years, 5 months ago](https://wordpress.org/support/topic/patch-to-start-php-session-only-when-needed/#post-17260888)
 * I can confirm that [@kkatpcc](https://wordpress.org/support/users/kkatpcc/)’s
   fix resolves the session issues that we reported earlier. We’ve published a git
   clone of the SVN repository containing the fix, the PHP 8 fixes that Jesse Loesberg
   authored, and a few local changes: [https://github.com/LafColITS/wp-cassify](https://github.com/LafColITS/wp-cassify).
   We don’t use gateway mode either so I can’t speak to whether it’s affected.

Viewing 6 replies - 1 through 6 (of 6 total)

The topic ‘Patch to start PHP session only when needed’ is closed to new replies.

 * ![](https://ps.w.org/wp-cassify/assets/icon-256x256.png?rev=3112582)
 * [WP Cassify](https://wordpress.org/plugins/wp-cassify/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/wp-cassify/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/wp-cassify/)
 * [Active Topics](https://wordpress.org/support/plugin/wp-cassify/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/wp-cassify/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/wp-cassify/reviews/)

 * 6 replies
 * 4 participants
 * Last reply from: [Charles Fulton](https://wordpress.org/support/users/mackensen/)
 * Last activity: [2 years, 5 months ago](https://wordpress.org/support/topic/patch-to-start-php-session-only-when-needed/#post-17260888)
 * Status: not resolved