Skip to content

Instantly share code, notes, and snippets.

@arcanisgk
Created January 17, 2023 17:08
Show Gist options
  • Save arcanisgk/3eb0eabbfb1cb3f142649b0e71bf02da to your computer and use it in GitHub Desktop.
Save arcanisgk/3eb0eabbfb1cb3f142649b0e71bf02da to your computer and use it in GitHub Desktop.
Session.php
<?php
declare(strict_types=1);
namespace FrameWork\App;
use DataBase\UserAccount;
use Exception;
class Session
{
/**
* @var Session|null
*/
private static ?Session $instance = null;
/**
* Description: Auto-Instance Helper for static development class BugCatcher.
* @return Session
*/
public static function getInstance(): Session
{
if (!self::$instance instanceof self) {
self::$instance = new self();
}
return self::$instance;
}
/**
* @var float
*/
private float $startTime;
/**
* @var float
*/
private float $startMemory;
/**
* @param float $startTime
* @param float $startMemory
* @return void
*/
public function initSession(float $startTime, float $startMemory): void
{
$this->setStartTime($startTime);
$this->setStartMemory($startMemory);
if (!isset($_SESSION)) {
$this->setSessionName();
$this->setSessionParams();
}
$this->startSession();
if (!isset($_SESSION['SES_START'])) {
$this->setSessionVariable();
} else {
$this->evaluateSession();
}
}
/**
* @return void
*/
private function setSessionName(): void
{
session_name($_SERVER['CONFIG']['SESSION']['SESSION_NAME']);
}
/**
* @return void
*/
private function setSessionParams(): void
{
$lifetime = (int)$_SERVER['CONFIG']['SESSION']['SESSION_LIFETIME'] + time();
$secure = $_SERVER['CONFIG']['SESSION']['SESSION_SECURE'];
$httponly = $_SERVER['CONFIG']['SESSION']['SESSION_HTTPONLY'];
$sameSite = $_SERVER['CONFIG']['SESSION']['SESSION_SAME_SITE'];
session_set_cookie_params(
[
'lifetime' => $lifetime, //mean 2678400
'path' => '/',
'domain' => $_SERVER['HTTP_HOST'], //mean domain
'secure' => $secure, //is set to false
'httponly' => $httponly, //is set to true
'samesite' => $sameSite, //is set to lax
]
);
}
/**
* @return void
*/
private function startSession(): void
{
session_start();
}
/**
* @return void
*/
private function setSessionVariable(): void
{
$_SESSION['SESSION_START'] = true;
$_SESSION['SESSION_LANG'] = $_SERVER['CONFIG']['HOST']['LANG'] ?? $_SERVER['LANG'];
$_SESSION['SESSION_ACTIVITY_EXPIRE'] = time() + (int)$_SERVER['CONFIG']['SESSION']['SESSION_ACTIVITY_EXPIRE'];
$_SESSION['START_TIME'] = $this->getStartTime();
$_SESSION['START_MEMORY'] = $this->getStartMemory();
}
/**
* @return float
*/
public function getStartTime(): float
{
return $this->startTime;
}
/**
* @param float $startTime
*/
public function setStartTime(float $startTime): void
{
$this->startTime = $startTime;
}
/**
* @return float
*/
public function getStartMemory(): float
{
return $this->startMemory;
}
/**
* @param float $startMemory
*/
public function setStartMemory(float $startMemory): void
{
$this->startMemory = $startMemory;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment