2025-07-19 15:57:10 +02:00
< ? php
//$fs = new furatalogSessions();
//$fs->setSessionData("nsfw", true);
//$fs->getSessionData("nsfw");
class furatalogSessions {
public $cookieKey = " " ;
public $dataObj ;
private mysqli $db ;
private $cookie_duration = 30 * 24 * 60 * 60 ;
public function __construct () {
$this -> dataObj = ( object ) array ();
$this -> connectDb ();
$this -> getCookieKey ();
}
public function setSessionData ( $varName , $content ) {
$this -> dataObj -> { $varName } = $content ;
$this -> saveToDb ();
$this -> saveCookie ();
}
public function getSessionData ( $varName ) {
try {
return $this -> dataObj -> { $varName };
} catch ( Exception $e ) {
return null ;
}
}
public function unsetSessionData ( $varName ) {
unset ( $this -> dataObj -> { $varName });
}
public function destroy () {
$this -> deleteData ( $this -> cookieKey );
$this -> deleteCookie ();
}
private function saveToDb () {
$jsonDataToSave = json_encode ( $this -> dataObj , JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_QUOT | JSON_HEX_AMP | JSON_UNESCAPED_UNICODE );
2025-07-19 16:17:14 +02:00
$tmpresult = $this -> db -> query ( " SELECT cookieKey FROM `sessions` WHERE cookieKey= \" " . $this -> cookieKey . " \" " );
2025-07-19 15:57:10 +02:00
if ( ! ( $tmpresult -> num_rows > 0 )) {
$this -> db -> query ( " INSERT INTO `sessions`(`cookieKey`, `lastused`, `data`) VALUES (' " . $this -> cookieKey . " ', now(),' " . $jsonDataToSave . " ') " );
} else {
$this -> db -> query ( " UPDATE `sessions` SET lastused=now(), data=' " . $jsonDataToSave . " ' WHERE cookieKey= \" " . $this -> cookieKey . " \" ; " );
}
}
private function saveCookie () {
2025-07-19 16:07:26 +02:00
setcookie ( " _cookieKey " , $this -> cookieKey , time () + $this -> cookie_duration );
2025-07-19 15:57:10 +02:00
}
private function connectDb () {
$this -> db = new mysqli ( " localhost " , " furatalog_admin_usr " , " NR6tLk7c56bPT5[] " , " furatalog " );
}
private function getCookieKey () {
$this -> cookieKey = isset ( $_COOKIE [ " _cookieKey " ]) ? $_COOKIE [ " _cookieKey " ] : " " ;
if ( $this -> cookieKey == " " ) {
2025-07-19 16:02:50 +02:00
$this -> cookieKey = $this -> generateKey ();
2025-07-19 15:57:10 +02:00
}
}
private function getData () {
$tmpresult = $this -> db -> query ( " SELECT id, cookieKey, UNIX_TIMESTAMP(lastused) as lastused, data FROM sessions WHERE cookieKey= \" " . $this -> cookieKey . " \" " );
if ( $tmpresult -> num_rows > 0 ) {
$fetchedData = $tmpresult -> fetch_assoc ();
if ( $fetchedData [ " lastused " ] < strtotime ( '-30 days' )) {
$this -> deleteData ( $fetchedData [ " cookieKey " ]);
$this -> deleteCookie ();
$this -> getCookieKey ();
} else {
$this -> dataObj = json_decode ( $fetchedData [ " data " ]);
}
}
}
private function deleteCookie () {
setcookie ( " _cookieKey " , " " , time () - 3600 );
}
private function deleteData ( $cookieKey ) {
if ( $cookieKey != " " && $cookieKey != null ) {
$tmpresult = $this -> db -> query ( " DELETE FROM sessions WHERE cookieKey= \" " . $cookieKey . " \" " );
}
}
private function generateKey () {
$isNotInDB = false ;
do {
$length = 20 ;
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' ;
$charactersLength = strlen ( $characters );
$randomString = '' ;
for ( $i = 0 ; $i < $length ; $i ++ ) {
$randomString .= $characters [ random_int ( 0 , $charactersLength - 1 )];
}
$tmpresult = $this -> db -> query ( " SELECT cookieKey FROM `sessions` WHERE cookieKey= \" " . $randomString . " \" " );
if ( ! ( $tmpresult -> num_rows > 0 )) {
$isNotInDB = true ;
}
} while ( ! $isNotInDB );
return $randomString ;
}
}