Call PHP function from url?

前端 未结 14 1606
清歌不尽
清歌不尽 2020-12-04 15:50

If I want to execute a php script, i just point the browser to www.something.com/myscript.php

But if i want to execute a specific function inside

相关标签:
14条回答
  • 2020-12-04 16:09

    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'; 
    
    } 
    
    ?> 
    
    0 讨论(0)
  • 2020-12-04 16:12

    One quick way is to do things like

    something.com/myscript.php?f=your_function_name
    

    then in myscript.php

    if(function_exists($_GET['f'])) {
       $_GET['f']();
    }
    

    But please, for the love of all kittens, don't abuse this.

    0 讨论(0)
  • 2020-12-04 16:14
    //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
    
    0 讨论(0)
  • 2020-12-04 16:18

    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:

    switch($_GET['function']) {
    case 'specificFunction':
        specificFunction();
    }
    

    Then you could visit 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.

    0 讨论(0)
  • 2020-12-04 16:20

    There are several ways.

    One way would be to pass the function name as a GET Parameter, and depending on it's existence you could call the function.

    0 讨论(0)
  • 2020-12-04 16:22

    You could also for security, check the HTTP Referrer. And only execute functions if the referrer is the same domain as itself. Perhaps also compare / check verious other things.

    But there should be many ways you can verify the request is coming from the same website were the function needs to be executed.

    0 讨论(0)
提交回复
热议问题