1

所以每次用户登录时,我都想为用户创建一个会话。我也在使用 Facebook API 登录。顺便说一句,我对 PHP 和一切都是全新的,如果我被打乱了,请道歉!

目前,我在登录控制器中拥有的是这个。

    if ($user) {
        try {
            // Proceed knowing you have a logged in user who's
            // authenticated.
            $user_profile = $facebook->api('/me');
        } catch (FacebookApiException $e) {
            error_log($e);
            $user = null;
        }

        if (! empty($user_profile)) {
            // Get the relevant information from the user's profile.
            $firstname = $user_profile['first_name'];
            $lastname = $user_profile['last_name'];
            $userid = $user_profile['id'];
            $model = new Enligne_Model_Users();
            $model->createUser($firstname, $lastname);
            $session = new Zend_Session_Namespace('session'); //Creates a new Zend namespace equivalent to session start.
            $session['uid'] = $userid; //sets the user id of the session to uid.
            Zend_Registry::set('usersession', $session); //sets the user session in the zend registry.

        }
    }

这是创建会话的正确位置吗?我希望用户在登录后“登录”,但是是的。我是否还需要编辑我的用户文件以反映我正在使用会话的事实?谢谢您的帮助!

4

1 回答 1

1

你可以这样做:

<?php
$user = $facebook->getUser();
//Create Zend_Session_Namespace
$session = new Zend_Session_Namespace('usersession');
if ($user) {
    try {
        // Proceed knowing you have a logged in user who's
        // authenticated.
        $user_profile = $facebook->api('/me');
        if (!empty($user_profile)) {
            // Get the relevant information from the user's profile.
            $firstname = $user_profile['first_name'];
            $lastname = $user_profile['last_name'];
            $userid = $user_profile['id'];
            $email = $user_profile['email'];

            $model = new Enligne_Model_Users();
            $result = $model->createUser($firstname, $lastname, $email);
            //gets the data from database and stored it into the session
            if ($result) {
                $session->userId = $result['id'];
                $session->fb_email = $result['emailId'];
                $session->fb_id = $result['socialId'];
                $session->fb_image = "http://graph.facebook.com/" . $userid . "/picture";
                $session->fb_firstName = $result['firstName'];
                $session->fb_lastName = $result['lastName'];
                //redirect to the controller or action
                $this->_redirect('your_controller_name');
            } else {
                //Redirect code                
            }
        }
    } catch (FacebookApiException $e) {
        error_log($e);
        $user = null;
    }
}
?>

希望它会帮助你。

于 2013-01-21T12:05:19.593 回答