如何使用Ajax通过URL或其他方法注入特定的PHP函数?

发布于 2024-08-18 15:20:32 字数 1692 浏览 4 评论 0原文

我正在从 JS 领域转向 php 和 Ajax。我过去曾涉足过一些 PHP。 我真的非常感谢 stackoverflow 在帮助我解决基本问题方面提供的帮助。

假设我有一个名为 #divName 的 div。

我使用以下 JS 进行 Ajax。其中一些只是伪代码。

var request = false;
   try {
     request = new XMLHttpRequest();
   } catch (trymicrosoft) {
     try {
       request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (othermicrosoft) {
       try {
         request = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (failed) {
         request = false;
       }  
     }
   }

   if (!request)
     alert("Error initializing XMLHttpRequest!");

   function getAjaxInfo(<name of the php function???>) { //<<<<< this is the function I need help with
    var myFunction= nameOfPHPfunctionIJustGot;
     var url = "filename.php?func=" + myFunction;
     request.open("GET", url, true);
     request.onreadystatechange = updatePage;
     request.send(null);
   }

  function updatePage() {
     if (request.readyState == 4) {
       if (request.status == 200) {
         var response = request.responseText;
             document.getElementById("divName").innerHTML = response; //probably going to use Jquery append here, but this example works.
               } else
         alert("status is " + request.status);
     }
   }

我有我的 fileName.php 文件:

<?php

function header() { ?>
   header content goes here
<?php }

function footer() { ?>
    footer content goes here
<?php }

?>

我的目标是当我执行 getAjaxInfo() 时,我可以提取我想要的任何 PHP 函数。

假设我执行 onClick="getAjaxInfo(header)" ,它将获取 php 标头函数,将其应用于 javascript 函数,然后将其应用于 div。

任何帮助将不胜感激!

I'm moving from the realm of just JS to php and Ajax. I've dabbled some with PHP in the past.
I really appreciate how much help stackoverflow has been in helping me with basic questions.

let says I have a div called #divName.

I use the following JS for Ajax. Some of this is just pseudo code.

var request = false;
   try {
     request = new XMLHttpRequest();
   } catch (trymicrosoft) {
     try {
       request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (othermicrosoft) {
       try {
         request = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (failed) {
         request = false;
       }  
     }
   }

   if (!request)
     alert("Error initializing XMLHttpRequest!");

   function getAjaxInfo(<name of the php function???>) { //<<<<< this is the function I need help with
    var myFunction= nameOfPHPfunctionIJustGot;
     var url = "filename.php?func=" + myFunction;
     request.open("GET", url, true);
     request.onreadystatechange = updatePage;
     request.send(null);
   }

  function updatePage() {
     if (request.readyState == 4) {
       if (request.status == 200) {
         var response = request.responseText;
             document.getElementById("divName").innerHTML = response; //probably going to use Jquery append here, but this example works.
               } else
         alert("status is " + request.status);
     }
   }

The I have my fileName.php file:

<?php

function header() { ?>
   header content goes here
<?php }

function footer() { ?>
    footer content goes here
<?php }

?>

My goal is that when I execute getAjaxInfo(), I can pull whatever PHP function I want.

So lets say if I do a onClick="getAjaxInfo(header)" it will get the php header function, apply it to a javascript function, and then apply that to a div.

Any help would be appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

度的依靠╰つ 2024-08-25 15:20:33
<?php

$allowedFunctions = array(
   'header',
   'footer'
);

$functionName = $_GET[ 'func' ];

if( in_array( $functionName, $allowedFunctions ) && function_exists( $functionName ) )
{
    $functionName();
}

/* your function definitions */
?>

$allowedFunctions 是您希望 ajax 调用允许执行的用户定义(iow 您的)php 函数的白名单数组。如果您不保留此白名单,您的脚本将存在潜在危险,因为它允许任何人执行任意函数。这绝对是你不想要的。

<?php

$allowedFunctions = array(
   'header',
   'footer'
);

$functionName = $_GET[ 'func' ];

if( in_array( $functionName, $allowedFunctions ) && function_exists( $functionName ) )
{
    $functionName();
}

/* your function definitions */
?>

$allowedFunctions is a whitelist array of user defined (i.o.w. your) php functions that you want the ajax call to allow to execute. If you don't keep this whitelist, your script will be potentially dangerous, since it would allow anyone to execute an arbitrary function. That is something you definately do not want.

╭⌒浅淡时光〆 2024-08-25 15:20:32

尝试:

$func=$_GET['func'];
if(function_exists($func)) $func();

通过这种方式,您可以获取 GET 传递的函数名称并执行它。

如果您只想调用某些函数:

$allowedFunctions=array("header","footer");
$func=$_GET['func'];
if(in_array($func,$allowedFunctions) && function_exists($func)) $func();

Try with:

$func=$_GET['func'];
if(function_exists($func)) $func();

In this way you get the function name passed by GET and the execute it.

If you want to be able to call only certain functions:

$allowedFunctions=array("header","footer");
$func=$_GET['func'];
if(in_array($func,$allowedFunctions) && function_exists($func)) $func();
_畞蕅 2024-08-25 15:20:32

采纳@VolkerK的建议,并添加一个失败函数:

$func = $_GET['func'];
$allowed = array('header', 'footer');
if (in_array($func, $allowed) && function_exists($func)) $func();
else default_action();

Picking up on @VolkerK's suggestion, and adding a failure function:

$func = $_GET['func'];
$allowed = array('header', 'footer');
if (in_array($func, $allowed) && function_exists($func)) $func();
else default_action();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文