streetparade
streetparade

Reputation: 32888

How to handle REQUEST in PHP

is there a class to handle $_REQUEST that makes the life of a php developer easier? I want to handle the client requests easier. I dont want to test with if(is_set($_REQUEST['blabla'])) {makesomthing();} I wish there could be a solution like this.

class rpclike
{
 public function getMember()
 {
  $memberid = $this->inputhandler['memberid'];
  $member = $this->memberclass->getmember($memberid);

   foreach($member as $mem)
   {
    echo $mem->id;
   }
 }

}

$rpc = new rpclike();

then if i call the rpclike from a javascript like this

<a href="#" onclick="GETURL("rpclike.php?getMember&memberid=22")">Get member</a>

Which class can do something like that?

Upvotes: 1

Views: 7876

Answers (5)

slebetman
slebetman

Reputation: 113886

Do something like:

url: /foo/bar?req=getMembers&memberid=22

Then you can do:

$request = $_GET['req'];
$request();

Slightly less dangerous version:

$req_methods = array(
    getMembers => 'some_function',
    saveMembers => 'another_function',
    sendMessage => 'send_him_an_email'
);
$request = $_GET['req'];
$req_methods[$request]();

Upvotes: -1

funwhilelost
funwhilelost

Reputation: 2010

Aside from the obvious security risks involved in this, it is feasible. It's a common pattern to use for steering requests in an MVC system.

Say you request index.php?class=User&method=ViewProfile

$module = new $_GET['class']();
if(!method_exists($module,$_GET['method']))
$module->$eventName();

Upvotes: 1

zombat
zombat

Reputation: 94157

PHP doesn't really have a default class structure that you can utilize in that kind of manner, as it's origins are in procedural-based programming.

It would be fairly trivial for you to create a class like that if you felt the need for it. However, you would really just be adding overhead. If the convenience of it is worth it for you, then you could utilize the __get() and __set() methods to handle existence checks for you.

The fact that you want to use this for handling client requests in an easier fashion is probably a good indicator that you should move to something like an MVC framework, which usually handle URLs and route them to appropriate methods for you automatically. Most PHP frameworks will do this for you already. For a nice overview on how the process commonly works, you could see how CodeIgniter does it.

Upvotes: 1

Corey Ballou
Corey Ballou

Reputation: 43457

It's not recommended that you use $_REQUEST as it poses security concerns. You should be using one of $_GET, $_POST, or $_COOKIE depending on what global request var you are trying to retrieve. Your best bet would be to have something like the following:

class input {

    public static function get($key, $value = false) {
        return (!empty($_GET[$key])) ? $_GET[$key] : $value;
    }

    public static function post($key, $value = false) {
        return (!empty($_POST[$key])) ? $_POST[$key] : $value;
    }

    public static function cookie($key, $value = false) {
        return (!empty($_COOKIE[$key])) ? $_COOKIE[$key] : $value;
    }

}

You could then use the class like:

if (input::post('field', null) != null) {

}

or

if (input::get('field', false) != false) {

}

Although this still requires testing, you can explicitly set the return values in the event no data was set for the global variable.

Upvotes: 2

Joe
Joe

Reputation: 47609

I don't think so. Being able to invoke an arbitrary method would be a massive security hole.

Upvotes: 0

Related Questions