skip to Main Content

The current session ID keeps changing on every request.
I have a file called session.php.

It has this php code in it:

<?php

session_start();

$s_id = session_id();
print_r($s_id);

?>

Everytime I navigate to a other page this ID changes. How is this possible?

I disabled all plugins and has only this in my functions.php

<?php

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

Everytime I reload datum.php it shows a different session id. On other WordPress sites it doesn’t.

2

Answers


  1. First create a session_id. Then store it into session variable.
    Next time check whether the session_id session value exits or not.
    If exits then get the previous session id value from session.
    otherwise create a new session id.

    After complete your tasks (example logout) delete the session variable.

    Login or Signup to reply.
  2. Method of start session in WordPress is something like below so check by updating your code first

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

    Also if you want to use session for woocommerce then there also existing sessions for that as well which you may use it like

    WC()->session->set( 'name_for_your_data' , $data );
    WC()->session->get( 'name_for_your_data' );
    

    More function and information about woocommerce session class here https://docs.woocommerce.com/wc-apidocs/class-WC_Session.html

    Hope this help you to resolve your problem with session

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search