Joomla 使用 JInput 检查空字符串

Joomla check for empty string with JInput(Joomla 使用 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 检查空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Convert JSON integers and floats to strings(将JSON整数和浮点数转换为字符串)
in php how do I use preg replace to turn a url into a tinyurl(在php中,如何使用preg替换将URL转换为TinyURL)
all day appointment for ics calendar file wont work(ICS日历文件的全天约会不起作用)
trim function is giving unexpected values php(Trim函数提供了意外的值php)
Basic PDO connection to MySQL(到MySQL的基本PDO连接)
PHP number_format returns 1.00(Php number_Format返回1.00)