There are situations, in which you need to implement a solution supporting periods in session names. E.g. I ran into a similar issue. I had to create a PHP page emulating the exact behavior of an ASP page to stay compatible with customer's external hardcoded frontend. In the given request header a cookie name ASP.NET_SessionId was present.
If someone else gets into similar constraints, here is a workaround.
After some research and testing, I came to the result, that the reason for the misbehavior is, that $_COOKIE does not contain the correct keys when there are special characters in the cookie name.
<?php session_start (['name' => 'ASP.NET_SessionId']); ?>
<html>
<head><title></title><meta charset="UTF-8"></head>
<body>
<pre><?php
//echo implode("\n", apache_request_headers());
foreach (apache_request_headers() as $header => $value)
echo "$header: $value" . PHP_EOL;
echo "\n";
var_dump($_COOKIE);
?></pre>
</body>
</html>
Output:
Host: example.com
User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Firefox/52.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Cookie: ASP.NET_SessionId=31run6vlfk69j0ni9l02fad82u
Connection: keep-alive
Upgrade-Insecure-Requests: 1
Cache-Control: max-age=0
array(1) {
["ASP_NET_SessionId"]=>
string(26) "31run6vlfk69j0ni9l02fad82u"
}
You get a similar output on the second request. The session id is changing with each request, since the default session handler does not recognize the session name properly. Unfortunely neither an error is thrown nor any notification is logged.
We could write our own session handler by implementing the SessionHandlerInterface and fetching the raw cookie data overriding the given precalculated session id when the session already exists.
However, I've chosen a quick'n'dirty workaround. The default session handler apparently just evaluates the $_COOKIES variable (or relies on the same generation algorithm). Actually the $_COOKIE variable is an associative array, which can have arbitrary strings including blanks, periods etc. as keys. We can overwrite the $_COOKIE variable at runtime as well.
<?php
$raw_cookies = apache_request_headers()['Cookie'];
$cookies = preg_split('/;\s*/', $raw_cookies);
$_COOKIE = [];
foreach ($cookies as $cookie)
{
list($k, $v) = explode('=', $cookie, 2);
$_COOKIE[$k] = $v;
}
session_start (['name' => 'ASP.NET_SessionId']);
?>
<html>
<head><title></title><meta charset="UTF-8"></head>
<body>
<pre><?php var_dump($_COOKIE); ?></pre>
</body>
</html>
Output:
array(1) {
["ASP.NET_SessionId"]=>
string(26) "jqf97quci4sh6k0n4p59b3d18n"
}
Now the session id remains stable and values in $_SESSION are restored after each request.
Note that apache_request_headers() is a specific function to support Apache web servers. There should be functions to support alternative servers as well.
EDIT: On my Apache server I have found the raw cookies string also in $_SERVER[HTTP_COOKIE'].