Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PHP Session #574

Open
PetteriSeoka opened this issue Feb 24, 2022 · 2 comments
Open

PHP Session #574

PetteriSeoka opened this issue Feb 24, 2022 · 2 comments

Comments

@PetteriSeoka
Copy link

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
Copy link

kktsvetkov commented Mar 25, 2024

@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.

@PetteriSeoka
Copy link
Author

Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants