Although, session_set_save_handler() has support for saving session data via other modes, This does not support a way for saving session data in COOKIES. This is required for a site with a huge concurrent request the recommended solution by session_set_save_handler() does not fit the load site is handling.
So, the other way around to do this is as below.
<?php
ob_start(); $sessCookieName = session_name();
$_SESSION = json_decode(base64_decode($_COOKIE[$sessCookieName]), true);
function echosess() {
echo $_SESSION['id'];
}
echosess();
$_SESSION['id'] = 1;
$op = ob_get_clean(); $encryptedData = base64_encode(json_encode($_SESSION));
setcookie($sessCookieName, $encryptedData, time() + (ini_get("session.gc_maxlifetime")), '/');
echo $op;
?>
If more security is required about the data of the session in cookie, then key based encryption/decryption can solve the issue.