Skip to main content
Changed outlook after finding a reason to
Source Link
rybo111
  • 101
  • 1

When youAfter initially answering this question by saying there should be no reason to modify superglobals such as $_POST, you are tamperingI'm editing this answer with an example of a time when I've decided to.

I'm currently working on a URL rewrite database table whereby the original datarequest column directs the user submittedto its corresponding target column.

This data is important. For instance, you can keep a log of everything your users have ever submittedrequest might be blog/title-here and see where they sometimes are going wrongits target might be blog.php?id=1.

Ultimately, there isSince no reason to modify superglobals when you see how easy itblog.php is expecting $_GET variables, and I don't want to simply create an array based on whatchange the user has submitted. For instance:

$post = $_POST;

This creates a new array that you can tamper with to your heart's content. It looks nicer too:

echo $_POST["surname"];
// is the same as:
echo $post["surname"];

Alsoheader("Location:"), I'm not a fan of creating a new variable for every bit of information the user has sent. Arrays are much easier to work with - even simply trimming the valuesleft doing something like this:

//$uri Why have this:
$username = trimexplode($username);
$firstname ='?', trim($firstname$uri_request);[0];
$surname$params = trim($surname);

// When you can have this instead:
array_walk_recursiveexplode($post'?', function (&$val$uri_request) { [1];
  $val = trimparse_str($val);$params, 
}$_GET);

This creates a $_GET array containing the intended parameters passed by the target column.

Ultimately, I would strongly advise against modifying superglobals unless you absolutely have to.

When you modify superglobals such as $_POST, you are tampering with the original data the user submitted.

This data is important. For instance, you can keep a log of everything your users have ever submitted and see where they sometimes are going wrong.

Ultimately, there is no reason to modify superglobals when you see how easy it is to simply create an array based on what the user has submitted. For instance:

$post = $_POST;

This creates a new array that you can tamper with to your heart's content. It looks nicer too:

echo $_POST["surname"];
// is the same as:
echo $post["surname"];

Also, I'm not a fan of creating a new variable for every bit of information the user has sent. Arrays are much easier to work with - even simply trimming the values:

// Why have this:
$username = trim($username);
$firstname = trim($firstname);
$surname = trim($surname);

// When you can have this instead:
array_walk_recursive($post, function (&$val) { 
  $val = trim($val); 
});

After initially answering this question by saying there should be no reason to modify superglobals, I'm editing this answer with an example of a time when I've decided to.

I'm currently working on a URL rewrite database table whereby the request column directs the user to its corresponding target column.

For instance, a request might be blog/title-here and its target might be blog.php?id=1.

Since blog.php is expecting $_GET variables, and I don't want to change the header("Location:"), I'm left doing something like this:

$uri    = explode('?', $uri_request)[0];
$params = explode('?', $uri_request)[1];
parse_str($params, $_GET);

This creates a $_GET array containing the intended parameters passed by the target column.

Ultimately, I would strongly advise against modifying superglobals unless you absolutely have to.

Source Link
rybo111
  • 101
  • 1

When you modify superglobals such as $_POST, you are tampering with the original data the user submitted.

This data is important. For instance, you can keep a log of everything your users have ever submitted and see where they sometimes are going wrong.

Ultimately, there is no reason to modify superglobals when you see how easy it is to simply create an array based on what the user has submitted. For instance:

$post = $_POST;

This creates a new array that you can tamper with to your heart's content. It looks nicer too:

echo $_POST["surname"];
// is the same as:
echo $post["surname"];

Also, I'm not a fan of creating a new variable for every bit of information the user has sent. Arrays are much easier to work with - even simply trimming the values:

// Why have this:
$username = trim($username);
$firstname = trim($firstname);
$surname = trim($surname);

// When you can have this instead:
array_walk_recursive($post, function (&$val) { 
  $val = trim($val); 
});