Call PHP function from url?

前端 未结 14 1608
清歌不尽
清歌不尽 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:23

    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.

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

    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

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

    You could do something like this (not recommended for security reasons): 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
    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.

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

    Use the constructor of your PHP Class:

    <?php
    class YourClass {
    
        function __construct() {
            $functionName = myFunction;
    
            if (isset($_GET['functionName']) 
                    && $_GET['functionName'] == $functionName)){
    
                myFunction();
            }
            else {
                echo "Function not found";
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 16:32

    you could put the function call in the script.

    myFunction();

    function myFunction() { .... }

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

    You cannot do this without adding special code to the PHP file itself to grab the name of the function from the URL and invoking it.

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