如果我想执行php脚本,我只需将浏览器指向 www.something.com/myscript.php
但如果我想在里面执行一个特定的功能myscript.php,有没有办法?就像是www.something.com/myscript.php.specificFunction
谢谢!
And*_*ong 46
一个快速的方法是做一些事情
something.com/myscript.php?f=your_function_name
Run Code Online (Sandbox Code Playgroud)
然后在myscript.php中
if(function_exists($_GET['f'])) {
$_GET['f']();
}
Run Code Online (Sandbox Code Playgroud)
但是,为了所有小猫的爱,请不要滥用这个.
Vot*_*ple 25
你的脚本完全取决于你.URL不能神奇地导致Apache,PHP或任何其他服务器组件采取某种行为,但如果您编写程序以便可以执行特定的功能,那么它当然是可能的.也许是这样的:
switch($_GET['function']) {
case 'specificFunction':
specificFunction();
}
Run Code Online (Sandbox Code Playgroud)
然后你可以访问 myScript.php?function=specificFunction
这里要特别注意列出每个允许的功能.你不能只是采取$_GET['function']参数并盲目地执行它所说的任何功能,因为这可能带来巨大的安全风险.
Rob*_*den 11
你必须以某种方式暴露它.这是因为暴露所有方法公开,将是一个安全风险.
使用example.php
<?php
function CalculateLength($source)
{
return strlen($source);
}
if(isset($_GET['calculate-length']) && isset($_GET['value']){
die(CalculateLength($_GET['value']));
}
?>
Run Code Online (Sandbox Code Playgroud)
然后打电话:
http://www.example.com/Example.php?calculate-length&value=My%20example
Run Code Online (Sandbox Code Playgroud)
Bal*_*aji 11
试试这个吧
$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];
}
Run Code Online (Sandbox Code Playgroud)
并且网址可以如下
http://domain.com/url.php/func1
http://domain.com/url.php/func2/arg1/arg2
您可以这样做(出于安全原因不推荐):www.exampe.com/myscript.php?run = getNames
然后:
<?php
if (isset($_GET['run']) && function_exists($_GET['run'])){
echo $_GET['run']();
} else {
echo 'Function not Found';
}
Run Code Online (Sandbox Code Playgroud)
你最好使用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';
}
Run Code Online (Sandbox Code Playgroud)
这也不允许调用类的私有函数等.
干得好.这个很简单.它只调用特定的函数并执行默认功能.
<?php
if (isset($_GET['p'])) $linkchoice=$_GET['p'];
else $linkchoice='';
switch($linkchoice){
case 's' :
firstfunc();
break;
case 'second' :
secondfunc();
break;
default :
echo 'no function';
}
?>
Run Code Online (Sandbox Code Playgroud)
我在我的网站上使用它。要使用它,只需像这样格式化您的 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;
}
?>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
78450 次 |
| 最近记录: |