How to open an external GUI application from web browser using PHP? - php

I'm new to web development. I'm trying to execute a shell script using PHP's shell_exec(). Inside the script, I'm trying to invoke a GUI application(Qt). When I executed the PHP script from a terminal the application started as expected. But when I opened it from browser an empty blank page appeared.
I'm using Ubuntu with apache2 server running as service. When I searched in google, the similar problem is solved in the Windows environment by allowing apache service to interact with the desktop.
PHP Script:
<?php
$log = shell_exec('sh testcmd.sh');
?>
testcmd.sh:
./Program1
Any help provided will be highly appreciated.

It is somewhat unclear what you're asking.
If you wish that browsing to a certain web site will run a PHP script that will open a GUI app for the client to interact with, the answer is "you can't". The reason is that the way the setup works is that the server and the client run on different machines, and your PHP runs on the server machine. As such, the client never gets to see the running program.
The above is true also for Windows. The answer you quote in your question does not apply to a server running on a different machine than the client.
If, for whatever reason, you want something that works only when the server and client run on the same machine (or there is someone watching the server's display), then you need to do the equivalent of the Windows answer.
The graphics display on Linux (assuming you're not running wayland) is using a protocol called X11. In order for the display to appear, your GUI program needs two things. The first is to know which display it needs to use. This is supplied with an environment variable called DISPLAY. The second is an authorization to actually use that display.
So in order for your PHP script to run a GUI app that will show its GUI, you will need to first do the following steps:
Set the DISPLAY variable to the correct value (copy from your desktop environment).
Run xauth add something, where you can get what something is by running xauth list on your desktop environment.
If you do these two things (in this order), your GUI should show up.

Related

Does HTML5 wih php require a webserver?

I am studying on YouTube HTML5 forms with PHP files; the videos either start with a web page containing the form and link to a PHP file or they put everything in php files; I copy the instructors exact files; but they do not work: nothing is posted after filling the form and hitting the submit button.
I have had the files on a USB flash drive then tried them actually on the computer: nothing.
My question is then: "Do I need to have an actual server on my computer in order for PHP files to function?"
Yes. PHP requires a web server to run on.
You can run it on your own computer; the web server doesn't have to be connected to the internet while you are creating and testing your PHP scripts off your local machine. You can read more about this on the official PHP What do I need page
You can download one of the following local servers:
Windows: WAMP
OSX: MAMP
Linux: LAMP
XAMPP is also an option, it's cross-platform (as referenced by ATechGuy)
Here is a good explanation of Why a web server is required to run PHP. Basically it is because PHP is a dynamic server-side scripting language.
However if you just want to run simple PHP scripts, with no web pages. This is possible without a web server running. See this question: How can I run a php without a web server?
Yes, PHP will need to run on a server in order to execute. If you have a Mac, it should be equipped to run an Apache server. I found this article to be really helpful when developing with PHP - https://jason.pureconcepts.net/2015/10/install-apache-php-mysql-mac-os-x-el-capitan/
Yes. PHP files contain code that must be handled by an interpreter, that is, a program that reads the PHP code and outputs accordingly. This can be done without a webserver (using command line php) but PHP is most commonly used with a web server.
You want to setup some sort of stack with a web server and php. A lot of beginners use apache as a web server, and since you are comfortable using youtube for learning, a simple search for "apache php" and your operating system.

Testing my php scripts and forms

I have two html forms and two php scripts. The first one is a form which is supposed to submit a users email to a .txt file, and the other is a Stripe payment form which uses php code to charge the customer.
Now my problem is that there are some issues with the two php scripts, that I can't figure out how to fix, because I am not really sure how to test the scripts. Normally when testing the html scripts I would just open the html files in my browser, but that doesn't work on my php scripts as the site just shows what is written in the scripts when called/submitted.
So my question is how do I test my scripts, without having to use a hosting account and can I even test it like this?
You need to run a web server on your local computer to test it out. I would suggest looking into something called Vagrant, which allows you to fairly easily create virtual machines on your computer in which you can install anything you like without fear of messing up anything else. If you go here you can even find a "box" to create a virtual machine that already has apache and php installed.
Depending on your version of PHP there's a webserver builtin right into PHP:
http://php.net/manual/en/features.commandline.webserver.php
You probably have to install a web server locally with php enabled. Since php is a server side language it needs a server to run on, in order to send you back the html, after execution. If you need to see the result on a browser.
http://en.wikipedia.org/wiki/LAMP_%28software_bundle%29.
Or run your script from terminal (you must have php installed) if you don't need to see the result in the browser and just want to run your script.
$ php myscript.php

PHP exec and OSASCRIPT?

I have a little Apple script as follow:
beep
delay 2
tell application "Finder" to activate
It just makes a sound, wait 2 second and then bring the "Finder" window to the foreground.
When I run it from the command line, it works fine.
Then I want PHP to call that script using the exec() php function.
<?
$cmd = "/usr/bin/osascript \"myscript.scpt\"";
exec($cmd);
?>
It still works fine.
But when I call that same PHP script from the browser, it doesn't work! The PHP starts, the Apple script starts as well since I can hear the beep sound but its last line is not executed.
I thought that would be an environment variable thing so I made sure they were all the same way as in the terminal:
$cmd = "HOME='/Users/mikael' && … && /usr/bin/osascript \"myscript.scpt\"";
The variables are set properly (as check with env|sort) but still no luck with running my apple script inside a php script displayed in the browser and using the standard MacOS apache stuff.
Any idea?
When osascript runs from PHP, through the web server, it's not running with a login context, so it can't send Apple events to applications running on the desktop (like the Finder). You'll find that a similar issue arises if you try to use osascript over SSH.
Login contexts are a complex, poorly documented area of OS X. You may want to get your hands on a copy of Amit Singh's Mac OS X Internals: A Systems Approach if you want to learn more about them.
If you don't, though, the answer is generally pretty simple: don't depend on osascript working correctly from the web server.
OK, I may have found a way that allows the Apple script to be called from PHP from within a browser.
It is not fully satisfactory but this is what I'm going to do:
So basically instead of using the default macOS apache server, I use this one that sets up a web server & also MySQL: http://www.mamp.info/en/index.html
I mention MAMP but there may be other.
my Apple script is finally run fully using that solution.
I had the same trouble and realised that apache executes as user www.
You can change this by editing etc/apache2/httpd.conf . Change user to your user short name and group to staff.

PHP - exec,shell_exec,system not working

i read so many similar questions but nothing works with me
im using wamp 2.2 ,Apache 2.0 , PHP V5.3.8
safe_mode=off - disabled_functions deleted from php.ini i'm trying to exec
exec("chrome.exe google.com");
nothing happens and when i try
exec("calc");
the windows shows weird message to execute calc in different user although i changed the user for the apache service to Administrator and i verify the user using
exec("whoami");
where is the problem?
Environment path for CMD and php shell might not be the same.
You either have to give entire path of the file, or export the path of chrome.exe in php first
Also, I believe that chrome.exe opening a page requires XServer, php can't open graphical interfaces in shell. These commands are not passed to actual shell as a parent, so php file can't "launch" applications for you.
Please read specifics of the exec function:
PHP exec Reference
Also try adding 2> errors.txt to see what are the errors if any in the execution of the programme.
--
The program needs to know what X server to connect to, and it needs to have permissions to connect to that server. You specify the X server with the DISPLAY environment variable; this will usually be set automatically if you are running the PHP program from a terminal in
X, or from a GNOME panel or something similar; however, if you are running the PHP script in some other manner, it likely won't know what X server to connect to.
X has various ways of specifying permission to connect to a server, but the most common one is using a file called ".Xauthority" in the users home directory. Because only the user who is logged in at the X server can read this file, they are the only user who can run GUI programs. So, if you start the PHP user as the same user who is logged in at the X server, you shouldn't have any problem with permissions. However, if the PHP program is running as a different user, you will have to give that user permission to access the X server.
Reference: http://bytes.com/topic/php/answers/838364-cant-launch-graphical-apps-php-exec-ubuntu-8-04-system
(I know that link is for linux and won't have exact same solution for Windows, but exec() still needs to know which X interface to refer to)
Stop Apache running as a service.
When windows runs a service it it is not running directly as the user that started the server or manages due to this it could well be starting chrome in a service environment so you wont see it load on your desktop there are 2 ways to get around this
Stop apache service browse to your apache directory and run httpd.exe manualy then try your script it should work or if it is not required to be running though a web request so it not using anything from the browser you can allow it to work with c:\wamp\php\php.exe yourfile.php (your php path should be replaced for c:\wamp\php)
Try PHP script?
<?php shell_exec('notepad.exe');?>
It's working.

Why can't I use shell_exec from a PHP script on a network share in IIS?

I have some PHP scripts that reside on a network share. For argument's sake, let's call the share \\nas\dev. I have a Web Site on \\w2k3dev\ set up in IIS6 that uses \\nas\dev\ as its home directory by setting "A share located on another computer".
Some of my scripts use shell_exec to execute functions on the server and return results to my script. Again for argument's sake let's say I'm just getting a directory listing like so:
echo shell_exec('dir');
If I run the script in IIS, I get no output - but no error logs either.
If I set up Apache on \\w2k3dev\ and configure it to use the same share, the script runs fine
If I copy the script to the local machine and run it through IIS it works fine.
So the problem only seems to be when IIS is set up to use a network share with a script containing shell_exec. Every other aspect of my scripts work fine.
Profiling in procmon, I can see that cmd.exe operates completely differently depending on whether it's being run by IIS or Apache. I have more details, but for the sake of being concise, I will omit them for now unless someone asks for more details.
Thank you so much for looking at this, I am at my wits end.
Kind regards
Iain
You may have a look at those IIS specific comments on php.net shell_exec's manual :
http://www.php.net/manual/en/function.shell-exec.php#84992
http://www.php.net/manual/en/function.shell-exec.php#70817
http://www.php.net/manual/en/function.shell-exec.php#70338
http://www.php.net/manual/en/function.shell-exec.php#43907

Categories