从另一个php文件中的php文件执行函数';s文本框
我是否可以通过在另一个php文件的输入框中键入函数名来执行php文件中的函数从另一个php文件中的php文件执行函数';s文本框,php,Php,我是否可以通过在另一个php文件的输入框中键入函数名来执行php文件中的函数 function bamiiChuckNorris() { $arrContextOptions=array( "ssl"=>array( "verify_peer"=>false, "verify_peer_name"=>false, ), ); $geocodeUrl = "http://api.icndb.com/jokes/ran
function bamiiChuckNorris() {
$arrContextOptions=array(
"ssl"=>array(
"verify_peer"=>false,
"verify_peer_name"=>false,
),
);
$geocodeUrl = "http://api.icndb.com/jokes/random";
$response = file_get_contents($geocodeUrl, false, stream_context_create($arrContextOptions));
$a =json_decode($response, true);
return $a['value']['joke'];
}function bamiiTellTime($data) {
if(strpos($data, 'in')) {
return "Sorry i can't tell you the time somewhere else right now";
} else {
return 'The time is:' . date("h:i");
}
}?>
这是我的档案
<?php include answers.php
This is the input text box
<input name="input" type="text" class="tb5" placeholder="Chat with me! Press Ask to send."?>
是的,可以动态执行函数,但要确保它有很多验证,用户只能调用有效函数
$functionName = $_POST['input'];
if (function_exists($functionName)) {
// Dynamic call using variable value as function name.
$response = {$functionName}();
} else {
throw new Exception(404, "Function '{$functionName}' not found");
}
如果检查对象方法,请使用
method\u exists($class$functionName)
要从输入执行代码,如
get_number(1)
,有几个选项:
1) 使用eval($input)
-
2) 使用regex解析用户输入
$uInput = $_POST['input'];
$matches = [];
preg_match('/(\w+)\((\w+)\)/', $uInput, $matches);
$functionName = $matches[1];
$params = $matches[2];
{$functionName}(...explode(', ', $params));
这是我基于您的代码@Justinas的解决方案
$function = $_POST['input'];
$functionName = explode("(", $function);
if (function_exists($functionName[0])) {
$functionVariable = explode(')',$functionName[1],2);
// Dynamic call using variable value as function name.
$response = $functionName[0]($functionVariable[0]);
你能解释一下你的问题吗?是的,我已经澄清了这个问题,你可能在谈论ajax调用,在更新文本区域的内容时,你想执行一个函数,对吗?不,我认为它涉及函数存在,如果函数名为get_number($number),用户输入的数字为get_number(1),它将如何发挥作用execute@AdokiyeIruene您必须将参数作为不同的输入传递或使用正则表达式解析,如
/(\w+)\(\w+)/
,并且$1
将是函数名,$2
将是参数。如果有更多的参数,请考虑使用explode(',',$params)
拆分$2
,如果用户编写get_number(1),那么基于上面的代码要执行的php代码是什么谢谢,但是我如何才能在同一代码中同时将function()或function_与_变量($var)连接起来呢。如果用户键入functions@AdokiyeIruene使用不同的正则表达式