【问题标题】:PHP : Serialization of a closure is not allowedPHP:不允许序列化闭包
【发布时间】:2017-02-14 13:41:58
【问题描述】:

整个场景是我在我的 opencart 商店中添加了 facebook 登录扩展,当新用户点击登录时,一切正常,使用 facebook 添加电子邮件和密码并点击登录,然后出现一个确认登录对话框,其中有两个按钮“取消”如果用户单击登录,则“登录”会将用户带到我的商店,但是当用户单击“取消”时,它应该将用户带回到我商店的登录页面,但它会给出错误 "Fatal error : uncaught exception with a message serialization of closure is not allowed
这是这条线 "$_SESSION["HA::STORE"][$key] = serialize($value); 我正在使用包含此行的以下函数:

public function set( $key, $value)
    {
        $key = strtolower( $key );

        $_SESSION["HA::STORE"][$key] = serialize($value);
    }

我试过这个 var_dump(序列化($value));它返回一个字符串 我怎样才能序列化这个?我已经搜索过,但没有找到任何有用的解决方案

更新:

function login()
    {
        Hybrid_Logger::info( "Enter Hybrid_Provider_Adapter::login( {$this->id} ) " );

        if( ! $this->adapter ){
            throw new Exception( "Hybrid_Provider_Adapter::login() should not directly used." );
        }

        // clear all unneeded params
        foreach( Hybrid_Auth::$config["providers"] as $idpid => $params ){
            Hybrid_Auth::storage()->delete( "hauth_session.{$idpid}.hauth_return_to"    );
            Hybrid_Auth::storage()->delete( "hauth_session.{$idpid}.hauth_endpoint"     );
            Hybrid_Auth::storage()->delete( "hauth_session.{$idpid}.id_provider_params" );
        }

        // make a fresh start
        $this->logout();

        # get hybridauth base url
        $HYBRID_AUTH_URL_BASE = Hybrid_Auth::$config["base_url"];

        # we make use of session_id() as storage hash to identify the current user
        # using session_regenerate_id() will be a problem, but ..
        $this->params["hauth_token"] = session_id();

        # set request timestamp
        $this->params["hauth_time"]  = time();

        # for default HybridAuth endpoint url hauth_login_start_url
        #   auth.start  required  the IDp ID
        #   auth.time   optional  login request timestamp
        $this->params["login_start"] = $HYBRID_AUTH_URL_BASE . ( strpos( $HYBRID_AUTH_URL_BASE, '?' ) ? '&' : '?' ) . "hauth.start={$this->id}&hauth.time={$this->params["hauth_time"]}";

        # for default HybridAuth endpoint url hauth_login_done_url
        #   auth.done   required  the IDp ID
        $this->params["login_done"]  = $HYBRID_AUTH_URL_BASE . ( strpos( $HYBRID_AUTH_URL_BASE, '?' ) ? '&' : '?' ) . "hauth.done={$this->id}";

        Hybrid_Auth::storage()->set( "hauth_session.{$this->id}.hauth_return_to"    , $this->params["hauth_return_to"] );
        Hybrid_Auth::storage()->set( "hauth_session.{$this->id}.hauth_endpoint"     , $this->params["login_done"] ); 
        Hybrid_Auth::storage()->set( "hauth_session.{$this->id}.id_provider_params" , $this->params );

        // store config to be used by the end point 
        Hybrid_Auth::storage()->config( "CONFIG", Hybrid_Auth::$config );

        // move on
        Hybrid_Logger::debug( "Hybrid_Provider_Adapter::login( {$this->id} ), redirect the user to login_start URL." );

        Hybrid_Auth::redirect( $this->params["login_start"] );
    }

在 function_login 中,set() 被 this 调用:

Hybrid_Auth::storage()->set( "hauth_session.{$this->id}.hauth_return_to"    , $this->params["hauth_return_to"] );
        Hybrid_Auth::storage()->set( "hauth_session.{$this->id}.hauth_endpoint"     , $this->params["login_done"] ); 
        Hybrid_Auth::storage()->set( "hauth_session.{$this->id}.id_provider_params" , $this->params );

【问题讨论】:

  • 问题出在set() 的调用者中,它没有传递正确的参数。我怀疑你忘了在函数名后面加上() 来调用函数,所以它试图将函数本身存储在会话中。
  • 显示调用set()的代码。
  • @Barmar 我已经更新了问题,请看一下
  • 如果你在$_SESSION 中存储了一些东西,你不需要手动序列化它,这已经发生在会话处理程序代码中。就做$_SESSION["HA::STORE"][$key] = $value;
  • 当我使用 $_SESSION["HA::STORE"][$key] = $value;它重定向回同一页面,此变量 $value 包含用户单击取消按钮时的地址

标签: php facebook serialization opencart2.x


【解决方案1】:

我猜你正在传递或被传递一个 closure 作为 set() 的 $value 参数。

您需要使用reflection 来检查和忽略闭包

public function set( $key, $value)
{
    if (is_object($value)) {
        try {
            $reflection = new ReflectionFunction($value);
            if ($reflection->isClosure()) {
                //Trigger a E_USER_NOTICE if you want to avoid silently failing
                trigger_error("You cannot pass a closure as the second parameter of set()");
                return; // Do nothing else
            }
        } catch (\ReflectionException $e) {
            // Catch the exception thrown if $value is not a closure
        }
    }
    $key = strtolower( $key );

    $_SESSION["HA::STORE"][$key] = serialize($value);
}

或者您可以捕获并忽略异常:

function set( $key, $value)
{
    $key = strtolower( $key );
    try{
        $_SESSION["HA::STORE"][$key] = serialize($value);
    } catch (\Exception $e) {
        // Catch the exception thrown and handle it however you want
    }
}

或者看看这个github project for serializing closures

【讨论】:

  • var_dump(serialize($value));这返回一个字符串。我不明白 $value 是如何闭包的?
  • @mehar444 你没有提供足够的上下文让我解释。当$value 是闭包时运行var_dump(serialize($value)); 将导致抛出异常,如下所述:stackoverflow.com/questions/13734224/…
  • 在尝试了你的代码之后,它给了我这个:警告:ReflectionFunction::__construct() 期望参数 1 是字符串,数组在 /var/www/html/mdpocketClipboards.com/catalog/model/ 中给出social_login_free/hybrid/storage.php 在第 63 行
  • 其实我用的是facebook api
  • 当用户在 Facebook 登录对话框中单击取消时出现错误。
猜你喜欢
  • 2014-04-21
  • 2014-06-20
  • 1970-01-01
  • 1970-01-01
  • 2020-04-06
  • 2012-11-23
  • 2018-05-08
  • 1970-01-01
  • 2019-01-30
相关资源
最近更新 更多