PHP code example of gears / session

1. Go to this page and download the library: Download gears/session library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

gears / session example snippets


// Make sure you have composer included
n = new Gears\Session();

// Configure the session container
$session->dbConfig = 
[
	'driver'    => 'mysql',
	'host'      => 'localhost',
	'database'  => 'db_name',
	'username'  => 'db_user',
	'password'  => 'abc123',
	'charset'   => 'utf8',
	'collation' => 'utf8_unicode_ci',
	'prefix'    => '',
];

// Install the session api
$session->install();

// Next you will probably want to make the session object global.
$session->globalise();

// Storing An Item In The Session
Session::put('key', 'value');

// Push A Value Onto An Array Session Value
Session::push('user.teams', 'developers');

// Retrieving An Item From The Session
$value = Session::get('key');

// Retrieving An Item Or Returning A Default Value
$value = Session::get('key', 'default');
$value = Session::get('key', function() { return 'default'; });

// Retrieving An Item And Forgetting It
$value = Session::pull('key', 'default');

// Retrieving All Data From The Session
$data = Session::all();

// Determining If An Item Exists In The Session
if (Session::has('users'))
{
    //
}

// Removing An Item From The Session
Session::forget('key');

// Removing All Items From The Session
Session::flush();

// Regenerating The Session ID
Session::regenerate();

// Flashing Data
Session::flash('key', 'value');

// Reflashing The Current Flash Data For Another Request
Session::reflash();

// Reflashing Only A Subset Of Flash Data
Session::keep(array('username', 'email'));

if (Session::hasExpired())
{
	echo 'Due to inactivity, your session has expired!';
	echo 'Please <a href="/login">click here</a> to login again.';
}