我正在尝试连接到其他数据库。我有一个脚本用于从SQL文件创建新实例。我通过插入主数据库表然后使用与所有用户的主登录表相同的数据库来创建用户。我正在尝试登录到另一个数据库并使用该数据库。我在尝试登录到另一个数据库时得到一个空白的主页,在某些情况下,我根本无法连接到另一个数据库,最终使用相同的主数据库。我将公司名称存储在会话中,并在所有模型文件中使用它并连接到数据库。我尝试了一切,但似乎没有任何工作。我的控制器模型和视图文件如下:
model:mod_login.php
<?php
class mod_login extends CI_Model{
var $myTables;
#print_r($this->db->last_query());
function __construct(){
parent::__construct();
$companyName = $companyName = $this->session->userdata('company');
$otherdb = $this->load->database("$companyName", TRUE);
$this->myTables=$this->config->item('myTables');
$this->load->helper('cookie');
}
function validate_admin_login(){
$this->form_validation->set_rules('username', 'Username', 'trim|required');
$this->form_validation->set_rules('userpass', 'Password', 'trim|required');
if ($this->form_validation->run() == TRUE){
return $this->check_admin_login();
}else{
return validation_errors();
}
}
function check_cookie_login(){
$this->db->where('username', trim($this->input->cookie('uusername')));
$this->db->where('userpass ', sha1(trim($this->input->cookie('userpass'))));
$this->db->where('status', '1');
$this->db->where('deleted', '0');
$this->db->select('*');
$query = $this->db->get($this->myTables['users']);
if($query->num_rows() > 0){
$row = $query->row();
$this->db->where('userid', $row->id);
$this->db->select('firstname,lastname,profileimage');
$query1 = $this->db->get($this->myTables['users_details']);
$row1 = $query1->row();
$newdata = array(
'is_admin_logged_in' => true,
'admin_user_name' => $row->username,
'admin_userpass' => $row->userpass,
'admin_id'=>$row->id,
'admin_lastlogin'=>date("d-m-Y H:i:s",$row->lastlogin),
'admin_lastloginip'=>$row->lastloginip,
'lastrefresh'=>time()
);
$this->session->set_userdata($newdata);
$this->update_admin_login_time($this->session->userdata('admin_id'));
$this->admin_init_elements->set_global_user($row->username,$row->userpass);
if($this->input->post('remember'))
{
$cookie = array('name' => 'username','value' => $row->username,'expire' => time()+7600,'secure' => false);
$this->input->set_cookie($cookie);
}
$name = $row1->firstname.' '.$row1->lastname;
$cookie1 = array('name' => 'name','value' => $name,'expire' => time()+7600,'secure' => false);
$this->input->set_cookie($cookie1);
$cookie2 = array('name' => 'image','value' => $row1->profileimage,'expire' => time()+7600,'secure' => false);
$this->input->set_cookie($cookie2);
return 'Login Successful';
}else{
}
}
function check_admin_login(){
$this->db->where('username', trim($this->input->post('username')));
$this->db->where('userpass ', sha1(trim($this->input->post('userpass'))));
$this->db->where('status', '1');
$this->db->where('deleted', '0');
$this->db->select('*');
$query = $this->db->get($this->myTables['users']);
if($query->num_rows() > 0){
$row = $query->row();
$this->db->where('userid', $row->id);
$this->db->select('firstname,lastname,profileimage,company');
//$query1 = $this->db->query("Select * from pr_users_details where userid = '".$row->id."'");
$query1 = $this->db->get($this->myTables['users_details']);
$row1 = $query1->row();
$newdata = array(
'is_admin_logged_in' => true,
'admin_user_name' => $row->username,
'admin_userpass' => $row->userpass,
'admin_id'=>$row->id,
'admin_lastlogin'=>date("d-m-Y H:i:s",$row->lastlogin),
'admin_lastloginip'=>$row->lastloginip,
'lastrefresh'=>time(),
'company'=>$row1->company
);
$company = $row1->company;
$this->session->set_userdata($newdata);
$companyName = $this->session->userdata('company');
$otherdb = $this->load->database("$companyName", TRUE);
echo "<pre>";
print_r($this->session->all_userdata());
echo "</pre>";
$this->update_admin_login_time($this->session->userdata('admin_id'));
$this->admin_init_elements->set_global_user($row->username,$row->userpass);
if($this->input->post('remember'))
{
$cookie = array('name' => 'username','value' => $row->username,'expire' => time()+7600,'secure' => false);
$this->input->set_cookie($cookie);
}
$name = $row1->firstname.' '.$row1->lastname;
$cookie1 = array('name' => 'name','value' => $name,'expire' => time()+7600,'secure' => false);
$this->input->set_cookie($cookie1);
$cookie2 = array('name' => 'image','value' => $row1->profileimage,'expire' => time()+7600,'secure' => false);
$this->input->set_cookie($cookie2);
return 'Login Successful';
}else{
return 'Incorrect Username or Password.';
}
}
控制器:login.php
<?php
class login extends CI_Controller
{
var $data;
function __construct() {
parent::__construct();
$this->load->helper('cookie');
$is_admin_logged_in = $this->admin_init_elements->admin_logged_in_status();
global $USER;
if($is_admin_logged_in == TRUE){
redirect('home');
//;
}
//populate viewfor header / footer elements
$this->admin_init_elements->init_elements('N');
$this->load->model('mod_login');
}
function index(){
//if Admin already logged in, send to Admin home
$this->data['message']='';
$this->data['msg_class'] = '';
$post_array=$this->input->post();
$data['old_images']=$this->mod_common->getBgImages();
if($this->input->cookie('remember') == 'on')
{
//echo $this->input->cookie('username');
$this->data['message']=strip_tags($this->mod_login->check_cookie_login());
if($this->data['message']=='Login Successful'){
$this->data['msg_class'] = 'sukses';
redirect('home');
}else{
$this->data['msg_class'] = 'gagal';
}
}
if($this->input->post('action')=='adminLogin'){
//print_r($this->input->post()); die;
if(isset($post_array['remember'])){
$username_cookie= array(
'name' => 'uusername',
'value' => $post_array['username'],
'expire' => '865000',
'secure' => FALSE
);
$password_cookie= array(
'name' => 'userpass',
'value' => $post_array['userpass'],
'expire' => '865000',
'secure' => FALSE
);
$remember_cookie= array(
'name' => 'remember',
'value' => 'on',
'expire' => '865000',
'secure' => FALSE
);
$this->input->set_cookie($username_cookie);
$this->input->set_cookie($password_cookie);
$this->input->set_cookie($remember_cookie);
//die;
}
else
{
if($this->input->cookie('remember') == 'on')
{
if($this->input->cookie('uusername') != $post_array['username'])
{
delete_cookie("remember");
delete_cookie("uusername");
delete_cookie("userpass");
}
}
}
$this->data['message']=strip_tags($this->mod_login->validate_admin_login());
if($this->data['message']=='Login Successful'){
$this->data['msg_class'] = 'sukses';
redirect('home');
}else{
$this->data['msg_class'] = 'gagal';
}
} /*else if(isset()){
}*/
///////////////////////
$this->data['cookieRemember'] = $this->input->cookie('remember');
$this->data['cookieUsername'] = $this->input->cookie('username');
$this->data['cookiePassword'] = $this->input->cookie('userpass');
//echo $this->data['cookieRemember'];echo $this->data['cookieUsername']; echo $this->data['cookiePassword'];
//////////////////////
$this->data['cookiename'] = $this->input->cookie('name', false);
$this->data['cookieimage'] = $this->input->cookie('image', false);
$sess_msg = $this->session->userdata('session_msg');
$session_msg_class = $this->session->userdata('session_msg_class');
if(isset($sess_msg) && $sess_msg!= ''){
$this->data['message']=$sess_msg;
$this->data['msg_class'] = $session_msg_class!=''?$session_msg_class:'gagal';
}
//render full layout, specific to this function
$this->load->view('login', $this->data);
}
查看:home.php
<?php
class home extends CI_Controller
{
var $data;
function __construct() {
parent::__construct();
//populate viewfor header / footer elements
$this->admin_init_elements->init_elements();
#$this->load->model('admin_m');
}
function index(){
$data = array();
//if Admin already logged in, send to Admin home
#$this->data['maincontent'] = $this->load->view('maincontents/gridview',$data,TRUE);
$data['lastlogin_details'] = $this->admin_lastlogin_details();
$this->data['maincontent'] = $this->load->view('maincontents/dashboard',$data,true);
//render full layout, specific to this function
$this->load->view('layout', $this->data);
}
function admin_lastlogin_details(){
$lastlogin_det['lastlogin'] = $this->session->userdata('admin_lastlogin');
$lastlogin_det['lastloginip'] = $this->session->userdata('admin_lastloginip');
return $lastlogin_det;
}
它显示home.php但显示空白页面没有错误