Results 1 to 4 of 4

Thread: [RESOLVED] Execute commands with PHP

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Resolved [RESOLVED] Execute commands with PHP

    I'm running a WAMP server on one of my computers.

    I've tried the following code:
    PHP Code:
    <?php
    system
    ("notepad");
    ?>
    <?php
    exec
    ("notepad");
    ?>
    <?php
    system
    ("c:\\windows\\notepad.exe");
    ?>

    <?php
      
    function runAsynchronously($path)
      {
        
    $WshShell = new COM("WScript.Shell");
        
    $spustec getcwd()."\\_".uniqid().".php";

      
    #Generate PHP code to run app and save it into file $spoustec
        
    fwrite(($fp fopen($spustec,"w+")),"<?php chdir(dirname(__FILE__));\$command = \"".addslashes($path)."\";echo \"Now running external program...\n\n\";system(\$command);echo \"Now deleting...\";unlink(__FILE__); ?>");
        
    fclose($fp);

      
    # Now run $spoustec -> it will start $path
        
    $oExec $WshShell->Run($spustec7false);
        unset(
    $WshShell,$oExec);
      }

    runAsynchronously("notepad");
    sleep(1);
    ?>
    But nothing seems to work. I'm thinking it could be a permission problem, but not sure. Has anyone else had success?
    Last edited by Slyke; Aug 17th, 2009 at 04:00 AM.

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: Execute commands with PHP

    from a comment on exec() on PHP.net:
    If you try to launch GUI apps from a service in Vista, you'll have lots of trouble. As a security feature, Vista mediates the interaction of services with the desktop using 'Interactive Services Detection'.

    That means, if you are running PHP as a module of an Apache service, you won't be able to launch GUI apps using any method. This kind of thing just won't work:

    $WshShell = new COM("WScript.Shell");
    $oExec = $WshShell->Run("notepad.exe", 7, false);

    So, if you want to use Apache/PHP as a proxy for launching GUI apps, you'll need to run Apache as a console application.

    First, if Apache is already installed as a service, you'll need to set it's startup type to "manual" using the services snap-in. (&#37;SystemRoot%\system32\services.msc) Search for Services in the start menu search box.

    Then add a shortcut to C:\apache\bin\httpd.exe (or wherever Apache is installed) to your Startup folder, and set that shortcut to start minimized. You can use an app like TrayIt! to force Apache down into the system tray.

    Then use any of the methods outlined on the PHP website and you will be able to open a Windows application from PHP and see it's GUI.
    not sure if other versions of Windows also do this or not.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Sep 2005
    Posts
    540

    Re: Execute commands with PHP

    Thanks dude, you pointed me in the right direction. Alls I did was tick "Allow service to interact with desktop" in the Log On tab for the service properties and it's all working now .

    I'm using Anaska's WAMP server, but I'm sure it will work for other WAMP servers too.

    I would +rep, but ti says I must spread it first .

  4. #4
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: [RESOLVED] Execute commands with PHP

    well, it's something specific with Apache and has nothing to do with your WAMP install, or whatever. It's just a Windows security thing.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width