因此,每次用户登录时,我都想为用户创建会话。我也使用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.
}
}
这是创建会话的正确位置吗?我希望用户在登录后“登录”,但是,是的。我是否还需要编辑我的用户文件以反映我正在使用会话的事实?谢谢你的帮助!
答案 0 :(得分: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;
}
}
?>
希望它能帮到你。