PHP Classes

How to Use PHP Sessions to Share Values Between PHP Scripts Running on the Command Line Console Using the Package PHP CLI Session: Retrieve PHP CLI environment values

Recommend this page to a friend!
     
  Info   Example   View files Files   Install with Composer Install with Composer   Download Download   Reputation   Support forum   Blog    
Last Updated Ratings Unique User Downloads Download Rankings
2025-07-30 (Yesterday) RSS 2.0 feedNot yet rated by the usersTotal: Not yet counted Not yet ranked
Version License PHP version Categories
php-cli-session 1.0MIT/X Consortium ...7.4System information, Unix, Console, Wi..., P...
Description 

Author

This package can retrieve PHP CLI environment values.

It provides a class that can inspect various aspects of the PHP environment when running the command-line console on either Linux, Windows, or macOS.

Currently, it can retrieve:

- The current machine user

- If the current terminal supports displaying colored characters

- If the current terminal supports using ANSI escape characters to format text or change the color of text

- If the current terminal is running on Windows or Linux

- Get the width or height of the current terminal in number of characters

- Get the current process number

- Get a system identifier to use as a unique value to identify sessions

- Get the computer model

- Get the network MAC address

Innovation Award
PHP Programming Innovation award nominee
July 2025
Nominee
Vote
PHP developer use sessions in Web applications to store variable values that can be retrieved during different accesses of the same user.

This package can generate a session identifier that allows to setup PHP sessions to store variable values that can be retrieved by different PHP scripts running from the command line console.

Manuel Lemos
Picture of Ujah Chigozie peter
  Performance   Level  
Name: Ujah Chigozie peter <contact>
Classes: 32 packages by
Country: Nigeria Nigeria
Innovation award
Innovation award
Nominee: 12x

Instructions

Example

#!/usr/bin/env php
<?php
use Peterujah\Cli\System\Session;

// Load composer bootloader.
require __DIR__ . '/plugins/vendor/autoload.php';

// Initialize CLI session
Session::init();

$sessionId = Session::getSystemId();
session_id($sessionId);

if (!
session_start()) {
    echo
"Failed to start session.\n";
    exit(
1);
}

if (!isset(
$_SESSION['cli_user'])) {
   
$_SESSION['cli_data'] = 'This is data from CLI session.';
   
$_SESSION['cli_user'] = 'peter';

    echo
"Registered new session ID: {$sessionId}\n";
    echo
"Run: 'php session.php' {$sessionId} to access this session.\n";
} else {
    echo
"Resumed session with ID: {$sessionId}\n";
    echo
"User: {$_SESSION['cli_user']}\n";
    echo
"Data: {$_SESSION['cli_data']}\n";
}


Details

CLI Session Helper for PHP

A lightweight utility class to manage and inspect CLI session environments in PHP. Designed for cross-platform compatibility (Linux, macOS, Windows) with focus on stream handling, session identification, terminal capability detection, and environmental diagnostics.

> No external dependencies. Built for PHP 7+

Features

  • Detects and resolves `STDIN`, `STDOUT`, `STDERR` in CLI environments
  • Handles platform-specific stream opening safely
  • Retrieves: - Terminal size (height and width) - Current user (`whoami`) - MAC address - OS and system model - Shell and terminal name
  • Detects: - ANSI and color support - Platform type (Windows, macOS, Linux, cloud)
  • Generates a unique system identifier for CLI sessions
  • Silently handles errors using proper fallbacks

Installation

Install via Composer PHP package manager.

composer require peterujah/php-cli-session

Usage

Initialize CLI streams

use Peterujah\Cli\System\Session;

Session::init(); // Optional if constructor is called

Start PHP session with CLI-safe ID

$sessionId = Session::getSystemId();
session_id($sessionId);
session_start();

See working example CLI Demos

Examples

Get CLI terminal info

$info = Session::getSystemInfo();

foreach ($info as $item) {
    echo $item['Name'] . ': ' . $item['Value'] . PHP_EOL;
}

Detect color and ANSI support

if (Session::isColorSupported()) {
    echo "? Color output is supported." . PHP_EOL;
}

if (Session::isAnsiSupported()) {
    echo "? ANSI escape sequences are supported." . PHP_EOL;
}

Detect terminal dimensions

$width = Session::getWidth();
$height = Session::getHeight();

echo "Terminal Size: {$width} x {$height}" . PHP_EOL;

Methods Overview

| Method | Description | | ---------------------------- | -------------------------------------------------------------- | | init() | Initializes CLI stream resources (STDIN, STDOUT, etc.) | | getSystemId() | Returns a unique, hash-based identifier for the current system | | getSystemInfo() | Returns structured system diagnostics | | getTerminalName() | Returns terminal name (tty, PowerShell, etc.) | | getMacAddress() | Retrieves system MAC address | | getPid() | Gets the parent process ID | | whoami() | Gets the current user executing the script | | isColorSupported() | Checks if terminal supports color output | | isAnsiSupported() | Checks if terminal supports ANSI escape codes | | getWidth() / getHeight() | Gets terminal width and height | | isPlatform($os) | Checks for OS platform or cloud environment |

Platform Compatibility

| OS | Status | | -------- | -------------------------------------------- | | Linux | ? Tested | | macOS | ? Tested | | Windows | ? Tested | | BSD/Unix | ?? Partial support | | Docker | ? Supported (if tput or stty available) |

License

MIT License ? use it, hack it, extend it.

TODO

  • Optional support for logging output to a file
  • Add unit tests
  • CLI command wrapper integration
  • PHP framework integration hook

  Files folder image Files (6)  
File Role Description
Files folder imagesrc (1 file)
Files folder imagetests (2 files)
Accessible without login Plain text file composer.json Data Auxiliary data
Accessible without login Plain text file LICENSE Lic. License text
Accessible without login Plain text file README.md Doc. Documentation

  Files folder image Files (6)  /  src  
File Role Description
  Plain text file Session.php Class Class source

  Files folder image Files (6)  /  tests  
File Role Description
  Accessible without login Plain text file luminova-session.php Example Configuration script
  Accessible without login Plain text file session.php Example Configuration script

The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page.
Install with Composer Install with Composer
 Version Control Unique User Downloads  
 100%
Total:0
This week:0