Switch focus app using PHP in local Windows - php

I want to change focus window from browser to another app using PHP, on Windows local machine.
I want to make a scenario, like:
User will access via browser, input some code. If the code is correct then PHP will change focus window from browser to Microsoft Word (for example). Only change the focus window, Ms Word already started.
Thinking of using system(), exec(), or shell_exec() in php to run a .bat file, but still didn't work.
swith-to-word.bat
echo new ActiveXObject("WScript.Shell").AppActivate("Microsoft Word"); > tmp.js
cscript //nologo tmp.js & del tmp.js
php
system('cmd /c C:\Users\User\Sites\admin\public\commands\swith-to-word.bat');
I want to run this scenario only in server locally, won't be accessed by another client.
Anyone has suggestions to this scenario?
Thank you in advance

Related

How to open an external GUI application from web browser using 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.

running exe app using php exec funtion make the page hangs

I am using apache server under xampp. I have some matlab exe file that I want to execute. I used this template
$tmp = exec($command, $output, $return_var);
while $command contains the exact command to execute the file using cmd.
What is happening is the page hangs and when I debug, I found that the server hang while calling this exec command.
I searched the web and tried many things like run the Apache service and my user account and give the user all administrator privileges, but unfortunately it still stuck.
Any help or advice would be appreciated.
The problem with my code was because the Matlab code is accessing file system and also reads an excel sheet. So, it was all about permissions.
The solution for this problem has 2 parts:
1. Apache should run as a a windows service with checking allow service to interact with desktop in the properties.
2. I found that there is no way to access windows COM objects to read excel in normal way. So, instead we should use xlsread in basic mode.
That's all.

Using PDFCreator's Command Line Arguments in PHP exec()

I have tried to use the Zend Framework to do my PDF conversion in PHP but cannot figure out how to install it. I found this program that allows for PDFs to be printed/converted via the command line, so I thought I could do an exec() command in PHP to run the program and convert my file on the server for me.
The program is PDFCreator from pdfforge.org and I am trying to convert .DOC files to .PDF files. This command line below works perfectly when running from cmd.exe. However, when running it from PHP I believe that it is trying to execute under the user System and not working properly. (I did a php exec('whoami') and it came back as System.) I also changed the user of which Apache opens up as to an Admin account and still have been unable to get it to work.
"C:\Program Files\PDFCreator\PDFCreator.exe" /OptionsFile"C:\PDFCreator.ini" /PF"C:\test.doc"
Any thoughts as to why it I cannot get the same behavior when running the command using exec()?
Thats because Apache user and account user should be the same, ex: your windows user is Pedro, so Apache user should be Pedro...you shoul open the services.msc and in the session tab allow apache to interact with the current desktop....in that way, the share the same windows session and desktop....but it shoud represent a security risk and it's not recommended.
Saludos.

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.

Run a PHP script from multiple computers?

I have written a PHP Script, I run the code like this on MS-DOS on my local PC:
C:\wamp\bin\php\php5.3.0\php.exe index.php
Now I want to upload the index.php on the web but on multiple computers (without PHP/Apache installed), how can I run index.php on ms-dos via web?
If that not possible, I can open multiple putty application on each computer and login ssh?
index.php will continues looping - it wont be good to run script on the browser because it may cause timeout via browser.
You can't. If you're using putty on each computer to log into another machine to run the script, the script will still be running on "the other" machine. All you've done is make its output appear on the local computer that's running putty. It'd be exactly the same as opening multiple shells on the server and running the script in each window.
If you want a PHP script to run "locally", then you need PHP installed locally on each machine that will run the script. And like I said in your other similar question, there are PHP compilers which can produce a .exe that MIGHT be portable, but it's not guaranteed to work. PHP can be compiled, but not all scripts are compileable.
Check out bamcompile.
It lets you compile the PHP file to an exe.
Not everything works but it is worth the shot: http://www.bambalam.se/bamcompile/
I think it uses PHP 4.something so don't know wether it works for your script without seeing it
EDIT
You said you are worried about an timeout if ran in browser.
What does the script do?
Perhaps PHP isn't you're best option.
The easiest way would be to have Apache and PHP installed on those computers, and then have a script visible on that computers that runs the command for you.
exec("C:\wamp\bin\php\php5.3.0\php.exe index.php")
This way, you can start running the script without having the script time out from Apache's end.

Categories