Unable to get session data

hello world,
i am new to wordpress plugin development. below functions are located in my plugin folder. the issue is that i am unable to get session data on theme pages. however, the session are being set, but the data is not displayed at frontend. when i refresh the page the data is displayed. what could be the reason? please help. Thank You

i tried fixing with this but it didnt work.

function register_session()
{ 
	if( !session_id() )
	{ 
		session_start(); 
	} 
} 
add_action('init', 'register_session');
function setSession($sessionKeys = array(),$value = NULL) {
   if(isset($sessionKeys[0]) && isset($sessionKeys[1])) {
        $_SESSION['main_entry'][$sessionKeys[0]][$sessionKeys[1]] = $value;
    } else if(isset($sessionKeys[0])){
        $_SESSION['main_entry'][$sessionKeys[0]] = $value;
    } else {
      return false;
    }
    session_write_close();
    sleep(1);
    write_log($_SESSION['main_entry'], 'MAIN ENTRY');
}

function getSession($sessionKeys = array(), $default = NULL) {
 if(isset($sessionKeys[0]) && isset($sessionKeys[1])) {
      return isset($_SESSION['main_entry'][$sessionKeys[0]][$sessionKeys[1]]) ? $_SESSION['main_entry'][$sessionKeys[0]][$sessionKeys[1]] : $default;
  } else if(isset($sessionKeys[0])) {
      return isset($_SESSION['main_entry'][$sessionKeys[0]]) ? $_SESSION['main_entry'][$sessionKeys[0]] : $default;
  } else {
      return $_SESSION['main_entry'];
  }
}

//unable to get session data on view pages.. even if session data is being set
getSession(array('order'));

I’m not able to look up resources right now but you might want to check out wordpress.org and sessions. I seem to remember running into a problem a few years ago and finding out that WordPress is stateless and it is not straightforward getting sessions to work.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.