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 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了