Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Module modifying $_POST, $_FILES

I would like to write a PHP Module that will modify the $_POST and $_FILES variables. The goal is to do preprocessing in the module, for instance, normalizing input, not allowing fies with certain extensions.

Is this possible? It seems that you can't modify the $_FILES variable but, if you have an extension, you might be able to modify these variables during the RINIT phase.

How can I do this?

EDIT: Clarification

I guess normalizing data wasn't a great example. I'm more interested in how it could be done and learning more about PHP and modules.

As an example, there is a function that reorganizes $_files. I thought it would be fun to write a module that would automatically do that, just as an academic exercise and was looking for pointers on where to start.

like image 993
jlg_foil Avatar asked Mar 07 '26 18:03

jlg_foil


1 Answers

Perhaps it would be better to encapsulate the "request" as an object with (POST/GET and even FILES etc). Then only refer to this object instead of the individual variable scopes.

This will allow you to create a new instance of said object and execute any methods of your choosing, or work with it in client code throughout your "module" (you could modify/normalise it as well). In addition, there is no need to modify the supermodels while doing so.

Very quick illustration which could easily be expanded upon:

  class Request
  {
    protected $params = array();

    public function __construct()
    {
      return $this;
    }

    public function getParam($key)
    {
      if (isSet($_params[$key])) {
        return $_POST[$key];
      } else if (isSet($_GET[$key])) {
        return $_GET[$key];
      } else if ($_POST[$key]) {
        return $_POST[$key];
      } else if ($_FILES[$key]) {
        // do something special with files here
      }
    }

    public function setParam($key, value);

    public function getParams();

    public function getPostParams();

    ...etc...etc
  }
like image 59
AlexP Avatar answered Mar 10 '26 08:03

AlexP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!