dpqjvoq9033 2015-01-29 16:52
浏览 71
已采纳

PHP致命错误:在不在对象上下文中时使用$ this ...用于重定向()

I am new to PHP. I've been trying to get this authorization code to work, but am getting this error:

Call to undefined function redirect()

Code:

$Auth = Auth::getAuth();
if($Auth->loggedIn())
{
    redirect(getCoreSitePath().'/account.'.SITE_CONFIG_PAGE_EXTENSION);
}

if(isset($_REQUEST['error_description']))
{
    $error = $_REQUEST['error_description'];
}
redirect(getCoreSitePath()."/login.".SITE_CONFIG_PAGE_EXTENSION."?social_login_error=".urlencode($error));

Based on some Googling, I saw that I might need to add $this->load->helper('url');

Revised code:

$Auth = Auth::getAuth();
$this->load->helper('url');
if($Auth->loggedIn())
{
    redirect(getCoreSitePath().'/account_home.'.SITE_CONFIG_PAGE_EXTENSION);
}

if(isset($_REQUEST['error_description']))
{
    $error = $_REQUEST['error_description'];
}
redirect(getCoreSitePath()."/login.".SITE_CONFIG_PAGE_EXTENSION."?plugin_social_login_error=".urlencode($error));

But now I get this error:

Using $this when not in object context

I tried the suggestions here but wasn't able to get it working.

  • 写回答

1条回答 默认 最新

  • doubu7134 2015-01-29 16:58
    关注

    redirect() is not a built-in function, do you have it defined somewhere? If not, you can simply define one without having to change your code

    function redirect($url)
    {
        header("Location: $url");
        die();
    }
    

    Note that this function is not really needed, you can simply change your redirect() calls with the header call that this function makes but i just added it because you have alot of calls for redirect() and will need to change a lot of code to do it that way. And you can define this function outside any class and then you can use it without $this->

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

报告相同问题?

悬赏问题

  • ¥20 matlab yalmip kkt 双层优化问题
  • ¥15 如何在3D高斯飞溅的渲染的场景中获得一个可控的旋转物体
  • ¥88 实在没有想法,需要个思路
  • ¥15 MATLAB报错输入参数太多
  • ¥15 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题