• I have a non-WP php application that requires _SESSION and I’m passing info from WP to that application via _SESSION. I’m issuing the start_session in WP via the following:

    add_action(‘init’, ‘register_my_session’);
    function register_my_session(){
    if( ! session_id() ) {
    session_start();
    }
    }

    Everything seems to work great with the exception of one thing. When I look at WP Tools > Site Health I see an error

    **The REST API encountered an error
    The REST API is one way WordPress, and other applications, communicate with the server. One example is the block editor screen, which relies on this to display, and save, your posts and pages.

    The REST API request failed due to an error.
    Error: cURL error 28: Operation timed out after 10001 milliseconds with 0 bytes received (http_request_failed)

    **

    If I remove just the session_start() call then the error goes away (but of course, I don’t have $_SESSION data available.)

    I’ve tried moving session_start to wp-config and have also tried moving it earlier / later in the wp hooks firing order, but so far no luck.

    Can anyone provide some insight as to why session_start would cause a REST error, and ideas on how to properly call session_start to get access to $_SESSION?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Time out errors typically mean an infinite loop was created somewhere. Not sure how that would happen when starting a session but the REST request is starting an unwanted session for the REST request despite checking session_id(). Try also checking if REST_REQUEST constant is defined (from the “init” callabck) before starting a session.

    Thread Starter Norm Sash

    (@normsash)

    Thanks for the help. Also, someone pointed me to this issue tracker https://core.trac.www.ads-software.com/ticket/47320.

    It does sound like the same issue that I’m having. Unfortunately it seems that the WP core is basically a ‘won’t fix’, other than modifying the error message that is displayed to state that REST may be affected by a session_start.

    I do see the possible use of session_write_close(). That might take some time to refactor the code to incorporate. Right now I can open the session before any http output and then use $_SESSION as needed. With this change I think I’ll have to open/close session every time I access it, but I also have to make sure that no http output has been done.

    I’ll also look into REST_REQUEST and see if that will help me work around the issue.

    Thread Starter Norm Sash

    (@normsash)

    Just for the record, I put the following code in the plugin and, so far, it seems to be working. Got rid of the REST API error…

    add_action('init', 'start_my_session');
    function start_my_session() {
    	if (session_status() == PHP_SESSION_NONE) {
    		session_start();
    	}
    }
    
    add_action('wp_loaded', 'close_my_session', 30);
    function close_my_session() {
    	if (session_status() == PHP_SESSION_ACTIVE) {
    		session_write_close();;
    	}
    }
    
    $Matheus

    (@matheusfastcom)

    @normsash
    Your code solved a similar problem that I had with a custom plugin.

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘WordPress site health REST error after calling start_session’ is closed to new replies.