https://wiki.uniformserver.com/index.php?title=PHP_WinBinder_4:_Command_Processing&feed=atom&action=historyPHP WinBinder 4: Command Processing - Revision history2024-03-28T10:22:42ZRevision history for this page on the wikiMediaWiki 1.41.0https://wiki.uniformserver.com/index.php?title=PHP_WinBinder_4:_Command_Processing&diff=4235&oldid=prevRic: New page: {{Nav PHP WinBinder 4}} '''''WinBinder Part 4 - Command Processing''''' == Introduction == Our tray menu is currently a pleasing cosmetic curiosity and performs no useful function. After ...2010-05-25T21:00:13Z<p>New page: {{Nav PHP WinBinder 4}} '''''WinBinder Part 4 - Command Processing''''' == Introduction == Our tray menu is currently a pleasing cosmetic curiosity and performs no useful function. After ...</p>
<p><b>New page</b></p><div>{{Nav PHP WinBinder 4}}<br />
'''''WinBinder Part 4 - Command Processing'''''<br />
== Introduction ==<br />
Our tray menu is currently a pleasing cosmetic curiosity and performs no useful function.<br />
<br />
After all it’s supposed to launch applications this part of the tutorial covers various method using PHP to do this.<br />
<br />
== Detached process ==<br />
=== Problem ===<br />
PHP functions such as exec(), system(), passthru() and even back ticks start a process and wait for any data to be returned from the process before continuing onto the next instruction. <br />
Waiting for a process to complete, clearly is not desirable for our tray menu.<br />
<br />
=== Solution ===<br />
Any process our tray-menu starts must be detached. I covered a solution to this problem sometime ago see page [[PHP CLI: Detached Processes | '''Detached Processes''']] for details.<br />
<br />
For convenience I have reproduced the solution below:<br />
<br />
A neat one line solution is provided using the PHP function popen(). <br />
{|<br />
|-<br />
|<br />
<pre><br />
<br />
pclose(popen($command,'r'));<br />
<br />
</pre><br />
|<br />
&nbsp;<br />
|<br />
* It creates a pipe ('''popen''') to a process for reading ('''‘r’''')<br />
* Process is specified by '''$command'''<br />
* Instantly closes ('''pclose''') the file pointer created hence '''detaching the process'''.<br />
|}<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== Commands ==<br />
So what can be passed to the above function more importantly what do we require for a command processing function?<br />
<br />
Answer is to run a few test cases for example:<br />
<br />
Edit file '''test_case_2.phpw''' un-comment one of the command ($cmd) lines and run '''test_case_2.bat'''<br />
{|<br />
|-<br />
|<br />
<pre><br />
<?php<br />
chdir(dirname(__FILE__)); // Change wd to this files location<br />
<br />
//$cmd = "start \"Test 1\" cmd.exe "; // open a command window set title<br />
//$cmd = "start cmd.exe "; // open a command window<br />
//$cmd = "start notepad.exe "; // open notepad<br />
//$cmd = "start notepad.exe test.txt"; // open a text file into notepad<br />
//$cmd = "start test.txt"; // open file text.txt in default editor<br />
//$cmd = "start http://localhost/"; // open file index.* in local browser<br />
//$cmd = "start ../test.txt"; // open file text.txt in default editor<br />
// one folder up relative to this test file<br />
//$cmd = "start notepad.exe \"C:/z test/test.txt\""; // Space in path to file<br />
<br />
//$cmd = "start php.exe -n rollover_1.phpw"; // Run Winbinder PHP script<br />
// Command window displayed<br />
<br />
//$cmd = "start php-win.exe -n rollover_1.phpw"; // Run Winbinder PHP script<br />
// Command window not displayed<br />
<br />
//=== Examples using a PHP configuration file <br />
//$cmd = "start php.exe -c php-wb.ini rollover_1.phpw"; // Run Winbinder PHP script<br />
// Command window displayed<br />
<br />
//$cmd = "start php-win.exe -c php-wb.ini rollover_1.phpw"; // Run Winbinder PHP script<br />
// Command window not displayed<br />
<br />
pclose(popen($cmd,'r')); // Run a detatched process<br />
</pre><br />
|}<br />
If the penny has not dropped there is no real difference between command format.<br />
<br />
They all require a path to the executable either directly or implied and an optional parameter. <br />
<br />
Since there is no difference our action options reduce<br />
* From shell, run, sub, separator<br />
* To run, sub, separator<br />
<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
== Hidden Process ==<br />
Perhaps a hidden process requires a special command entry. <br />
<br />
Most processes are easy to hide use one of the following methods:<br />
{|<br />
|-<br />
|<pre><br />
$cmd = "start /b php.exe -n rollover_1.phpw"; <br />
</pre><br />
|<br />
&nbsp;<br />
|<br />
Script is run in the background any scripts run from this inherits the hidden environment hence are hidden.<br />
|-<br />
|<pre><br />
$cmd = "start php-win.exe -n rollover_1.phpw"; <br />
</pre><br />
|<br />
&nbsp;<br />
|<br />
The command window is hidden and the windows application is visible<br />
|}<br />
Reason for showing the above is to look at the command format. Again in terms of the command processing there is nothing special required.<br />
=== Persistent ===<br />
The following issues may require a special command entry. <br />
<br />
Certain processes are very persistent and are difficult to hide in particular Apache when run as a standard program. It opens a new command window and runs in that.<br />
<br />
Similarly if a process runs an infinite loop within a command window that window will remain visible.<br />
Another annoying scenario is where a process periodically fires-up and produces a pop-up command window to run a cron process. <br />
<br />
Solution is to use Uniform Server's utility '''''uniserv.exe''''' (2.5k) this was specifically designed to run Apache in the background hidden and can be used to resolve the above issues. <br />
Once a process is hidden it indirectly hides the task bar icon.<br />
<pre><br />
$cmd = "start uniserv.exe \"php.exe test_case_3_a.phpw\""; // Hide app <br />
</pre><br />
In terms of command format there is no difference. <br />
<br />
However from a user point of view could use a command such as “runh” for run hidden.<br />
<br />
'''''[[#top | Top]]'''''<br />
=== Test Example ===<br />
The following example shows how to use Uniform Server’s process hide utility.<br />
{|<br />
|-valign="top"<br />
|<br />
{|<br />
|-<br />
|<br />
'''test_case_3.bat'''<br />
|-<br />
|<br />
<pre><br />
php.exe -n test_case_3.phpw<br />
pause<br />
</pre><br />
|-<br />
|<br />
'''test_case_3.phpw'''<br />
|-<br />
|<br />
<pre><br />
<?php<br />
chdir(dirname(__FILE__)); // Change wd to this files location<br />
<br />
//$cmd = "start php.exe -n test_case_3_a.phpw"; // #1 <br />
//$cmd = "start uniserv.exe \"php.exe -n test_case_3_a.phpw\""; // #2 <br />
<br />
pclose(popen($cmd,'r')); // Run a detatched process<br />
?><br />
</pre><br />
|-<br />
|<br />
'''test_case_2.phpw'''<br />
|-<br />
|<br />
<pre><br />
<?php<br />
chdir(dirname(__FILE__)); // Change wd<br />
<br />
$loop=0;<br />
while($loop <= 10 ){<br />
sleep(1);<br />
file_put_contents("z_test.txt",$loop,FILE_APPEND);<br />
$loop = $loop+1;<br />
}<br />
?><br />
</pre><br />
|}<br />
|<br />
&nbsp;<br />
|<br />
'''''test_case_3.bat'''''<br />
<br />
Runs script test_case_3.phpw (batch file required only for testing)<br />
<br />
'''test_case_3.phpw'''<br />
<br />
Runs script test_case_3_a.phpw <br />
<br />
'''''Test 1'':'''<br />
* Un-comment line #1 and run the batch file test_case_3.bat<br />
* A command window opens and runs script test_case_3.phpw.<br />
* Shortly after a second command window opens. Showing script test_case_3_a.phpw is running.<br />
* This can be confirmed because it creates a file z_test.txt<br />
* After ten seconds script completes and the command window automatically closes.<br />
<br />
<br />
'''''Test 2'':'''<br />
* Delete file z_test.txt <br />
* Un-comment line #2 and run the batch file test_case_3.bat<br />
* A command window opens and runs script test_case_3.phpw.<br />
* Shortly after script test_case_3_a.phpw is run.<br />
* Note: This time a second command window does not opens (runs in background). <br />
* To confirm test_case_3_a.phpw has been run check file z_test.txt was created<br />
<br />
Note: In both cases instantly returns to script test_case_3.bat because script test_case_3_a.phpw is detatched.<br />
<br />
|}<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
=== Pattern ===<br />
There should be no difference in text entered by a user to run a command other than the commadnd itself (run or runh) <br />
Is this acheiveable? I have rearranged the two lines as shown below:<br />
<pre><br />
$cmd = "start php.exe -n test_case_3_a.phpw " ; <br />
$cmd = "start uniserv.exe \" php.exe -n test_case_3_a.phpw \" "; <br />
</pre><br />
Ignoring text to the left and right of '''''php.exe -n test_case_3_a.phpw''''' demonstates there is no problem<br />
<br />
'''''[[#top | Top]]'''''<br />
== Summary ==<br />
The above has shown how to read a configuration file into an array.<br />
<br />
This array is scanned extracted data is combined with positioning information to form a master array.<br />
<br />
Our tray-menu contains unresolved details such as what elements to include in the master array and configuration file. Next page covers [[PHP WinBinder 4: Coding | '''coding''']] and puts a stake in the ground.<br />
<br />
'''''[[#top | Top]]'''''<br />
<br />
----<br />
<br />
[[Category: Tutorials]]<br />
[[Category: How To]]<br />
[[Category: Uniform Server 5.0-Nano]]</div>Ric