Basic Session Class with PHP
Hello, I needed to use session in a small project and I wrote a class to make my job easier, we can easily use or develop it in your simple projects. Now use it how you want to use it. You can create a maximum of 2 dimensional arrays, and you won't need more in the session :)
<?php
session_start();
class Session {
public static function get($key, $subKey = false) {
return $_SESSION[$key][$subKey] ?? $_SESSION[$key] ?? false;
}
public static function create($key, $data) {
$_SESSION[$key] = $data;
}
public static function append($key, $data, $singleData = false) {
if ( !self::get($key) ) {
throw new Exception('The session you added is not created');
}
if ( !is_array(self::get($key)) ) {
throw new Exception('The session you are adding is not of array type');
}
if (is_array($data)) {
$_SESSION[$key] += $data;
} else {
$_SESSION[$key][$data] = $singleData;
}
}
public static function prepend($key, $data) {
if ( !self::get($key) ) {
throw new Exception('The session you added is not created');
}
if ( !is_array(self::get($key)) ) {
throw new Exception('The session you are adding is not of array type');
}
if (is_array($data)) {
$_SESSION[$key] = $data + $_SESSION[$key];
} else {
$_SESSION[$key]= [$data => $singleData] + $_SESSION[$key];
}
}
public static function remove($key, $subKey = false) {
if ($subKey) {
if (is_array($subKey)) {
array_map(function($k) use ($key) {
unset($_SESSION[$key][$k]);
}, $subKey);
} else {
unset($_SESSION[$key][$subKey]);
}
} else {
unset($_SESSION[$key]);
}
}
public static function getAll() {
return $_SESSION;
}
public static function dumpAll() {
echo '<pre>';
print_r(self::getAll());
}
}
Session::create('user', [
'name' => 'phpExample'
]);
Session::dumpAll();
Session::append('user', [
'email' => '[email protected]',
'surname' => 'phpExampleNet'
]);
Session::append('user', 'key', 'value');
Session::prepend('user', [
'id' => 1
]);
Session::dumpAll();
print_r(Session::get('user'));
echo Session::get('user', 'id');
// Session::remove('user');
// Session::remove('user', 'email');
Session::remove('user', ['email', 'key', 'surname']);
Session::dumpAll();
Session::create('name', 'phpExample');
try {
Session::append('name', 'key', 'value'); // incorrect
} catch (Exception $e) {
echo $e->getMessage();
}
Session::dumpAll();
see you in my next post
Comments
Popular Articles
Popular Tags
- #directive
- #Function
- #Json
- #Class
- #SASS
- #Form
- #Redirect
- #API
- #Special Characters
- #Special
- #Random
- #Generating
- #
- #Text
- #Ajax
- #URL
- #Encrypting
- #React
- #Show
- #timeAgo
- #PSR-4
- #Hide
- #DDOS Protection
- #DDOS
- #cURL
- #Logic For Displaying Posts
- #Error
- #Key
- #General Error
- #1364 Field
- #Abbreviation
- #Blade
- #Version
- #QR
- #QR Code Generating
- #Array
- #Arrays with Key Values to String Statement
- #Short Tag
- #Activate
- #Real Time
- #Socket.io
- #301
- #Custom Directives
- #Iframe Detection
- #Date
- #Characters
- #Insert
- #Autoloader
- #Composer
- #Reading
There are no comments, make the firs comment