Joomla check for empty string with JInput(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.
这篇关于Joomla 使用 JInput 检查空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Joomla 使用 JInput 检查空字符串
基础教程推荐
- 有什么方法可以用编码 UTF-8 而不是 Unicode 返回 PHP`json_encode`? 2021-01-01
- PHP 类:全局变量作为类中的属性 2021-01-01
- 如何替换eregi() 2022-01-01
- 如何在 Laravel 5.3 注册中添加动态下拉列表列? 2021-01-01
- PHP PDO MySQL 查询 LIKE ->多个关键词 2021-01-01
- Cron Jobs 调用带有变量的 PHP 脚本 2022-01-01
- 在PHP中根据W3C规范Unicode 2022-01-01
- YouTube API v3 点赞视频,但计数器不增加 2022-01-01
- 如何在 Laravel 中使用 React Router? 2022-01-01
- 学说 dbal querybuilder 作为准备好的语句 2022-01-01
