douwen9343 2014-04-29 21:21
浏览 28

使用javascript执行php进程

I created a server using php. I also created an html page that acts as the control panel for the php server. If I visit the control panel page and the php server is not running, I want to push on a button which creates a new separate process that launches the php server.

Here is the code that launches the php server (launchserver.php) using a terminal: #!/usr/bin/php -q

<?php
    error_reporting(E_ALL);
    require_once 'lib/server.php';
    require_once 'lib/client.php';

    set_time_limit(0);

    // Get the local ip address of the host machine (NOTE: Not the loop back address)
    $command="/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'";
    $ipaddress = exec ($command);
    $server = new Server($ipaddress, 15000);
    $server->run();
?>

To run the server, I just open a terminal and execute "php -q launchserver.php".

However, I don't want to launch the php script using a terminal, I want to use my new control panel page, where I can click on a button and the button creates a separate process which executes the launchserver.php script and stands up the server and if I leave the control page, the server does not die because I'm no longer in the control page.

  • 写回答

1条回答 默认 最新

  • drpfu51608120170 2014-04-29 21:36
    关注

    I had to do something similar recently (had to run some php code on another file using an HTML button).

    This is my suggestion (I am a "junior", so there probably is a better/simpler solution):

    Change your launchserver.php file to this:

    <?php
    if($_POST) {
      error_reporting(E_ALL);
      require_once 'lib/server.php';
      require_once 'lib/client.php';
    
      set_time_limit(0);
    
      // Get the local ip address of the host machine (NOTE: Not the loop back address)
      $command="/sbin/ifconfig eth0 | grep 'inet addr:' | cut -d: -f2 | awk '{ print $1}'";
      $ipaddress = exec ($command);
      $server = new Server($ipaddress, 15000);
      $server->run();
      }
    ?>
    

    Then you need a button with "onclick". e.g. <button onclick="startMyServer();">Start My Server</button>

    Then the JS function:

    function startMyServer() {
      var startServer = 1;
      $.post("<full url here>/launchserver.php", { 'startServer': startServer }, function(d){
    
      });
    }
    

    This will act like you sent a form to the file. $.post requires JQuery, so be sure to include that.

    So if you click the button it will send $_POST to your php file and if $_POST was sent, then the code in the php file runs.

    I hope this is helpful. I used this JQuery function to run some php code ( mysql queries located on another file) and it works flawlessly for me.

    Since you mentioned that "I do have apache2 httpd web server running with php", it should do the trick for you.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?