Skip to content

Commit

Permalink
Generate new PHP session name with vertical and environment to avoid …
Browse files Browse the repository at this point in the history
…wrong behaviors
  • Loading branch information
p0lemic authored Jan 20, 2021
2 parents b75f9dc + 49a683a commit 3e6962c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
28 changes: 20 additions & 8 deletions src/Sifo/Session.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Session

static private $instance;

private function __construct()
private function __construct($session_name)
{
if ( !headers_sent( ) )
{
Expand All @@ -39,13 +39,15 @@ private function __construct()
if ( headers_sent ( ) )
{
trigger_error( "Session: The session was not started before the sending of the headers." );
return false;
return;
}
else
{
// Session init.
session_start();

if ($session_name instanceof SessionNameStrategy) {
$session_name->set();
}

// Session init.
session_start();
}
}

Expand All @@ -56,10 +58,20 @@ private function __construct()
* @return Session
*/
public static function getInstance()
{
{
if ( !isset( self::$instance ) )
{
self::$instance = new self();
$session_name_environment = FilterEnv::getInstance()->getString('SESSION_NAME');

switch ($session_name_environment) {
case 'environment_and_vertical':
$session_name_strategy = new SessionEnvironmentStrategy();
break;
default:
$session_name_strategy = null;
}

self::$instance = new self($session_name_strategy);
}
return self::$instance;
}
Expand Down
15 changes: 15 additions & 0 deletions src/Sifo/Session/SessionEnvironmentStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace Sifo;

class SessionEnvironmentStrategy implements SessionNameStrategy
{
public function set()
{
$instance_inheritance = Domains::getInstance()->getInstanceInheritance();
$vertical_instance = array_pop($instance_inheritance);
$instance_environment_initial = isset($_SERVER['APP_ENV'][0]) ? $_SERVER['APP_ENV'][0] : '';
$instance_session_name = "SSID_{$instance_environment_initial}_{$vertical_instance}";
session_name($instance_session_name);
}
}
8 changes: 8 additions & 0 deletions src/Sifo/Session/SessionNameStrategy.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Sifo;

interface SessionNameStrategy
{
public function set();
}

0 comments on commit 3e6962c

Please sign in to comment.