Technology

Using the shell_exec function to integrate PHP and server software

Posted by Pixafy Team

Have you ever had a problem when programming in PHP, for which PHP just didn’t have the muscle to get the job done?  For example, complex algorithms that take too long to process or need to do something that PHP just can’t do.

The shell_exec function makes all your wildest PHP programming dreams come true.  You can use this function to get real time GPS tracking information, and complicated bin-packing algorithms can be easily integrated with any cart solution written in PHP. This function, when combined with AJAX and a game engine, could even allow you to produce an online game that connects into the game server via the web browser, thus avoiding flash or java applets.  The possibilities are limitless!

The power of this function is that it allows you to use the Unix terminal to send commands and arguments down to software on the server and get a response back from the software.

A very basic example:

- index.php

<?php
echo shell_exec("hostname");
?>

The response you would get back would be the hostname of the server that your code is being executed on.

Another example could be passing session data down into a custom piece of software that returns data related to that user:

<?php echo shell_exec("./hello –u Dan");

The hello binary, which was written in C++, is a simple program that just returns the –u argument with a string concatenated to the end of it.

- main.cpp

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main(int argc,char** argv){
stringstream user;
string username;

for(int i = 0;i < argc;i++){
if(i >0){
stringstream args;
string arg;
args << argv[i];
arg = args.str();
if(arg == "-u"){
user << argv[i+1];
}
}
}

username = user.str();
if(username == "Dan"){
cout << username << " really like to program!" << endl;
}else if(username == "Eric"){
cout << username << " really like to manage projects!" << endl;
}else if(username == "Jason"){
cout << username << " is a great programmer!" << endl;
}else if(username == "Uri"){
cout << username << " rules over the servers!" << endl;
}else{
cout << username << " is awesome at what they do!";
}
return 0;
}

Again, this is an extremely basic example; however, we are going make it a little more complicated by sending data from a form to the CPP program and back up to the browser.

Using the same principles that we applied in the last example:

-index.phtml

<?php

if(isset($_POST['name'])){
$callback = shell_exec("./hello -u".addslashes($_POST['name']));
}

?>

<html>
<head>
<title>What's your name?</title>
<style>

.callback{
position:relative;
float:left;
width:800;
margin-left:50%;
left: -400px;
}

.clear{
clear:both;
}

.form{
position:relative;
float:left;
width:800;
margin-left:50%;
left: -400px;
}

.form-row{
position:relative;
float:left;
width:100%;
height:10px;
padding:10px;
}

.label{
position:relative;
float:left;
font-size:13pt;
font-weight:900;
}

.input{
position:relative;
float:left;
margin-left:10px;
}

</style>
</head>
<body>
<div>
<?php if(isset($callback)){
echo $callback;
}
?>
</div>
<div></div>
<div>
<form action="index.php" method="post">
<div>
<div>What's Your Name?</div>
<div><input type="text" name="name" value="" /></div>
</div>
<div></div>
<div>
<div></div>
<input type="submit" name="submit" value="Submit" />
</div>
</form>
</div>
</body>
</html>


Leave us your comments below or tweet us @Pixafy!