6

i need to run a web browser (chrome - firefox ..) using exec

i have tried to do it using bat file (this method mentioned here)

C:\Users\farok\AppData\Local\Google\Chrome\Application\chrome.exe www.google.com

when i open the file using windows every thing goes well but nothing happened when i open it using exec

and i have tried to do it using jar file by BrowserControl class

BrowserControl.displayURL("www.google.com");

and the same as bat file happened so is there any way to do it?

note:im using wamp 2.2 ,Apache 2.0 , PHP V5.3.8

Update

i found that after i run this command

exec('"C:\Program Files (x86)\Mozilla Firefox\firefox.exe" "www.google.com" 2> errors.txt');

firefox dose open in task manager but the browser interface not visible .. any ideas?

Community
  • 1
  • 1
Farok Ojil
  • 664
  • 1
  • 12
  • 22
  • Is there a reason you need to run a browser? IF you provide insight into what you are trying to do, there may be a better way. – Kris Jun 30 '12 at 17:09
  • You do not need an Apache server (or any web server) for this. In fact running it via a server won't work because the output is nto supposed to be sent to a browser but to the visual interface functions that draw stuff on the display. – Mihai Stancu Jun 30 '12 at 17:13
  • @Kris i'm trying to do this http://stackoverflow.com/questions/11246700/render-html-pages-in-php-or-java – Farok Ojil Jun 30 '12 at 18:00
  • @FarokOjil - In that thread you got suggestions; phantomjs, using java or even JSP. Just out of interest, why did you ask a question just to disregard it's answers? The [Java answer](http://www.javaworld.com/javaworld/javatips/jw-javatip66.html) was actually really simple and you specified Java as a language you could work with. – Fergus In London Jul 01 '12 at 00:56

5 Answers5

5

I'm no windows expert, but I think you need to allow desktop interaction, which isn't easy/possible if the parent process runs as a windows service. php runs inside the apache process, which you probably have running as a service.

Try stopping the service and manually starting httpd.exe, and then the following works for me on win7 when i request the script via localhost url through apache. my php interfaces with apache via plain old cgi.

exec('"C:\Program Files (x86)\Mozilla Firefox\firefox.exe" "http://stackoverflow.com/"');

note my use of quotes.

ch271828n
  • 15,854
  • 5
  • 53
  • 88
goat
  • 31,486
  • 7
  • 73
  • 96
  • 2
    try executing the script from the command line, using the php cli. To be honest, you should just be sending these commands to some external daemon process that will launch the browser - you shouldn't try to do these kinda things in webserver processes. – goat Jun 30 '12 at 19:09
  • how can i use "external daemon process" can u give me an example? – Farok Ojil Jun 30 '12 at 19:29
  • 2
    No, I'm sorry I won't be giving an example. You can do some research on it. – goat Jun 30 '12 at 19:35
  • +1@rambocoder for actually telling someone to do some research; you learn nothing by getting spoon fed! – Fergus In London Jul 01 '12 at 00:52
5

so this is another good workaround i found here, the idea is to create a scheduler that execute the program you want and call it using command

hope this help :

shell_exec('SCHTASKS /F /Create /TN _notepad /TR "notepad.exe" /SC DAILY /RU INTERACTIVE');
shell_exec('SCHTASKS /RUN /TN "_notepad"');
shell_exec('SCHTASKS /DELETE /TN "_notepad" /F');
Baim Wrong
  • 478
  • 1
  • 10
  • 14
  • good ideas!any ideas without dependency on operating system feature – Carson May 11 '18 at 04:23
  • Excellent! I was instantiating as a com object an MSO application & passing the external command to a VBA module function (I did not have to make the application 'visible' for the external command's GUI to be interactive)--but this is faster and cleaner. – Dale Thompson Jan 07 '21 at 02:25
3

I solved that by disabling apache service on windows and start apache with httpd.exe After that can use exec() for open any GUI windows program.

exec("Path_to_mi_program.exe" "file_to_open");
MTK
  • 3,300
  • 2
  • 33
  • 49
  • This worked for me for a related (exe that interacts with gui) where nothing else worked. – Rick Sep 26 '19 at 00:18
1

Probably the easiest way is to use COM (I assume it will only run locally on a Windows computer):

<?php
function _exec($cmd) 
{ 
  $WshShell = new COM("WScript.Shell"); 
  $oExec = $WshShell->Run($cmd, 0,false); 
  echo $cmd;
  return $oExec == 0 ? true : false; 
}

_exec("youexe.exe");
?>

Taken from here

LeleDumbo
  • 9,192
  • 4
  • 24
  • 38
0

It's just about users. When you run a program, it runs as system user. I tried runas /user:myusername blabla.exe but it returned password for myusername and exit.

$deneme = shell_exec('runas /user:myusername C:\Windows\Temp\putty.exe');
echo "$deneme";

it returned:

myusername için parolayı girin: (english: password for myusername)
pzin
  • 4,200
  • 2
  • 28
  • 49
Melih Yıldız'
  • 415
  • 5
  • 17