PHP code example of rcastera / session

1. Go to this page and download the library: Download rcastera/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/ */

    

rcastera / session example snippets






use rcastera\Browser\Session\Session;

$session = new Session();


    castera\Browser\Session\Session;

    $errors = array();

    // You'll definitely want to add more validation here and check against a
    // database or something. This is just an example.
    if (! empty($_POST)) {
        if ($_POST['username'] == 'test' && $_POST['password'] == 'test') {
            $session = new Session();

            // You can define what you like to be stored.
            $user = array(
                'user_id' => 1,
                'username' => $_POST['username']
            );

            $session->register(120); // Register for 2 hours.
            $session->set('current_user', $user);
            header('location: index.php');
            exit;
        } else {
            $errors[] = 'Invalid login.';
        }
    }


    castera\Browser\Session\Session;

    $session = new Session();

    // Check if the session registered.
    if ($session->isRegistered()) {
        // Check to see if the session has expired.
        // If it has, end the session and redirect to login.
        if ($session->isExpired()) {
            $session->end();
            header('location: login.php');
            exit;
        } else {
            // Keep renewing the session as long as they keep taking action.
            $session->renew();
        }
    } else {
        header('location: login.php');
        exit;
    }


    castera\Browser\Session\Session;

    $session = new Session();
    $session->end();
    header('location: login.php');
    exit;