https://wiki.uniformserver.com/index.php?title=PHP_CLI:_PHP_INI&feed=atom&action=historyPHP CLI: PHP INI - Revision history2024-03-29T05:09:54ZRevision history for this page on the wikiMediaWiki 1.41.0https://wiki.uniformserver.com/index.php?title=PHP_CLI:_PHP_INI&diff=3818&oldid=prevRic: New page: {{Uc nav 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 ...2009-08-15T09:50:46Z<p>New page: {{Uc nav 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 ...</p>
<p><b>New page</b></p><div>{{Uc nav PHP CLI}}<br />
'''''PHP CLI ini file and installing modules'''''<br />
<br />
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.<br />
<br />
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.<br />
<br />
== Initial test setup ==<br />
Edit our two test files '''Run.bat''' and '''test_1.php''' contained in folder UniServer to have the following content:<br />
{|<br />
|-<br />
|'''''Run.bat'''''||'''''test_1.php'''''<br />
|-valign="top"<br />
|<br />
<pre><br />
TITLE CLI TEST BAT<br />
COLOR B0<br />
@echo off<br />
cls<br />
echo.<br />
usr\local\php\php.exe test_1.php<br />
echo.<br />
pause<br />
</pre><br />
|<br />
<pre><br />
<?php<br />
echo "Hello World\n";<br />
exit(0); // Script ran OK<br />
?><br />
</pre><br />
|}<br />
Run the batch file (double click on Run.bat), "Hello World" displayed<br />
<br />
'''''Note Mona'':'''<br />
<br />
If running on UniServer Mona add folder udrive to path as shown '''udrive\'''usr\local\php\php.exe test_1.php<br />
<br />
Running the batch file produces several warning messages, remember the cause!<br />
<br />
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 <br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== Create a new PHP ini-file ==<br />
Instead of letting PHP use defaults we want to specify certain values using our own configuration file.<br />
<br />
This is achieved by creating a '''php-cgi.ini''' file in folder PHP.<br />
<br />
Create a new configuration file named '''php-cli.ini''' in folder UniServer\usr\local\'''php''' with the following content:<br />
<br />
{|<br />
|-<br />
|'''''php-cli.ini'''''||<br />
|-valign="top"<br />
|<br />
<pre><br />
extension=fred.dll<br />
</pre><br />
|valign="middle"|<br />
Ini-files are plain text files, '''extension=fred.dll''' is a dummy for testing.<br />
|}<br />
<br />
The two files Run.bat and test_1.php remains unchanged as above.<br />
<br />
'''''Note Nano'':'''<br />
<br />
Run the batch file a single warning is produced and “Hello World” displayed.<br />
<br />
The warning proves correct configuration file is being picked up.<br />
<br />
'''''Note Mona'':'''<br />
<br />
If running on UniServer Mona add folder udrive to paths as shown<br />
<br />
udrive\usr\local\php\php.exe test_1.php<br />
<br />
Run the batch file. This time instead of multiple warnings a single warning is produced and “Hello World” displayed.<br />
<br />
The warning proves correct configuration file is being picked up.<br />
<br />
'''''Note 1'':'''<br />
The warning is interesting:<br />
<pre><br />
PHP Warning: PHP Startup: Unable to load dynamic library 'C:\php5\fred.dll'<br />
- The specified module could not be found.<br />
in Unknown on line 0<br />
Hello World<br />
Press any key to continue . . .<br />
</pre><br />
PHP is looking in folder '''C:\php5''' for the non existent dll. This path is a default and set at compile time. <br />
<br />
However we can now '''tailor php-cli.ini''' to meet our specific requirements and that includes the path where PHP looks for dll's<br />
<br />
What’s important most of the directives in a '''standard php.ini''' file can be used in a CLI configuration file.<br />
<br />
Remainder of this page shows how to install a new module that is not included in the standard PHP distribution.<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== Extension download ==<br />
The core component '''php5ts.dll''' is generally adequate for most scripting tasks however you may need to add extensions for a special task.<br />
<br />
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.<br />
<br />
'''''[[#top | Top]]'''''<br />
=== Download ===<br />
'''''Note: This extension works only on the Mona series'''''<br />
<br />
'''Will extend this section for Nano when I find a version that runs on PHP 5.3.0'''<br />
<br />
<br />
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.<br />
<br />
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.<br />
<br />
Create a new folder named UniServer\udrive\usr\local\php\'''new_extensions''' save the downloaded file to this folder.<br />
<br />
'''''Note'':''' Any extensions you download must match the version of PHP you are using.<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
=== Install ===<br />
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.<br />
<br />
Edit file php-cli.ini and add the following content: <br />
<pre><br />
extension=php_win32ps.dll<br />
extension_dir = "./new_extensions"<br />
</pre><br />
Run the batch file (double click on Run.bat) no error messages indicate no problems with the path or loading the extension..<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
=== Test ===<br />
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.<br />
<br />
The extension lists all running processes to see this in action edit file test_1.php as follows:<br />
<pre><br />
<?php<br />
print_r(win32_ps_list_procs());<br />
echo "Hello World";<br />
exit(0); // Script ran OK<br />
?><br />
</pre><br />
Run the script by double clicking on Run.bat each process is listed with relevant information for example this small extract:<br />
<pre><br />
[31] => Array<br />
(<br />
[pid] => 2932<br />
[exe] => C:\Program Files\Mozilla Firefox\firefox.exe<br />
[mem] => Array<br />
(<br />
[page_fault_count] => 18999<br />
[peak_working_set_size] => 65421312<br />
[working_set_size] => 52396032<br />
[quota_peak_paged_pool_usage] => 119868<br />
[quota_paged_pool_usage] => 116388<br />
[quota_peak_non_paged_pool_usage] => 23704<br />
[quota_non_paged_pool_usage] => 14856<br />
[pagefile_usage] => 41562112<br />
[peak_pagefile_usage] => 55746560<br />
)<br />
[tms] => Array<br />
(<br />
[created] => 77.765<br />
[kernel] => 1.781<br />
[user] => 2.031<br />
)<br />
)<br />
</pre> <br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== Command line switches ==<br />
To view a full list of the currently available command line switches modify Run.bat as follows:<br />
<pre><br />
TITLE CLI TEST BAT<br />
COLOR B0<br />
@echo off<br />
cls<br />
echo.<br />
:usr\local\php\php.exe -n test_1.php<br />
usr\local\php\php.exe -n -h<br />
<br />
echo.<br />
pause<br />
</pre><br />
'''''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.<br />
<br />
'''''Note 2'':''' The "-n" switch followed by "-h" produces this help file: <br />
<br />
'''''Note Mona'':'''<br />
<br />
Change the path to: '''udrive\'''usr\local\php\php.exe -n -h<br />
<br />
<pre><br />
Usage: php [options] [-f] <file> [--] [args...]<br />
php [options] -r <code> [--] [args...]<br />
php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]<br />
php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]<br />
php [options] -- [args...]<br />
php [options] -a<br />
<br />
-a Run interactively<br />
-c <path>|<file> Look for php.ini file in this directory<br />
-n No php.ini file will be used<br />
-d foo[=bar] Define INI entry foo with value 'bar'<br />
-e Generate extended information for debugger/profiler<br />
-f <file> Parse and execute <file>.<br />
-h This help<br />
-i PHP information<br />
-l Syntax check only (lint)<br />
-m Show compiled in modules<br />
-r <code> Run PHP <code> without using script tags <?..?><br />
-B <begin_code> Run PHP <begin_code> before processing input lines<br />
-R <code> Run PHP <code> for every input line<br />
-F <file> Parse and execute <file> for every input line<br />
-E <end_code> Run PHP <end_code> after processing all input lines<br />
-H Hide any passed arguments from external tools.<br />
-s Display colour syntax highlighted source.<br />
-v Version number<br />
-w Display source with stripped comments and whitespace.<br />
-z <file> Load Zend extension <file>.<br />
<br />
args... Arguments passed to script. Use -- args when first argument<br />
starts with - or script is read from stdin<br />
<br />
--ini Show configuration file names<br />
<br />
--rf <name> Show information about function <name>.<br />
--rc <name> Show information about class <name>.<br />
--re <name> Show information about extension <name>.<br />
--ri <name> Show configuration for extension <name>.<br />
</pre><br />
'''''Note 3'':''' If you have problems running a script and suspect a configuration file conflict use the "'''--ini'''" swith for example:<br />
<br />
<pre><br />
TITLE CLI TEST BAT<br />
COLOR B0<br />
@echo off<br />
cls<br />
echo.<br />
:usr\local\php\php.exe -n test_1.php<br />
usr\local\php\php.exe -n --ini<br />
<br />
echo.<br />
pause<br />
</pre><br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== Summary ==<br />
Essentially that completes CLI use and configuration, remainder of this series covers some useful scripts and techniques.<br />
<br />
To repeat, generally you will not require a configuration file hence use the “-n” switch. The above extension is covered on the [[PHP CLI: Process Running |'''next page''']] its not required because UniServer has a better alternative also covered on that page.<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
----<br />
<br />
{| <br />
| [[Image:uc_small_logo.gif]] || '''MPG''' (Ric)<br />
|}<br />
<br />
[[Category: Tutorials]]<br />
[[Category: How To]]<br />
[[Category: Uniform Server 5.0-Nano]]</div>Ric