0

I am working with XAMPP and Windows and I am running some test files from Terminals (cmder or CMD or Power Shell).

But I need know the terminal layout setting. Line width; to create a frame box or a box drawer that fits the terminal column layout.

I run this to get global variable but get nothing:

echo var_dump($_SERVER);

How can I get this information from the server side?

Markus AO
  • 4,771
  • 2
  • 18
  • 29
  • 1
    If you are using the PHP CLI like I think you say you are, there is no Server in that concept – RiggsFolly Aug 18 '20 at 15:43
  • sure but i cant find this infirmation.... but i am runing from CLI and need the settiing layout of CLI, terminal send this information to php Side ??? –  Aug 18 '20 at 16:06
  • @RiggsFolly curious, I thought so too. However, I see that PHP 7.4 as CLI on both Windows and Centos gives me an elaborate `$_SERVER` array, in the interactive shell (`php -a`) and when I run a script. It doesn't have HTTP server related variables, sure, and the available variables are quite different. (Again, on Win they are different depending whether I use CygWin or CMD.) – Markus AO Aug 18 '20 at 18:00
  • @walternunez run `php -i` to get the CLI equivalent of `phpinfo()`. You can see the available Environment and Server variables there. I seem to be able to `var_dump($_SERVER);` just fine in CLI, how exactly are you doing it? Anyway. None of the platforms I looked at have terminal layout or other windowing info. (As much is true over HTTP: you can't get browser size using PHP.) You might be able to get it by querying the terminal with your platform/terminal's equivalent of `exec('how big you pls?');`. – Markus AO Aug 18 '20 at 18:17

2 Answers2

2

based on: markus-ao Answer and this Answer

$info = shell_exec('MODE 2> null') ?? shell_exec('tput cols');
if (strlen($info) > 5) {
    preg_match('/CON.*:(\n[^|]+?){3}(?<cols>\d+)/', $info, $match);
    $info = $match['cols'] ?? 80;
}
echo 'Terminal Width: '. $info;

output in cmder (based on windows size) (windows 10):

C:\xampp\htdocs\dev\avi3\dev
λ php test.php
Terminal Width: 75

output in PowerShell (based on windows size) (windows 10):

PS C:\xampp\htdocs\dev\avi3\dev> php test.php
Terminal Width: 95

output in from putty (in windows 10) connect to (Ubuntu 20.04 server)
(based on putty setting that windows is fixed by default 80 i change it to 120):

admin@testserver:/var/www/html/dev/$ php test.php
Terminal Width: 120

Readers: Please feel free to add notes on getting this data on other platforms. Will integrate to the answer.

1

There is a $_SERVER array accessible via PHP CLI (testing with PHP 7.4); though it won't contain any HTTP server related variables. The remainder will vary a great deal depending on your environment. I looked at what's available on Windows 10, both Command Prompt and CygWin, along with Centos 7. None include client terminal size or other windowing system information. (As much is true for PHP over HTTP; you'll need Javascript to get the user agent size.)

For reference, if you run php -i in your terminal, you will see a complete listing of available server and environment variables toward the end. But as I noted, user agent size isn't one of these variables. (Maybe there's a user agent out there that inputs the size; I have no idea!)

We'll have to do something else here, ie. run a shell command and query the underlying platform for the information. First, a gritty "old school" solution (works both in CMD and PS):

// Using PHP as CLI on Windows.
$info = shell_exec('MODE');

Running the MODE command without arguments gives you something like the following output:

Status for device CON:
----------------------
    Lines:          9001
    Columns:        120
    Keyboard rate:  31
    Keyboard delay: 1
    Code page:      65001

The "Columns" row states the window width in characters. (Note: This output is locale-specific. You may get "Columnas" or "Oszlopok", etc.) Then let's parse for the number:

preg_match('~Columns:\s+(\d+)~', $info, $match);
$columns = $match[1] ?? null;

The $columns variable will contain the width in characters of the current command prompt terminal, if the data is available. If not, set default width instead (here simply "null").

This solution will work only on PHP CLI on Windows Command Prompt and Power Shell; or other (presumably Microsoft) terminal applications that support the MODE command.

There's also a cleaner alternative way to get the column width, if calling PowerShell is an option. The column size / character width is stored in the PS $Host object:

// Returns the number of columns:
$columns = shell_exec('powershell $Host.UI.RawUI.WindowSize.Width');

For other platforms, you'd have to find an equivalent command. In bash, the equivalent would be tput cols (which returns the number of columns). As follows:

$columns = shell_exec('tput cols');

This bash command also works with MacOS and CygWin (tested both). Simple enough!
Readers: Please feel free to add notes on getting this data on other platforms. Will integrate to the answer.

Markus AO
  • 4,771
  • 2
  • 18
  • 29
  • There are also older questions where different methods and languages for accessing console size on different platforms are explored. For reference: - [How do I find the width & height of a terminal window?](https://stackoverflow.com/questions/263890/how-do-i-find-the-width-height-of-a-terminal-window), - [How to get Linux console window width in Python](https://stackoverflow.com/questions/566746/how-to-get-linux-console-window-width-in-python/6550596#6550596) – Markus AO Aug 18 '20 at 19:53
  • how can i determine the terminal installed in php: cmder, cmd, PS, linux terminal? to implement the correct argumento for `shell_exec()` and get the column # –  Aug 18 '20 at 20:34
  • why cant not execute any like this: `$info = shell_exec('MODE') || shell_exec('tput cols');` –  Aug 18 '20 at 20:56
  • 1. You'll have to compare the server/environment variables between platforms/terminals to determine this. 2. Because if [shell_exec](https://www.php.net/manual/en/function.shell-exec.php) returns a string, such as *"'mode' is not recognized as an internal or external command, operable program or batch file"*, that doesn't evaluate as `false`. Manual: *"This function can return NULL both when an error occurs or the program produces no output. It is not possible to detect execution failures using this function. exec() should be used when access to the program exit code is required."* – Markus AO Aug 18 '20 at 21:21
  • i have solve it: https://stackoverflow.com/a/63476834/11091648 –  Aug 18 '20 at 21:25