How can I generate "variable names" from posted data?
Discuss How can I generate "variable names" from posted data? in the PHP Development forum on Dev Shed. How can I generate "variable names" from posted data? PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
Posts: 2,033
Time spent in forums: 1 Month 2 Weeks 6 Days 20 h 59 m 13 sec
Reputation Power: 812
You're right, Northie, but do note that PHP actively encourages dumping user input into variables. I mean, register_globals has more or less died out, but extract() lives on and keeps being used.
But since you don't need all this, anway, it shouldn't be a problem here.
Posts: 3,439
Time spent in forums: 3 Weeks 5 Days 14 h 20 m 45 sec
Reputation Power: 3896
What is the point of trying to extract your variables into the current scope? They exist in super global scope any way.
If you really really really want to extract them into your own code somewhere then use a registry class and tag/annotate/otherwise flag the variables to say that they came from GET, POST, SERVER, COOKIE, etc.
Knowing where your variables came from is more important than you may think and is the first step in wring secure code
eg
PHP Code:
//quick, dirty and untested
class Request {
private $store = array();
private static $instance;
private function __construct() {
}
public static function Load() {
if(!isset(self::$instance)) {
self::$instance = new __CLASS__;
}
return self::$instance;
}
public function set($data,$type) {
$this->store[$type] = $data;
}
public function get($type,$key=false) {
if($key) {
return $this->store[$type][$key]
}
Posts: 2,033
Time spent in forums: 1 Month 2 Weeks 6 Days 20 h 59 m 13 sec
Reputation Power: 812
@ zxcvbnm:
You forgot the escaping.
Apart from that, yes, that's the correct way.
Regarding the discussion about variables:
Even when it's not about values from POST, GET etc. (Northie already talked about that), I can't think of any situation where it might make sense to dynamically create loads of variables.
It's cumbersome, potentially dangerous, can easily lead to naming conflicts, is hard to manage and "pollutes" the scope -- and what's the benefit? To save a few characters?
When you have a lot of related data you need to save, just put it in an array.
Posts: 2,033
Time spent in forums: 1 Month 2 Weeks 6 Days 20 h 59 m 13 sec
Reputation Power: 812
Quote:
Originally Posted by zxcvbnm
I wonder now, if I have 40 fields, do I have to do this for each posted value?
Why do you even want to do that in the first place? What's the point of putting every value $this->input->post('xyz') into a variable $xyz?
Sure, the latter is 21 characters shorter. So what? If you're using a proper IDE, you have autocomplete, anyway.
So my question would be: Do you have an actual reason to put every POST value into a variable instead of simply accessing $this->input directly? Or isn't this rather a kind of bad habit?