doushaqing7080 2016-11-14 15:55
浏览 76
已采纳

使用参数'method'作为<data-source>的JInput导致'致命错误:在非对象上调用成员函数getString()'

I'm quite new to Joomla! (and PHP in general) and trying to learn by developing a website on my local Joomla!-Installation. I'm using WAMP-Server with PHP 5.5.12, Apache 2.4.9 and Joomla! 3.6.4.

Now I like to retrieve data from both $_POST and $_GET. Both are equaly insecure so it is only logical to retrieve and treat them together.

According to this article https://docs.joomla.org/Secure_coding_guidelines#Secure_strings i should be able to do it like this:

$string = JFactory::getApplication()->input->method->getString( 'myText', '' );

It's not working, complaining that 'method' is a non-object. ('Fatal error: Call to a member function getString() on a non-object')

All other data-source's from that same list (e.g. 'get', 'post', 'cookie', 'request', etc.) do not produce any error and seem to work flawless.

Unfortunately I need to retrieve data from either $_POST or $_GET (or both, but without $_COOKIE) wich is exactly what data-source='method' is supposed to do.

Of course I can use 'post' and 'get' sequentially but that seems stupid to me if there is an option wich could do it directly (less overhead? and slimmer code). Than I maybe have to address priority, but let's leave that aside for now.

At https://docs.joomla.org/Retrieving_request_data_using_JInput the only Super-Global-'s mentioned are 'get', 'post' and 'server'. Not a word about the other sources that obviously exist (no error occurring) or wich of the named sources is used as default.

My search has gone in circles for a while now and i can't find more related informations (targeting Joomla! or JInput, not PHP). If I'm missing something fundamental here, feel free to tell me.

With this said my questions are now:

Is there any setting (or update) i have to make to get the 'method'-data-source working?

Is there another value (!='method') for data-source in JInput that can be used to directly retrieve data from exactly either $_POST or $_GET or do I need to sequentially call 'post' and 'get' to accomplish this (maybe 'method' was renamed due to a conflict in names)?

Thanks for your time reading (and maybe answering).

  • 写回答

1条回答 默认 最新

  • dpa89292 2016-11-15 14:03
    关注

    I finally figured it out, thx to @Xorifelse for a push in the right direction.

    The answers to my questions would be:

    1) Is there any setting (or update) i have to make to get the 'method'- working?


    One would have to add an array called '_METHOD' to the $GLOBALS-array in a way like this:

    $GLOBALS['_METHOD'] = array_merge($_GET,$_POST);
    

    Calling the JInput with data-source='method' then would return the merged content of $_GET and $_POST, with $_POST overwriting values in $_GET if they have the same key.


    2) Is there another value (!='method') for data-source in JInput that can be used...?

    Every existing array inside the $GLOBALS-array (who's name starts with an underscore ('_') character and consists of uppercase characters only) can be used by calling the JInput with data-source beeing the name of that array (without the leading underscore).

    If one has newly created and/or filled that array prior to the call by JInput doesn't matter. So using a name like _GETANDPOST instead of _METHOD in the first question would describe its purpose mutch better.


    BUT....

    messing around with $GLOBALS is commonly considered bad practice!!


    For everyone wondering how these values for the data-source in JInput work and why they seemingly could be called like a method even they aren't methods:

    the answer is to be found in 'magic methods' and 'property overloading'.

    https://secure.php.net/manual/en/language.oop5.magic.php

    https://secure.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图