Support » Developing with WordPress » Problems with sessions

  • I created a plugin in which sessions are required. Therefore I created this piece of code to make it work:

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

    Then I uploaded the plugin to the WP plugin repository and this is what they told me:

    ## Forcing PHP Sessions on all pages

    Using session_start() or ob_start() in your plugin without having it encapsulated in a function means that it will run on every single page load when your plugin is active. That means you’ve just broken caching for everyone because the way PHP Sessions work is they indicate the visitor using sessions is unique and should have a non-cached view of the website. This specifically breaks Varnish type caching.

    Please remove this from your plugin, or put it only in the function that absolutely must have it.

    However, if I put “session_start()” inside a function, I get this error: “Warning: session_start(): Cannot send session cache limiter – headers already sent”.

    What should I do?

    • This topic was modified 2 months, 2 weeks ago by  oligalma.
    • This topic was modified 2 months, 2 weeks ago by  oligalma.
    • This topic was modified 2 months, 2 weeks ago by  oligalma.
    • This topic was modified 2 months, 2 weeks ago by  oligalma.
    • This topic was modified 2 months, 2 weeks ago by  oligalma.
Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • You must be logged in to reply to this topic.