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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀