我正在使用CodeIgniter来开发我的登录系统。通过获取当前用户信息的控制器类的核心扩展,我在整个app控制器中使用$ this->用户来显示用户的信息。在我当地的WAMP服务器上,这非常有效。但是,在我的Web服务器上,它始终在您登录后显示来宾的信息。注意var_dump朝向成员获取信息方法的底部。这始终显示正确的帐户信息。但是一旦我从控制器扩展中调用它,它就会显示访客信息。
Controller扩展类:
class MY_Controller extends CI_Controller {
var $user = array();
function __construct(){
parent::__construct();
$this->benchmark->mark('foo');
//$this->output->cache(5);
if(!$this->config->item('global_maintenance')){
$this->member->get_info($this->session->userdata('member_id'));
if($this->member->error == NULL){
$this->user = $this->member->info;
} else {
die($this->member->error);
}
} else {
die('BaseCMD is currently down for routine maintenance. Please try again later!');
}
}
}
会员模型:
<?php
class Member extends CI_Model {
var $info = array();
var $activation_code = '';
var $error = NULL;
function __construct(){
parent::__construct();
$this->error = NULL;
}
function default_info(){
$info = array(
'id' => 0,
'display_name' => 'Guest',
'email_address' => '',
'password' => '',
'status' => 0,
'activation_code' => '',
'location' => '',
'date_joined' => '',
'gender' => '',
'results_per_page' => 10,
'admin_emails' => FALSE,
'member_emails' => FALSE,
'team' => array('id' => 0),
'perms' => array(),
'group' => array('id' => 0),
'stats' => array(),
'signature' => '',
'last_active' => 0,
'last_forum_visit' => 0,
'member_profile' => 'Guest',
'date_of_birth' => '',
'gender' => '',
'profile_banner' => '',
'avatar' => array('url' => '')
);
return $info;
}
public function get_info($criteria = 0){
if(is_numeric($criteria)){
$where = "WHERE m.member_id = ".$criteria;
} else {
$where = "WHERE email_address = '".$criteria."'";
}
$query_member = "
SELECT
m.member_id AS member_id, m.display_name, m.email_address, m.group_id, m.status, m.activation_code, UNIX_TIMESTAMP(m.date_joined) AS date_joined,
m.gender, m.location, m.biography, m.mantra, m.birth_date, m.results_per_page, m.admin_emails, m.member_emails, m.last_active, m.avatar_id,
m.banner_id, m.signature, m.newsletter_subscription, m.recruiting_status, m.facebook_username, m.website, m.steam_username, m.xboxlive_gamertag, m.psn_id,
g.group_id, g.title, g.description,
a.attachment_id, a.file_name,
f.message_id, f.author_id, COUNT(f.message_id) AS forum_count,
b.attachment_id AS banner_id, b.file_name AS banner_file,
mr.request_id, mr.author_id, mr.recipient_id, mr.status, COUNT(mr.request_id) AS total_friends,
tm.team_member_id, tm.member_id, tm.team_id
FROM members AS m
LEFT JOIN member_groups AS g ON (m.group_id = g.group_id)
LEFT JOIN attachments AS a ON (m.avatar_id = a.attachment_id)
LEFT JOIN forum_messages AS f ON (m.member_id = f.author_id)
LEFT JOIN attachments AS b ON (m.banner_id = b.attachment_id)
LEFT JOIN member_requests AS mr ON (m.member_id = mr.author_id OR m.member_id = mr.recipient_id) AND mr.status = 1
LEFT JOIN team_members AS tm ON (m.member_id = tm.member_id) AND date_left = ''
".$where."
GROUP BY m.member_id
LIMIT 1";
//show_error($query_member);
if($query_member = $this->db->query($query_member)){
if($query_member->num_rows() > 0){
$member = $query_member->row_array();
$member_id = $member['member_id'];
var_dump($this->session->all_userdata());
$info = array(
'id' => $member['member_id'],
'display_name' => $member['display_name'],
'email_address' => $member['email_address'],
'status' => ($member['status'] == 0) ? FALSE : TRUE,
'activation_code' => $member['activation_code'],
'location' => $member['location'],
'date_joined' => date('M jS, Y', $member['date_joined']),
'gender' => ($member['gender'] == 0) ? 'Male' : 'Female',
'results_per_page' => $member['results_per_page'],
'admin_emails' => ($member['admin_emails'] == 0) ? FALSE : TRUE,
'member_emails' => ($member['member_emails'] == 0) ? FALSE : TRUE,
'group' => array(
'id' => $member['group_id'],
'title' => $member['title'],
'description' => $member['description']
),
'signature' => $member['signature'],
'last_active' => strtotime($member['last_active']),
'member_profile' => anchor('members/'.url_title($member['display_name']).'/'.$member['member_id'], $member['display_name']),
'newsletter' => ($member['newsletter_subscription'] == 1) ? TRUE : FALSE,
'date_of_birth' => date('M jS, Y', strtotime($member['birth_date'])),
'birth_date' => date('Y-m-d', strtotime($member['birth_date'])),
'age' => floor((time() - strtotime($member['birth_date'])) / (60 * 60 * 24 * 365)),
'gender' => $member['gender'],
'biography' => $member['biography'],
'mantra' => $member['mantra'],
'contact' => array(
'email' => ($member['member_emails'] == '1') ? $member['email_address'] : NULL,
'website' => ($member['website'] != '') ? $member['website'] : NULL,
'facebook' => ($member['facebook_username'] != '') ? 'http://www.facebook.com/'.$member['facebook_username'] : NULL,
'steam' => ($member['steam_username'] != '') ? 'http://www.steamcommunity.com/id/'.$member['steam_username'] : NULL,
),
'gaming' => array(
'xboxlive' => $member['xboxlive_gamertag'],
'psn' => $member['psn_id'],
'steam' => $member['steam_username']
),
'statistics' => array('post_count' => $member['forum_count']),
'avatar' => array(
'attachment_id' => $member['attachment_id'],
'url' => base_url('attachments/'.$member['file_name'])
),
'banner' => array(
'id' => $member['banner_id'],
'url' => base_url('attachments/'.$member['banner_file'])
),
'friend_status' => FALSE,
'recruiting_status' => ($member['team_id'] == '' && $member['recruiting_status'] == '1') ? TRUE : FALSE
);
//$info['friend_results'] = $this->friend_results($member_id);
var_dump($info);
$this->info = $info;
} else {
$this->info = $this->default_info();
}
} else {
$this->error = $this->db->_error_message();
}
//show_error(var_dump($this->info));
}
答案 0 :(得分:2)
好的,所以在从我的控制器扩展,成员模型和我的模板转储变量之后,我意识到我的问题与我的会话或我的类没有任何关系。实际上我的条件语句确定用户是否已登录,而不是我在模板中使用。
<?php
if($this->user['id'] === 0){// This is correct.
echo "You're logged out.";
}
if($this->user['id'] == 0) // This is what I was using and is not correct, apparently.