PHP code example of anax / session

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

    

anax / session example snippets


/**
 * Config-file for sessions.
 */
return [
    // Session name
    "name" => preg_replace("/[^a-z\d]/i", "", __DIR__),
    //"name" => preg_replace("/[^a-z\d]/i", "", ANAX_APP_PATH),
];

/**
 * Creating the session as a $di service.
 */
return [
    // Services to add to the container.
    "services" => [
        "session" => [
            "active" => defined("ANAX_WITH_SESSION") && ANAX_WITH_SESSION, // true|false
            "shared" => true,
            "callback" => function () {
                $session = new \Anax\Session\Session();

                // Load the configuration files
                $cfg = $this->get("configuration");
                $config = $cfg->load("session");

                // Set session name
                $name = $config["config"]["name"] ?? null;
                if (is_string($name)) {
                    $session->name($name);
                }

                $session->start();

                return $session;
            }
        ],
    ],
];

# $app style
$app->session->start();

# $di style, two alternatives
$di->get("session")->start();

$session = $di->get("session");
$session->start();

# Check if a key is set in the session
$app->session->has($key);

# Get a value from the session, null if it is not set.
$app->session->get($key);

# Get a value from the session or get the default value.
$app->session->get($key, $default);

# Set a value in the session, associated with a key.
$app->session->set($key, $value);

# Delete a key from the session.
$app->session->delete($key);

# Get the value from the session and then delete its key, default is null.
$app->session->getOnce($key);

# Specify your own default value when key does not exists.
$app->session->getOnce($key, $default);

# Debug the session and review its content through var_dump.
var_dump($app->session);

# Destroy the session.
$app->session->destroy();