Skip to content

umarov-safar/easy-session

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Easy php session package for small project

Usage:

Installation

composer require easy-session/easy-session

Methods:

  1. Get instance of session
require_once './vendor/autoload.php';

use Easy\EasySession\Session;

$session = Session::getInstance();
  1. Start session
$session->start();
  1. Set value by key
$session->set('key', 'values');
  1. Get values from session by key
$session->get('key');

4.1 Get default value if key does not exists

$default = $session->get('not_existing_key', 'default value');
  1. Get all items
$session->all();
  1. Get item and remove it
$session->remove('key');
  1. Free(clear) all session variables
$session->clear();
  1. Clear all and destory session
$session->destroy();
  1. Get session id
$session->getId();
  1. Set from existing key
$session->setFromExistingKey('new_key', 'exis_key');

Examples

  1. Start session and set, get, remove velue
require_once './vendor/autoload.php';

use Easy\EasySession\Session;

$session = Session::getInstance();

$session->start(); // Start session

$session->set('user', ['name' => 'Ali', 'email' => 'test@gmail']); // setting data to session

$user = $session->get('user'); // getting user from sesion

echo '<pre>';
print_r($user);
// output: Array([name] => Ali [email] => test@gmail)

// not existing key
print_r($session->get('test')) 
//output: NULL

// default value
print_r($session->get('test', 'default value'))
//output: default value

//Remove item from session
$session->remove('user');

// get item and remove it
$user = $session->get('user'); 
  1. Free all session variables and destroy
require_once './vendor/autoload.php';

use Easy\EasySession\Session;

$session = Session::getInstance();

$session->start(); // Start session

$session->set('user', ['name' => 'Ali', 'email' => 'test@gmail']); // setting data to session

$session->set('cart', ['product 1', 'product 2']);

echo count($this->all()); 
// output: 2

$session->isEmpty(); // return false session not empty

$session->clear(); // clear all session variables

$session->isEmpty(); // return true 

// to destroy session 
$session->destroy();

Enjoy it

About

The easy session library to use in your project!

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages