• Hi,

    I’m using a code to randomise my posts and it works a treat, but it’s causing two critical issues:

    An active PHP session was detected
    The REST API encountered an error

    I know it’s the code and not a plugin, for if I remove the code these critical errors disappear.

    I tried changing the session_start():
    ` session_start( [
    ‘read_and_close’ => true,
    ] );
    }’

    However, this stopped the randomising of posts upon pagination. Does anyone have any ideas what I could please try to fix the health issues caused by session_start(), while still having the code work?

    I’ve not shared the full code as someone kindly did this for us as a one-off, I feel it would be rude to share what they wrote without their permission.

    Thanks for any help.

Viewing 8 replies - 1 through 8 (of 8 total)
  • Hi @oriver

    It’s difficult to help you debug the issue without seeing the custom code, but I understand your predicament.

    The message you have referenced “An active PHP session was detected” indicates that the custom code needs to close the session using session_write_close() to prevent the Site Health error being reported.

    You could try adding this function at the end of your custom code, to see if it resolves the issue, but I can’t be sure this will fix it for you.

    Thread Starter oriver

    (@oriver)

    Hi @psykro

    Thank you for helping. I just tried this and unfortunately it causes the same problem of stopping the randomising of posts on pagination, and instead I get duplicated posts.

    Sorry I can’t share the full code, feel it would be wrong when I didn’t write it.

    Thank you for understanding.

    Hi @oriver I do understand. However, without seeing the code I won’t be able to help further. It sounds like the randomisation of the posts relies on that session.

    Thread Starter oriver

    (@oriver)

    Hi @psykro

    As I can’t get hold of them to ask, I don’t think they’ll mind me sharing the original template for the code, which worked pretty much in the same way, and causes the same two critical errors.

    session_start();
    
    add_filter('posts_orderby', 'edit_posts_orderby');
    
    function edit_posts_orderby($orderby_statement) {
    
    $seed = $_SESSION['seed'];
    if (empty($seed)) {
      $seed = rand();
      $_SESSION['seed'] = $seed;
    }
    
    $orderby_statement = 'RAND('.$seed.')';
    return $orderby_statement;
    }"

    Thanks @oriver that does help.

    Can you let me know how this is implemented in the WordPress site, is it via a theme or a custom plugin?

    Thread Starter oriver

    (@oriver)

    Hi @psykro

    The code being used is via Code Snippets, thank you

    • This reply was modified 4 years, 1 month ago by oriver.

    Thanks @oriver I did a search on the plugin repo for Code Snippets and there were a few results, perhaps you could point me to the correct one?

    Thread Starter oriver

    (@oriver)

    Hi @psykro

    Sorry for the very late reply, I didn’t see a notification of your reply. The code snippet used is: https://ww.wp.xz.cn/plugins/code-snippets/

    I had found that the following amendment fixed the errors, while the code seemed to still work on the staging site with no duplicates (but a different story for the live site). Having said this the change to the session_start() is causing duplicates on both sites now.

    However, this code might be helpful to others in order to remove the critical PHP errors caused by session_start(), while most codes should still work:
    ‘session_start([
    ‘cookie_lifetime’ => 86400,
    ‘read_and_close’ => true,
    ]);’

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

The topic ‘Two critical errors caused by session_start(). Tried read & close stops code’ is closed to new replies.