Vault9 Modding Vault9 Graphics Vault9 Code Vault9 Net Vault9 OS Vault9 Gaming Vault9 ChillZone Vault9 Tech Vault9 Archives       Vault9 Global9       Vault9 Fusion     Vault9 Blog     Vault9 Network          
 

Please Note!

This is a read-only board, no new topics or registrations are allowed. The Code Vault has moved to http://forums.vault9.net - Be sure to find more information there!


 
Reply to this topicStart new topic
> PHP Tutorials, Part 1. Using Various Exec methods
Rating  5
post Sep 17 2006, 02:12 PM
Post #1


Yay! Im a noob!
Group Icon

Group: Administration
Posts: 1,577
Joined: 4-January 03
From: Sandton, Johannesburg
Member No.: 827

Sex: Male



Hi.

I figured that I would write some PHP tutorials starting with basic techniques, move through file handling, database handling, classes and finally end up with some tricky stuff.

There are three basic exec possibilities namely the "exec()", "system" command or the `` operators. Let me start by showing you the basic ways of using exec():
CODE

<?php

// This will return the uptime on a *nix machine. This sets $uptime with a string of the output from the uptime command.
$uptime = exec('uptime');
echo $uptime;

// This will return the uptime from a *nix machine into an array called $uptime
exec('uptime',$uptime);
print_r($uptime);

// This will return the uptime from a *nix machine into an array, and the return status of the program
exec('uptime',$uptime,$ret_status');

// This will run the command specified but wont return anything to any variables
exec('uptime');


?>

Take note of the difference on how they are used. Note that if you need to run a command without accepting input back (i.e. waiting for error messages), then exec() is the way to go. The $ret_val option would be set if the program doesnt terminate properly, normally a sign of incorrect parameters given to it. When using exec() php will always wait for the program you are calling to terminate before returning to the "code" as it where.

Now for the `` (back tick operators):
CODE

<?php

// Returns the output of the program to the variable
$uptime = `uptime`;
echo $uptime;

?>

With the ``s there is only one way of using them, you cannot use them to write the output to an array, only strings are returned and you do need to specifiy a variable name else the ``s wont be interprited.

Now for the system command:
CODE

<?php

// Returns the last line of output from the command
$uptime = system('uptime');

// Returns an array of the output to the $uptime variable
system('uptime',$uptime);

// Returns an array of the output to the $uptime variable as well as the return code from the program
system('uptime',$uptime,$ret_val);

?>

This function is a copy of the C/C++ function for calling system programs (hence the name), and can be used in much the same way as exec() can.

There are more complicated functions that can be used for runing backend applications, but they are hardly used in normal day to day php coding.

A few handy hints when using exec commands and you are having problems getting the outpt you want:
1.) Always make sure you have spelt everything correctly.
2.) If you are running a program on a *nix system, make sure that the webserver has permission to run the application. The easiest way to test this, is to output the command you are trying to run and then to (at the command line of the unit), run the followin command: su www (substitute www for the user your webserver runs as), and then run the command you are having problems with. 90% of the time the problem lies with permissions
3.) print_r and echo are your best friends. Use them alot, it always helps to debug and they can be removed simply once done.

Well that concludes the first in the series. Expect another one sometime soon dealing with File handling!

Cheers
User is offlineProfile CardPM
+Quote PostGo to the top of the page
post Sep 17 2006, 02:12 PM
Post #


Member


Group: Administration

Joined: Today, 03:30 AM





Quote PostGo to the top of the page

Reply to this topicStart new topic

Collapse

> Similar Topics

Topic Title Replies Topic Starter Views Last Action
Programming Tutorials 0 simply_coool 1,812 Sep 18 2006, 02:16 AM
Last post by: simply_coool
Ebook and Tutorials 2 simply_coool 1,605 Sep 13 2006, 09:31 PM
Last post by: CrEaTi0n
E-books And Tutorials 13 Rush 1,865 Dec 12 2005, 06:22 AM
Last post by: Britz
ASP News->ASP.NET 2.0 QuickStart Tutorials 0 Vault9 502 Nov 7 2005, 11:14 AM
Last post by: Vault9
Vb Tutorials 2 Zaa 622 Feb 4 2005, 02:50 PM
Last post by: PyroSA


 



- Lo-Fi Version Time is now: 22nd March 2010 - 03:30 AM
Privacy Policy
South Africa's Top Sites Kinetiq