PHP CLI: PHP INI

From The Uniform Server Wiki
Jump to navigation Jump to search

 

MPG UniCenter

UniServer 5.0-Nano
PHP CLI.

PHP CLI ini file and installing modules

One the previous page I suggested you may never require a CLI ini file. However you may want to include extra modules for your CLI scripts hence this page covers creating an ini file and shows how to include a module. The module covered is not part of the standard PHP package distribution hence needs to be installed with an ini file.

The module is specific to CLI, it provides statistics for all currently running processes. You can use it to determine if a process is running retrieve its pid along with other statistics.

Initial test setup

Edit our two test files Run.bat and test_1.php contained in folder UniServer to have the following content:

Run.bat test_1.php
TITLE CLI TEST BAT
COLOR B0
@echo off
cls
echo.
usr\local\php\php.exe test_1.php
echo.
pause
<?php
echo "Hello World\n";
exit(0); // Script ran OK
?>

Run the batch file (double click on Run.bat), "Hello World" displayed

Note Mona:

If running on UniServer Mona add folder udrive to path as shown udrive\usr\local\php\php.exe test_1.php

Running the batch file produces several warning messages, remember the cause!

If a php-cli.ini file is not found main PHP program uses as default any php.ini file it finds. On the previous page we resolved this using the "-n" switch as parameter to php.exe

Top

Create a new PHP ini-file

Instead of letting PHP use defaults we want to specify certain values using our own configuration file.

This is achieved by creating a php-cgi.ini file in folder PHP.

Create a new configuration file named php-cli.ini in folder UniServer\usr\local\php with the following content:

php-cli.ini
extension=fred.dll

Ini-files are plain text files, extension=fred.dll is a dummy for testing.

The two files Run.bat and test_1.php remains unchanged as above.

Note Nano:

Run the batch file a single warning is produced and “Hello World” displayed.

The warning proves correct configuration file is being picked up.

Note Mona:

If running on UniServer Mona add folder udrive to paths as shown

udrive\usr\local\php\php.exe test_1.php

Run the batch file. This time instead of multiple warnings a single warning is produced and “Hello World” displayed.

The warning proves correct configuration file is being picked up.

Note 1: The warning is interesting:

PHP Warning:  PHP Startup: Unable to load dynamic library 'C:\php5\fred.dll'
 - The specified module could not be found.
 in Unknown on line 0
Hello World
Press any key to continue . . .

PHP is looking in folder C:\php5 for the non existent dll. This path is a default and set at compile time.

However we can now tailor php-cli.ini to meet our specific requirements and that includes the path where PHP looks for dll's

What’s important most of the directives in a standard php.ini file can be used in a CLI configuration file.

Remainder of this page shows how to install a new module that is not included in the standard PHP distribution.

Top

Extension download

The core component php5ts.dll is generally adequate for most scripting tasks however you may need to add extensions for a special task.

The following example uses an extension (php_win32ps.dll) it is not part of the main PHP distribution hence requires downloading and installing. It lists all currently running processes.

Top

Download

Note: This extension works only on the Mona series

Will extend this section for Nano when I find a version that runs on PHP 5.3.0


For this example download php_win32ps.dll from http://www.sfr-fresh.com/windows/www/pecl-5.2.6-Win32.zip/ towards end of the page.

You can save to the existing extensions folder UniServer\udrive\usr\local\php\extensions however my personal preference is to keep any new extensions separate in their own folder.

Create a new folder named UniServer\udrive\usr\local\php\new_extensions save the downloaded file to this folder.

Note: Any extensions you download must match the version of PHP you are using.

Top

Install

The location of our extension (php_win32ps.dll) is unimportant because it is installed using the configuration file (php-cli.ini) where we inform PHP where this extension folder is located and what extension to load.

Edit file php-cli.ini and add the following content:

extension=php_win32ps.dll
extension_dir = "./new_extensions"

Run the batch file (double click on Run.bat) no error messages indicate no problems with the path or loading the extension..

Top

Test

Although the simple test above produced no errors its no guarantee the extension was correctly installed hence it is best to run a small test script that uses the extension.

The extension lists all running processes to see this in action edit file test_1.php as follows:

<?php
print_r(win32_ps_list_procs());
echo "Hello World";
exit(0); // Script ran OK
?>

Run the script by double clicking on Run.bat each process is listed with relevant information for example this small extract:

[31] => Array
        (
            [pid] => 2932
            [exe] => C:\Program Files\Mozilla Firefox\firefox.exe
            [mem] => Array
                (
                    [page_fault_count] => 18999
                    [peak_working_set_size] => 65421312
                    [working_set_size] => 52396032
                    [quota_peak_paged_pool_usage] => 119868
                    [quota_paged_pool_usage] => 116388
                    [quota_peak_non_paged_pool_usage] => 23704
                    [quota_non_paged_pool_usage] => 14856
                    [pagefile_usage] => 41562112
                    [peak_pagefile_usage] => 55746560
                )
            [tms] => Array
                (
                    [created] => 77.765
                    [kernel] => 1.781
                    [user] => 2.031
                )
        )

Top

Command line switches

To view a full list of the currently available command line switches modify Run.bat as follows:

TITLE CLI TEST BAT
COLOR B0
@echo off
cls
echo.
:usr\local\php\php.exe -n test_1.php
usr\local\php\php.exe -n -h

echo.
pause

Note 1: I commented out the main line using ":" batch files ignore lines with a comment. Its a quick method to disable a line of code, easily reinstated for latter use.

Note 2: The "-n" switch followed by "-h" produces this help file:

Note Mona:

Change the path to: udrive\usr\local\php\php.exe -n -h

Usage: php [options] [-f] <file> [--] [args...]
       php [options] -r <code> [--] [args...]
       php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]
       php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]
       php [options] -- [args...]
       php [options] -a

  -a               Run interactively
  -c <path>|<file> Look for php.ini file in this directory
  -n               No php.ini file will be used
  -d foo[=bar]     Define INI entry foo with value 'bar'
  -e               Generate extended information for debugger/profiler
  -f <file>        Parse and execute <file>.
  -h               This help
  -i               PHP information
  -l               Syntax check only (lint)
  -m               Show compiled in modules
  -r <code>        Run PHP <code> without using script tags <?..?>
  -B <begin_code>  Run PHP <begin_code> before processing input lines
  -R <code>        Run PHP <code> for every input line
  -F <file>        Parse and execute <file> for every input line
  -E <end_code>    Run PHP <end_code> after processing all input lines
  -H               Hide any passed arguments from external tools.
  -s               Display colour syntax highlighted source.
  -v               Version number
  -w               Display source with stripped comments and whitespace.
  -z <file>        Load Zend extension <file>.

  args...          Arguments passed to script. Use -- args when first argument
                   starts with - or script is read from stdin

  --ini            Show configuration file names

  --rf <name>      Show information about function <name>.
  --rc <name>      Show information about class <name>.
  --re <name>      Show information about extension <name>.
  --ri <name>      Show configuration for extension <name>.

Note 3: If you have problems running a script and suspect a configuration file conflict use the "--ini" swith for example:

TITLE CLI TEST BAT
COLOR B0
@echo off
cls
echo.
:usr\local\php\php.exe -n test_1.php
usr\local\php\php.exe -n --ini

echo.
pause

Top

Summary

Essentially that completes CLI use and configuration, remainder of this series covers some useful scripts and techniques.

To repeat, generally you will not require a configuration file hence use the “-n” switch. The above extension is covered on the next page its not required because UniServer has a better alternative also covered on that page.

Top


MPG (Ric)