PHP AJAX Joomla身份验证脚本

时间:2011-12-21 16:24:46

标签: php authentication post namespaces include

我有一个像这样的PHP脚本:

<?php
include 'authorization_script.php';

foreach ($_POST as $key => $value){
    //do something here
}
?>

问题是如果“authorization_script.php”传递的$ _POST变量只有一个数字名称,脚本就会崩溃。我只是希望确保没有$ _POST变量传递给include脚本,而不是解决这个问题。

为了使它工作,“authorization_script”不需要任何外部变量,但由于$ _POST变量默认是全局变量,因此它们将传递给此脚本。在one of my previous questions中建议我可以用PHP中的名称空间来解决这个问题。

我可以更改$ _POST变量的命名空间,以便它们不会传递给include脚本吗?如果是这样,有人可以帮助如何做到这一点?或者有更好的方法吗?

THX

编辑:而不是通过重命名$ _POST变量来破解我的方式,就像你们中的一些人建议我应该解决真正的问题。这是Joomla网站内的AJAX脚本。当我调用这个ajax脚本时,我仍然想要对用户进行身份验证,然后对本地数据库中的凭据进行分级。我发现从AJAX脚本验证用户的方式是这样的:

define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__));
define( 'DS', DIRECTORY_SEPARATOR );
require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

/* Create the Application */
$mainframe =& JFactory::getApplication('site');

/* Make sure we are logged in at all. */
if (JFactory::getUser()->id == 0)
    die("Access denied: login required.");

我认为错误是在joomla框架脚本中引发的(this script上的第528行)。

有哪些想法可以更好地向经验丰富的Joomla人员验证用户?

3 个答案:

答案 0 :(得分:3)

或者如果你不想修复你的包含脚本,你可以做这个糟糕的黑客攻击:

<?php
// watch this ugly hack
$post_hack = $_POST;
unset($_POST);
include 'authorization_script.php';
$_POST = $post_hack;

foreach ($_POST as $key => $value){
    //do something here
}
?>

答案 1 :(得分:0)

  

我可以更改$ _POST变量的命名空间,以便它们可以   没有传递给包含脚本?

没有。无论命名空间如何,全局都是全局的。

  

或者有更好的方法吗?

修复包含脚本。您应该努力使您的代码能够适应不良输入 - 尤其是执行授权的代码。

答案 2 :(得分:0)

这是非常难看的,但应该解决它:

<?php
$aPost = $_POST;
unset($_POST);
include 'authorization_script.php';

foreach ($aPost as $key => $value){
    //do something here
}
?>
相关问题