2

I know that usual PHP sessions don't work as expected in CLI mode because they're based on cookies.

But what if I need to keep some user settings until terminal is closed? For example, user can specify UI language, some extra credentials, color scheme etc. I could save the settings in database but how to associate them with the user?

Is there a unique "session" or "connection" ID at all?

ymakux
  • 3,415
  • 1
  • 34
  • 43
  • 1
    Maybe this post would help: https://unix.stackexchange.com/questions/86157/find-out-screen-id – Akhil VL Jan 11 '18 at 12:27
  • They advise to use environment variables as a temporary storage. It won't work across all commands – ymakux Jan 11 '18 at 12:56

2 Answers2

2

Try dumping the $_ENV array to find out which variables are available on your system:

php -r 'print_r($_ENV);'

Typically you can use $_ENV['USER'] to get the current user's name for example.

Thomas Sahlin
  • 796
  • 1
  • 4
  • 4
1

You can either store some information in the users home directory

$path = getenv("HOME") . DIRECTORY_SEPARATOR . ".myscriptprops";

Try it out:

php -r 'echo getenv("HOME") . DIRECTORY_SEPARATOR . ".myscriptprops";'

Or use STDIN to request the user to insert data while the script executes:

echo "Please enter some value: ";
$value = fgets(STDIN);
Philipp Wrann
  • 1,751
  • 3
  • 19
  • 29
  • Thanks. Each command consists of "php executable-file [options]". So each invocation of executable-file will produce a different PHP process. The second approach will work only within a particular command, not whole "session" – ymakux Jan 11 '18 at 13:34
  • And yes, $_ENV is empty (WIN), so getenv('HOME') won't work too – ymakux Jan 11 '18 at 13:38
  • 1
    Have a look at [this](https://stackoverflow.com/questions/1894917/how-to-get-the-home-directory-from-a-php-cli-script) – Philipp Wrann Jan 11 '18 at 13:59