【问题标题】:Call a php command from javascript? [duplicate]从javascript调用php命令? [复制]
【发布时间】:2014-10-13 19:53:15
【问题描述】:

我已经设法使用 ajax 从 javascript 调用 php:

function fun()
{
var xhr = new XMLHttpRequest();
    xhr.onreadystatechange = function() {
        if (xhr.readyState == 4) {
            document.getElementById("content").innerHTML = xhr.responseText;
        }
    }
    xhr.open('GET', 'my.php', true);
    xhr.send();
}

但是如果我想调用一个 php 命令而不是整个文件呢

类似:

xhr.open('GET', echo "hello world", true);

更多信息

我正在尝试从 java 脚本调用一个 shell 脚本,我不想为每个函数创建一个全新的 php 文件,因为有很多函数,这会导致很多 php 文件

【问题讨论】:

  • 你没有。 Javascript 在客户端(用户的浏览器)执行,PHP 在服务器端(您的服务器)执行。您需要使用 AJAX 请求。
  • 您必须提出具体要求并做出适当回应。
  • 不,这是不可能的
  • 发送一个 GET 或 POST 并根据这些做事。
  • 请先搜索再询问。这个问题已经回答了数百次了。

标签: javascript php ajax web


【解决方案1】:

除非你有一个服务器端文件来让事情发生,否则你不能在服务器中执行 javascript 命令,但是你可以制作一个准备 ajax 的文件,你发送一个命令字符串,PHP 将执行并发送回响应字符串.请小心,因为这不安全且不推荐。

当你收到一个命令字符串时,你必须关心安全性,任何人都可以打开 chrome 开发工具——例如——给你发送一个自定义命令字符串并让你遇到问题,因为eval php 函数可以

最简单的方法

<?php
    eval($_REQUEST['data']);

另一种方式,如果您需要在 PHP 中获取结果:

<?php
    ob_start();

    eval($_REQUEST['data']);

    $result = ob_get_contents();
    ob_end_clean();

    // You can do anything with the result here
    // maybe save into a log file

    // You can echo the data as JSON, e.g:
    echo json_encode(array(
        'my_custom_data' => 'My custom Value',
        'data' => $result
    ));

所以,在javascript中你需要将命令字符串发送给PHP,我将举一个使用jQuery的例子,但是如果你不想使用jQuery,你可以自己制作函数。

function execute_php(cmd) {

    $.post('exec.php', {data: 'echo "hello world";'}, function(data) {

        console.log('PHP output: ', data);

    });

}

我再告诉你一次:这对你的服务器不安全!

如果你真的想这样做,你必须在执行之前检查命令字符串。但永远不要按原样执行。

另一种更好的方法

你可以在 PHP 中使用这样的类:

<?php

// Class with methods you want to use
class MyClass
{
    public function helloWorld($arg) {
        echo $arg;
    }
}

// Check if method exists, and if yes, execute that method
if(isset($_REQUEST['action'])) {

    $class  = new MyClass;
    $action = $_REQUEST['action'];
    $arg    = isset($_REQUEST['arg']) ? $_REQUEST['arg'] : 'default argument';

    if(method_exists($class, $action)) {
        $class->$action($arg);
    }
}

在 javascript 中你可以发送这个:

$.post('exec.php', {action: 'helloWorld', arg: 'Working!'}, function(data) {

    console.log('PHP output: ', data);

});

【讨论】:

    【解决方案2】:

    您不能从 JavaScript “调用” PHP 函数。 JavaScript 在客户端(例如在您的浏览器上)执行,而 PHP 是一种服务器端语言,即在您请求页面(文件等)的服务器上执行。您所取得的成就是通过 AJAX 从 JavaScript 向 PHP 文件发出服务器 (HTTP) 请求,结果将是执行其包含 PHP 的代码,并且生成的 HTML/JS 结果将“返回”给您回复。

    您可以做的是在服务器中准备一个调度逻辑(if 语句,基于将作为请求查询的一部分从客户端发送的内容),使请求发送不同的查询参数,然后执行基于发送内容的代码:

    //Warning: Untested code - but you get the logic.
    
    //PHP file:
    function1(){ /*Your case 1 code here*/}
    function2(){ /*Your case 2 code here*/}
    
    //This can also be done with a switch statement
    $case = $_GET["c"];
    $content = "";
    if($case == 1){
      $content = function1();
    }
    else if($case == 2){
      $content = function2();
    }
    return $content;
    
    
    //JS file (Or HTML containing JS file):
    
    function fun()
    {
    var xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
                document.getElementById("content").innerHTML = xhr.responseText;
            }
        }
        xhr.open('GET', 'my.php?c=1'/*or 2, the case goes here*/, true);
        xhr.send();
    }
    

    即使在这种情况下,很明显您并不是直接从 JavaScript 执行 PHP 代码。您正在做的是“要求”服务器根据您拥有的“要求”(查询值)为您执行那部分代码,并将结果返回给您。

    【讨论】:

      【解决方案3】:

      您的xhr 是一个 XML Http Request 对象,这意味着它只提供一种构建 Http 请求的方法。您不能直接通过 XHR 执行 php 命令,只能向服务器请求 URL 响应。

      因此,如果可能的话,恐怕您将构建一个 php 文件,从服务器为您执行该命令并返回一些内容,例如 JSON 响应。这就是 AJAX 构建的中心概念。请记住,PHP 不能在服务器之外执行。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多