Solved

Problem running external program with php

Posted on 2006-07-04
34
291 Views
Last Modified: 2008-02-01
Hello everyone!!

Have a little problem. I'm trying to run a program by clicking a buttonion a php web page. When i run it in a shell (I work with Suse linux 9.1), it works perfect . But the same call with php functions(exec(), passthru(), shell_exec...) won't work. Why?
Standard linux shell commands like whoami or pwd work when called throught php. But none of my personal program or commercial ones runs when called by php functions. Any idea? (safe_mode is off in php.ini).
0
Comment
Question by:KCTeam
  • 14
  • 13
  • 7
34 Comments
 
LVL 40

Expert Comment

by:RQuadling
ID: 17037082
Where are the programs installed? PHP runs on the server, so the programs need to be installed on the server.

0
 
LVL 40

Expert Comment

by:RQuadling
ID: 17037083
And I would make sure you supply the FULL path to the program rather than relying on any shell path parsing.
0
 

Author Comment

by:KCTeam
ID: 17037134
Thx for the fast answers. Actually PHP and the programs are on the same server. And i do use absolute path when calling the programs.
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 17037288
Ok, turn on full error reporting in your PHP script.

<?php
error_reporting(E_ALL);
?>

and capture the output of your command ...

<?php
// Full debugging.
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Run an external program.
$s_command = '/usr/bin/my_prog param1 param2'; // Completely made up this line. Give us an example.
$s_output = shell_exec($s_command);

// What was reported.
echo "<pre>$s_output</pre>";
?>

Can you now tell us the command and the output.
0
 

Author Comment

by:KCTeam
ID: 17037392
i tryed first with the "whoami" command and the output was "wwwrun" which is totaly normal.
then i try with $s_command = '/home/$mylogin/bin/Project1' : Project1 is the name of my program, and it's located in /home.$mylogin/bin which is a valid directory. This time, nothing. No output at all. What do you think?
0
 

Author Comment

by:KCTeam
ID: 17037480
another try with the commercial kylix program  $s_command = '/usr/local/bin/startdelphi &'.
No output and the program won't launch.
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 17039244
Are you sure the user wwwrun has the proper permissions to run these binaries?
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 17039261
Delphi is an X-Windows program isn't it? That will never work since there will not be a display available. That might be the problem with Project1 as well?
0
 

Author Comment

by:KCTeam
ID: 17040616
about wwwrun, it's the apache daemon. Startdelphi and Project1 are linux programs. StartDelphi is the launcher of kylis which is the linux version of delphi. Both program run perfect when launched from a shell or by clicking on their icons. I can't even launch standard programs like firefox, or the gimp with this method.
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 17040868
Like I said, of course not. wwwrun does not have a display available. A display is needed for GUI programs to run since they are X-Windows programs. It could only work if you would login to X-Windows as wwwrun. That will not work either since that user is not allowed to. In short: X-Windows programs can NEVER be started from PHP when you initiate it from browser.
0
 

Author Comment

by:KCTeam
ID: 17040992
OOOOOOOOOOHH MY!!!!
you're damn right. What was i thinking!!! I think i have my answer. I'll try with no Gui Programs and i'll let you know if it finally works.
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 17040998
Should be working alright ;)
0
 

Author Comment

by:KCTeam
ID: 17042059
Let's start over. Now i have a program with no gui that access mysql DB and does a few thing in it like "create table..", "select...", "insert"....
howdo i launch it via php? i tryed to use :
<?php
// Full debugging.
error_reporting(E_ALL);
ini_set('display_errors', 1);

// Run an external program.
$s_command = '/usr/bin/my_prog param1 param2'; // Completely made up this line. Give us an example.
$s_output = shell_exec($s_command);

// What was reported.
echo "<pre>$s_output</pre>";
?>
replacing command's path with full path of my new program. how come i still can't launch my program and i stiil don't get any output?
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 17042827
What is your program written in? Even though it may not USE a gui, it may fail to load if the GUI is unavailable. Maybe try logging in as the wwwrun user (don't know if that is possible). Then manually run the program from the command line/shell.

Does that work? If not, then it pretty much points to the permissions of prog_x not allowing wwwrun to execute it.
0
 

Author Comment

by:KCTeam
ID: 17043360
no way to log in as wwwrun because it's apache daemon's login as a virtual user, i think.
but the prog runs when launched in my shell and logged as a standard user.
My program 's been  written with kylix(linux version of delphi)..

0
 
LVL 29

Expert Comment

by:TeRReF
ID: 17043969
Like I said, you cannot login as wwwuser indeed. Have a look at the system logs. They will tell you what went wrong.
0
 

Author Comment

by:KCTeam
ID: 17044036
Once you had linux installed, did you configure something to be able to call programs from php pages? (user permissions, apache config..... whateva...)
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 29

Expert Comment

by:TeRReF
ID: 17044097
I use FreeBSD, but in this case it works the same.
There is 2 things that you need to do in order to run an external app via PHP.
1. Make sure the executable  is runnable by the webserver process by setting the proper permissions with chmod (755)
2. Make sure it does not use any X-Windows libraries that need a display available (In *nix programs that are run under X-Windows use a 'display' to make themselves visible (hope that explanation makes sense))

Of course it might be possible to run the programs if you use the php-cli version (Command line interface) That process runs under the currently logged in user...

0
 
LVL 29

Expert Comment

by:TeRReF
ID: 17050044
Any luck yet?
0
 

Author Comment

by:KCTeam
ID: 17080708
hello guys. I'm back with my php issues. I tryed to use php-cli as you advised me. I created a php executable file in which i launch a program with exec, shell_exec,system and passthru(i tryed all four). When used with php in a console, everything's ok. But once called from the web ( with full path), program won't start. I precise that my programs have no gui. one access mysql db, creates new db, create table and insert values, the sleep for 5 seconds. The other one just writes "program launched", sleeps 5 seconds ,write "prog will exit " and finally exits.

Questions: - Is php-cli bug-free? - are there any config to add for cli to work? - I read a few stuff about suphp and how to get permissions to execute external programs. do you know annything about that? if so and if it can help resolving my problem, can you tell what i have to do ...

Thx!!
 
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 17080852
Can you try your code on another server or workstation?
0
 

Author Comment

by:KCTeam
ID: 17080914
i already did! my programs work perfect from the shell, as they're very simple ones. But, once called from the web ... :-(. Do you have a little program that you call from the web using php? If you do, can i try it on my workstation to make it clear that my program are not the ones making trooouble...
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 17081770
You can download a hello world program I wrote quickly in C from here:
http://www.xs4all.nl/~cits/hello.tar

Untar it, chmod it to 777 (just to be sure :)
And try to run it from PHP
0
 
LVL 29

Accepted Solution

by:
TeRReF earned 500 total points
ID: 17081868
If you look into your Kylix code, you'll see that there is a 'uses' line somewhere on top. Make sure you remove and units you don't need from there!
0
 

Author Comment

by:KCTeam
ID: 17082437
this is what i did with the hello you sent me:
s_command = "/home/eh/hello";
system($s_command) or die("NO");

The output is "NO";
I think we are now sure that the problem doesn't come from my programs.  Right?
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 17082583
It is runnable from command line?

And is it runnable when you run it with full path:
/home/eh/hello
0
 

Author Comment

by:KCTeam
ID: 17082740
oh!!! apparently there's a missing shared library for hello.
"/home/eh/hello: error while loading shared libraries: libstdc++.so.6: cannot open shared object file: No such file or directory".
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 17083557
Oh darn...
You will have to compile it yourself then..
Put this in hello.c

#include<stdio.h>

main()
{
    printf("Hello World");
}

And compile it with a c or c++ compiler

Try this from command line:
c++ hello.c

If all went well, there should be an executable called a.out in the same dir. Rename that to hello and try again
(both from command line and PHP)
0
 

Author Comment

by:KCTeam
ID: 17089365
TeRReF, you are the man!!!! Great advice about the unused units in my program. All the php executioon functions works know. The fact is i had some unit weren't needed to execut e my program. So THX a lot. By the way, how did you know about thaht weird "useless units" thing? I mean why doesn't my program work because of some units that are not even used to make the call?
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 17089401
If one of the units includes a compiler directive that says only run within a GUI, then when you try to load it, it will fail to load.
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 17089430
I used to program in Delphi a lot. Not one of my favorite languages I must say. Although the compiler is very fast, it doesn't see some crucial errors you can make while programming causing access violations that were VERY hard to debug.

But who am I :))
0
 
LVL 40

Expert Comment

by:RQuadling
ID: 17089464
Yeah. You'd release the code and a user would phone with the details.

Unless you had the debug version of EXACTLY the same release (i.e. not a single line of code change and not even a recompile), then the debugger would completely lose it.

But that's the problem with optimization. The code that executes can quite often bear no relationship to your code.

e.g.

A loop from 0 to 10 which has no impact outside of the loop will sometimes execute 10 to 0 as doing a decrement and compare to 0 is quicker than incrementing and comparing an integer!

Took me a while to work out WTF as I was stepping through the code.

I liked Delphi for the IDE and a pretty solid Object model. Not perfect in all regards, but a lot better than VB at the time (Delphi 3). But as Borland is selling off the IDE, I don't know what future Delphi has. Kylix has more of less gone now too I think. Shame. I cut my teeth with Borland Turbo C in the 80's 90's.
0
 

Author Comment

by:KCTeam
ID: 17090136
Ok. Sounds good! Anyway, thx for the big help. C ya!
0
 
LVL 29

Expert Comment

by:TeRReF
ID: 17090196
You're welcome!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now