Forum Moderators: coopster

Message Too Old, No Replies

Any reason not to add elements to $_POST?

         

Salsa

11:01 pm on Nov 7, 2004 (gmt 0)

10+ Year Member



With Register Globals off, besides using $_POST and $_GET I generally get additional global variables into a navigation function either as arguments of the function (usually putting them in an array first) or by defining constants, as appropriate.

For example, near the top of a script I'll usually define a variable like $PHP_SELF as a constant [define('PHP_SELF',$PHP_SELF);] so I can easily access it as a constant within a function.

However, because arrays like $_POST are super global, in the main script I can also put a variable like $PHP_SELF into the $_POST array; e.g., [$_POST["PHP_SELF"] = $PHP_SELF;].

It works, and it makes it very easy to extract all of the values in $_POST(++) in a foreach/variable variable line of code within a function. BUT, is there any downside to adding elements to $_POST? What's a better way?

Thanks in advance for your comments.
Salsa

growingdigital

12:49 am on Nov 8, 2004 (gmt 0)

10+ Year Member



Why not just stuff these values into session variables?

mincklerstraat

10:33 am on Nov 8, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can also access $PHP_SELF with the autoglobal $_SERVER['PHP_SELF'] inside functions without globalizing anything.

Probably better than putting global functions in any of the pre-defined variables is just making your own array of globals your script uses and globalizing that one array name in the functions which need it. This just helps you keep your code straight and remember what's coming in from where.

However, your idea is interesting; I think many would think of this as 'sloppy coding', but if it's your own code and you really know what your doing with it, yeah, this might save you that little bit of time. A downside of it is that you'll really have to beef up security. E.g., if you're extracting every value in POST automatically, and somebody issues an HTTP request to your file with POST values in a hacking attempt, you could be set for problems. In the long run it will probably be easiest to keep your code secure, legible, and maintainable just by using conventional coding practices.