Home » Php » Call PHP function from url?

Call PHP function from url?

Posted by: admin April 23, 2020 Leave a comment

Questions:

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 myscript.php, is there a way? something like www.something.com/myscript.php.specificFunction

Thanks!

How to&Answers:

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.

Answer:

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.

Answer:

You will have to expose it in some way. This is because exposing all methods public, would be a security risk.

Example.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

Answer:

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

Answer:

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.

Answer:

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'; 

} 

?> 

Answer:

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;
        }
    ?>

Answer:

you could put the function call in the script.

myFunction();

function myFunction()
{
….
}

Answer:

//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

Answer:

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.

Answer:

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.

Answer:

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.

Answer:

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";
        }
    }
}

Answer:

 if (isset($GET[param_name])){

  if($GET[param_name] === value)
   {
      function 1... 
    } else if 
 {

function 2...
 }

}