从 url 调用 PHP 函数?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4360182/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Call PHP function from url?
提问by Gabriel A. Zorrilla
If I want to execute a php script, i just point the browser to www.something.com/myscript.php
如果我想执行一个 php 脚本,我只需将浏览器指向 www.something.com/myscript.php
But if i want to execute a specific function inside myscript.php
, is there a way? something like www.something.com/myscript.php.specificFunction
但是如果我想在里面执行一个特定的函数myscript.php
,有什么办法吗?就像是www.something.com/myscript.php.specificFunction
Thanks!
谢谢!
回答by Andreas Wong
One quick wayis to do things like
一种快速的方法是做这样的事情
something.com/myscript.php?f=your_function_name
then in myscript.php
然后在 myscript.php
if(function_exists($_GET['f'])) {
$_GET['f']();
}
But please, for the love of all kittens, don't abuse this.
但是,为了所有小猫的爱,请不要滥用它。
回答by VoteyDisciple
What your script does is entirely up to you. URLs cannot magically cause Apache, PHP, or any other server component to take a certain behavior, but if you write your program such that a particular function can be executed, it's certainly possible. Perhaps something like:
你的脚本做什么完全取决于你。URL 不能神奇地导致 Apache、PHP 或任何其他服务器组件采取某种行为,但是如果您编写的程序可以执行特定的功能,这当然是可能的。也许是这样的:
switch($_GET['function']) {
case 'specificFunction':
specificFunction();
}
Then you could visit myScript.php?function=specificFunction
然后你可以访问 myScript.php?function=specificFunction
Be extremely careful here to specifically list each allowable function. You must not just take the $_GET['function']
parameter and blindly execute whatever function it says, since that could present an enormous security risk.
在这里要特别小心地具体列出每个允许的功能。您不能只是获取$_GET['function']
参数并盲目执行它所说的任何功能,因为这可能会带来巨大的安全风险。
回答by Robin Orheden
You will have to expose it in some way. This is because exposing all methods public, would be a security risk.
你将不得不以某种方式暴露它。这是因为公开所有方法会带来安全风险。
Example.php
例子.php
<?php
function CalculateLength($source)
{
return strlen($source);
}
if(isset($_GET['calculate-length']) && isset($_GET['value']){
die(CalculateLength($_GET['value']));
}
?>
Then just call:
然后只需调用:
http://www.example.com/Example.php?calculate-length&value=My%20example
回答by Balaji
Try this one
试试这个
$urlParams = explode('/', $_SERVER['REQUEST_URI']);
$functionName = $urlParams[2];
$functionName($urlParams);
function func1 ($urlParams) {
echo "In func1";
}
function func2 ($urlParams) {
echo "In func2";
echo "<br/>Argument 1 -> ".$urlParams[3];
echo "<br/>Argument 2 -> ".$urlParams[4];
}
and the urls can be as below
http://domain.com/url.php/func1
http://domain.com/url.php/func2/arg1/arg2
并且网址可以如下
http://domain.com/url.php/func1
http://domain.com/url.php/func2/arg1/arg2
回答by CodeJoust
You could do something like this (not recommended for security reasons): www.exampe.com/myscript.php?run=getNames
你可以这样做(出于安全原因不推荐):www.exampe.com/myscript.php?run=getNames
then:
然后:
<?php
if (isset($_GET['run']) && function_exists($_GET['run'])){
echo $_GET['run']();
} else {
echo 'Function not Found';
}
You would be better off using a php class instead of trying to call a function on the global namespace because they could call a potenitally dangerous function or call a function you don't want them to see the result to:
您最好使用 php 类而不是尝试调用全局命名空间上的函数,因为它们可能会调用潜在危险的函数或调用您不希望他们看到结果的函数:
<?php
class PublicView {
function get_page(){ echo 'hey'; }
}
if (isset($_GET['run']) && method_exists('PublicView',$_GET['run'])){
$view = new PublicView();
$view->$_GET['run']();
} else {
echo 'Function not found';
}
This also wouldn't allow the class's private functions to be called, etc.
这也不允许调用类的私有函数等。
回答by Simon
Here you go. This one is really simple. It calls only specific functions and does default function.
干得好。这个真的很简单。它只调用特定函数并执行默认函数。
<?php
if (isset($_GET['p'])) $linkchoice=$_GET['p'];
else $linkchoice='';
switch($linkchoice){
case 's' :
firstfunc();
break;
case 'second' :
secondfunc();
break;
default :
echo 'no function';
}
?>
回答by Somebody
I am using this on my website. To use this simply format your url like this www.something.com/myscript.php?function=myFunction&arg=foo&otherarg=bar
It doesn't matter what you call the args in the url and you can have as many args as you want.
我在我的网站上使用它。要使用它,只需像这样格式化您的 urlwww.something.com/myscript.php?function=myFunction&arg=foo&otherarg=bar
无论您在 url 中调用什么 args 都没有关系,您可以拥有任意数量的 args。
<?php
if(isset($_GET["function"])&&!empty($_GET)){
$function = $_GET["function"];
unset($_GET["function"]);
$canexec = array(
"function name that can be executed",
"function name that can be executed",
"function name that can be executed"
);
if(!in_array($function,$canexec)){
die("That function cannot be executed via url.");
}
$args = array();
if(count($_GET)>0){
//handle args
foreach($_GET as $arg){
array_push($args,$arg);
}
}
$result = call_user_func_array($function,$args);
//this part is only necessary if you want to send a response to a http request.
if(is_bool($result)){
die(($r)?'true':'false');
}
else if(is_array($result)){
die(json_encode($result));
}
else {
die($result);
}
}
myFunction($foo,$bar){
echo $foo." ".$bar;
}
?>
回答by Freddie
you could put the function call in the script.
您可以将函数调用放在脚本中。
myFunction();
我的函数();
function myFunction() { .... }
函数 myFunction() { .... }
回答by Zeeshan Abbas
//Register Hooks
$hooks = ['lang','score'];
function parseHook() {
if (isset($_GET['_h'])) {
return $hook = explode('/', filter_var(rtrim($_GET['_h'], '/'), FILTER_SANITIZE_URL));
} else {
return false;
}
}
$hook = parseHook();
if ($hook && in_array($hook[0],$hooks) && function_exists($hook[0])) {
$callMe = $hook[0];
unset($hook[0]);
$params = $hook ? array_values($hook) : [];
$callMe($params);
}
function lang($params) {
$Lib = new Lib();
$Lib->printWithPreTag($params);
}
function score($params) {
$Lib = new Lib();
$Lib->printWithPreTag($params);
}
//&_h=hookname/par1/par2/par3
回答by robert
if (isset($GET[param_name])){
if($GET[param_name] === value)
{
function 1...
} else if
{
function 2...
}
}