DevinVinson / WordPress-Plugin-Boilerplate

[WordPress] A foundation for WordPress Plugin Development that aims to provide a clear and consistent guide for building your plugins.
http://wppb.io
7.62k stars 2.25k forks source link

PHP Session #574

Open PetteriSeoka opened 2 years ago

PetteriSeoka commented 2 years ago

Hi,

I've tried to create a multiform but got stuck in PHP sessions. The session doesn't start when added

includes/ $this->loader->add_action( 'wp_ajax_public_start_form_session', $plugin_public, 'start_form_session' ); $this->loader->add_action( 'wp_ajax_nopriv_start_form_session', $plugin_public, 'start_form_session' );

public/ public function start_form_session() { if (session_status() === PHP_SESSION_NONE) { session_start(); } }

Anybody that can point me to the right direction? :)

kktsvetkov commented 4 months ago

@PetteriSeoka WordPress does not use PHP sessions. It looks like you want to start the session yourself here. The tricky part is that the PHP sessions need to send a few HTTP headers, and if the website has already pushed content out and has sent the headers, it just is not going to work. In other words, there isn't really a way to start the session late into the script loading, and instead, you got to do it really early, like for example on top of your plugin, like this:

if (PHP_SESSION_ACTIVE !== session_status())
{
        session_start();
}

One important takeaway with the default PHP sessions is that they are file-based. That works on with small sites hosted on just one machine. If you are using a multi-server setup like a web farm, the file-based sessions are not going to work -- as the files for the session might end up on a different server.