Help - Search - Members - Calendar
Full Version: PHP Tutorial Part 1
Vault9 Forums > Tech Den > Binary Refinery > Codemonkeys

Custom Search
CrEaTi0n
* NOTE THIS IS COPIED FROM CODE.VAULT9.NET *

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
cyfermaster
OMG, Some stuff that I can use to learn. smile.gif me bookmarks this page. smile.gif Thanks.
CyberStorm
QUOTE
OMG, Some stuff that I can use to learn. me bookmarks this page. Thanks.

Yes, nice tutorial. Will study it greater detail when I get home.

Hey cyfer, thought all the admins knew this stuff.. unsure.gif
cyfermaster
QUOTE
Hey cyfer, thought all the admins knew this stuff..

We don't know everything. sad.gif Just most things. biggrin.gif
CyberStorm
QUOTE
We don't know everything. Just most things.


Thats what everyone in IT says! LOL!
cyfermaster
Yes, but I mean it. bleh.gif
CrEaTi0n
Stay tuned for part two coming soon smile.gif
This is a "lo-fi" version of our main content. To view the full version with more information, formatting and images, please click here.
Custom Search
Invision Power Board © 2001-2008 Invision Power Services, Inc.