Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Ligonsker's avatar

How to prevent sessions keys from splitting for certain characters like dot?

Hello,

I have a URL that I want to set as the session key: http://my.url/some-route, but when I set the session key, it splits the session key to an array when it encounters the dot:

session([$url => $data]);

The result of the above when I use dd(session()->all()) is:

  "http://my" => array:1 [
      "url/some-route" => array:1 [
          "some_data" => "some_value"         		
    ]
  ]

Why?

0 likes
5 replies
LaryAI's avatar
Level 58

The issue is caused by the dot character being used as a delimiter for array keys in PHP. To prevent this from happening, you can use a different character as the delimiter for the session key. One option is to use an underscore instead of a dot. For example:

$url = str_replace('.', '_', $url);
session([$url => $data]);

This will replace any dots in the URL with underscores before setting it as the session key. When you retrieve the session data, you can simply replace the underscores with dots to get the original URL:

$url = str_replace('_', '.', $url);
$data = session($url);
Snapey's avatar

its a feature of the session helper so that you can access sub keys, the same way you can with config helper, eg

config('app.name');
newbie360's avatar
session(['test' => 'http://en.example.com']);
dd(session()->all());

// result
'test' => 'http://en.example.com'

dd the $url and $data before set the session

Please or to participate in this conversation.