I’m writing a bot for the IMified network.
I want to filter the items processed by my bot by only accepting certain values into my script.
This is what I’m using right now:
$items = array('botkey', 'userkey', 'network', 'user', 'channel', 'msg', 'step');
foreach ($_POST as $key => $value)
{
if (in_array($key, $items) || preg_match('value\d*', $key))
{
$this->data[$key] = $value;
}
}
- Is there any way to do this better?
- Why doesn’t my regular expression work – it is meant to only allow keys named like
value1234or anything similar (“value” + number), but it doesn’t let anything through.
There’s always a better way, but — as long as you put some taint/sanity checking into your code for the values, you’re doing just fine.
Your regular expression syntax is wrong. Try:
preg_match('/^value\d+$/', $key)