使用 .htaccess 文件或修改服务器配置以使用 mod_rewrite,这应该可以作为纯 PHP 解决方案正常工作:
$sname = session_name(); // Get the name of the session cookie.
if ( !isset($_SESSION) ) session_start(); // If a session has not been started then start one.
$sid = ( isset($_COOKIE[$sname]) ) ? session_id($_COOKIE[$sname]) : session_id(); // Get an encrypted session identifier.
setcookie($sname, $sid, time()+86400, "/", ".example.com", false, true); // Set the .example.com cookie so we can work across all sub-domains.
将 .example.com 更改为 .example.com 中的任何域名。domain.tld格式没有子域,但包括开头的句点。这应该设置一个适用于您域的所有子域的 cookie。