Joomla 使用 JInput 检查空字符串
按照这个指南来清理我的输入,我想知道是否有一个空字符串被这个覆盖?
Following this guide to sanitize my inputs, I'm wondering if an empty string is covered with this?
$jinput = JFactory::getApplication()->input;
$this->name = $jinput->get('name', '', 'STRING');
如果没有 Joomla,我通常也会检查空字符串.类似的东西:
Typically without Joomla I'd be checking for an empty string as well. Something like:
if (!empty($_POST['name']))
查看 JInput get 方法,我看到它检查它是否是 isset
:
Looking at the JInput get method I see that it checks if it is isset
:
public function get($name, $default = null, $filter = 'cmd')
{
if (isset($this->data[$name]))
{
return $this->filter->clean($this->data[$name], $filter);
}
return $default;
}
不是一回事,因为 isset
只会检查 null.但是,这是使用 get 方法的默认值.因此,如果我为第二个参数指定一个空字符串,我是否会在这里涵盖?
Not the same thing, as isset
will only check for null. However that is the default value for using the get method. So if I specify an empty string for the second parameter am I covered here?
$this->name = $jinput->get('name', '', 'STRING');
推荐答案
Joomla 不能决定您的空字符串是否为有效值.他们必须使用 isset()
,因为如果他们使用 empty()
并且您返回 '0'
,这是您期望的正常情况,Joomla 将返回默认值而不是 '0'
.
It's not up to Joomla to decide whether your empty string is valid value or not. They have to use isset()
, because if they would use empty()
and you return '0'
which you would expect as normal, Joomla would return default value instead of that '0'
.
所以他们只是使用 isset()
来检查变量是否被设置是完全正常的,由你决定你接受什么值.
So it's completely normal that they just use isset()
to check if variable is set, and it's up to you to decide what values you accept.
如果未设置该值,并且您将第二个参数设置为空字符串''
,您将得到一个空字符串返回.
If the value isn't set, and you set as the second parameter empty string ''
, you'll get an empty string returned.
在您的示例中,将返回一个空字符串,这是预期的行为.
In your example an empty string would be returned, which is expected behaviour.
相关文章