Codeigniter检查index.php文件中是否有LoggedIn
我真的需要帮助,2天前我自己在寻找解决方案,但没有结果。 我需要检查用户是否使用以下方式登录: $CI=&get_instance(); $loggedIn=$CI->session->userdata('loggedIn')==TRUE 如果$loggedIn为true,则表示用户已登录。我需要在索引文件中对此进行检查,因为我从以下位置选择了数据库:Codeigniter检查index.php文件中是否有LoggedIn,php,mysql,codeigniter,Php,Mysql,Codeigniter,我真的需要帮助,2天前我自己在寻找解决方案,但没有结果。 我需要检查用户是否使用以下方式登录: $CI=&get_instance(); $loggedIn=$CI->session->userdata('loggedIn')==TRUE 如果$loggedIn为true,则表示用户已登录。我需要在索引文件中对此进行检查,因为我从以下位置选择了数据库: $parsed = parse_url($_SERVER['REQUEST_URI']); $path = $parsed['path']; $
$parsed = parse_url($_SERVER['REQUEST_URI']);
$path = $parsed['path'];
$path_parts = explode('/', $path);
$db = $path_parts[4];
if(isset($db) && is_numeric($db)){
define('DB', $db);
}
$db是我的数据库的名称,因此如果我在登录页面中,就不会有问题,因为它在url中设置为数字。但当im登录时,我会在会话变量中存储$db,因此我需要执行以下测试:
if($this->session->userdata('loggedin') == TRUE){
define('DB', $this->session->userdata('db'));
}
我的数据库文件:
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = '*********';
$db['default']['password'] = '*********';
$db['default']['database'] = DB;
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = FALSE;
$db['default']['db_debug'] = TRUE;
$db['default']['cache_on'] = FALSE;
$db['default']['cachedir'] = '';
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_general_ci';
$db['default']['swap_pre'] = '';
$db['default']['autoinit'] = TRUE;
$db['default']['stricton'] = TRUE;
问题是我需要通过url使用不同的数据库。如果用户输入此链接,例如:/user/login/34,他将使用名为34的数据库
我正在使用codeigniter php框架
core/MY_Controller.php的代码
class MY_Controller extends CI_Controller
{
public $data = array();
function __construct ()
{
parent::__construct();
$this->data['errors'] = array();
$this->data['app_name'] = config_item('app_name');
$this->load->helper('form');
$this->load->library('form_validation');
$this->load->model('user_m');
$db_prefix = 'autojahi_dev';
$config = array(
'hostname' => 'localhost',
'username' => '****',
'password' => '****',
'database' => '****',
'dbdriver' => 'mysql',
'dbprefix' => '',
'pconnect' => FALSE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => 'cache',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'autoinit' => TRUE,
'stricton' => TRUE
);
if($this->uri->segment(4)){
$config['database'] = $this->uri->segment(4);
}
$this->load->database($config);
//$this->db->reconnect();//shouldnt be required but try it if there is problem
echo $this->db->database;//for testing
$current_link = uri_string();
// Algorithm of autojahiz access autorization
$allow_not_loggedin = array('login', 'logout', 'reset_pass', 'send_reset_pass_email', 'reset_pass_form', 'update_pass', 'views', 'invalid_link' );
switch (strtolower( $this->router->class ) )
{
case 'user':
$allow_loggedin = array('denied', 'index', 'edit', 'logout');
break;
case 'dashboard':
$allow_loggedin = array('denied', 'index');
break;
}
if ( $this->session->userdata('loggedin') == FALSE )
{
if (!in_array( $this->router->method, $allow_not_loggedin) )
{
redirect( 'user/login' );
}
}
else
{
if (!in_array( $this->router->method, $allow_loggedin ) )
{
//redirect( 'user/denied' );
}
}
}
}
如果我是您,我将定义将使用的所有数据库,如下所示:
$db[30] = array(
'hostname' => '',
'username' => '',
'password' => '',
'database' => '',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => APPPATH .'cache',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'autoinit' => TRUE,
'stricton' => FALSE
);
$db[32] = array(
'hostname' => '',
'username' => '',
'password' => '',
'database' => '',
'dbdriver' => 'pdo',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => APPPATH .'cache',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'autoinit' => TRUE,
'stricton' => FALSE
);
$active_group = SELECTED_DB;
然后我将使用钩子定义所选的\u DB
,或者在默认\u控制器中定义:
if($this->session->userdata('loggedin') == TRUE){
define('SELECTED_DB', $this->session->userdata('selecdb'));
}
else{
$selected_db = $this->uri->segment(2); // or use $this->input->get('db');
$this->load->database($selected_db);
defined('SELECTED_DB')
|| define('SELECTED_DB', $selected_db);
// Do login
}
在控制器中执行此操作。为了方便起见,您可以在application/core文件夹中创建一个名为MY_Controller.php
的扩展文件,并扩展到您的控制器,如
class-Other\u-controller扩展我的\u-controller{}
在application/core/MY_Controller.php中构造函数do
class MY_controller extends CI_Controller{
public function __construct() {
parent::__construct();
$config = [
'hostname' => '',
'username' => '',
'password' => '',
'database' => 'defaultdb',
'dbdriver' => 'sqlsrv',
'dbprefix' => '',
'pconnect' => TRUE,
'db_debug' => TRUE,
'cache_on' => FALSE,
'cachedir' => APPPATH .'cache',
'char_set' => 'utf8',
'dbcollat' => 'utf8_general_ci',
'swap_pre' => '',
'autoinit' => TRUE,
'stricton' => FALSE
];
if($this->uri->segment(4)){
$config['database']=$this->uri->segment(4);
}
$this->load->database($config);
$this->db->reconnect();//shouldnt be required but try it if there is problem
echo $this->db->database;//for testing
}
不确定这是否正是您所需要的,但以下是我将如何做到这一点:
在application/core中,创建MY_Loader.php
class MY_Loader extends CI_Loader
{
public function database($params = '', $return = FALSE, $query_builder = NULL)
{
// Grab the super object
$CI =& get_instance();
$CI->load->library('session');
$db_name = "mydefault_db";
if($CI->session->userdata('db'))
$db_name = $CI->session->userdata('db');
$params['hostname'] = "localhost";
$params['username'] = "root";
$params['password'] = "";
$params['database'] = $db_name;
$params['dbdriver'] = "mysqli";
$params['dbprefix'] = "";
$params['pconnect'] = FALSE;
$params['db_debug'] = TRUE;
$params['cache_on'] = FALSE;
$params['cachedir'] = "";
$params['char_set'] = "utf8";
$params['dbcollat'] = "utf8_general_ci";
// Do we even need to load the database class?
if ($return === FALSE && $query_builder === NULL && isset($CI->db) && is_object($CI->db) && ! empty($CI->db->conn_id))
{
return FALSE;
}
require_once(BASEPATH.'database/DB.php');
if ($return === TRUE)
{
return DB($params, $query_builder);
}
// Initialize the db variable. Needed to prevent
// reference errors with some configurations
$CI->db = '';
// Load the DB class
$CI->db =& DB($params, $query_builder);
return $this;
}
}
然后在控制器中:
public function login($db_id)
{
$this->session->set_userdata('db', $db_id);
//Your stuff here
//let's suppose we have a flag for the login success
if(!$logedin)
$this->session->unset_userdata('db');
}
我什么都不懂。根据用户的不同,您将连接到不同的数据库?你有多少分贝?为什么在index.php中,您可以在配置文件中声明所有数据库,然后在模型的构造函数中选择所需的数据库。我想通过url使用不同的数据库,例如,对于此url:/user/login/56系统必须使用名为56的数据库。问题是,我需要通过url使用不同的数据库。如果用户输入此链接,例如:/user/login/34,他将使用名为34的数据库。我找不到如何解析此CodeIgniter,因为它有URI帮助程序,所以您不需要自己进行解析。CI将更加健壮。您可以像$this->uri->segment(4)这样访问代码>@MikeMiller,因为uri库尚未加载!我有超过68个数据库,我不能在一个文件中定义它们,我发现的想法是当用户没有登录时,我从$\u GET($this->uri->segment)中选择数据库,如果登录,数据库将从会话中选择variables@AhmedFaiçal,当用户访问默认_控制器
且未登录时,你把他重定向到登录页面,对吗?那么,你怎么知道要使用哪个数据库呢?为什么你不能在一个文件中定义68个数据库呢?这就是我需要的,我需要用户通过url选择数据库。一旦被选中。他可以访问该网站并登录url,网址如下“/user/login/56”56是数据库的名称。我将从url中检索并将其定义为全局变量“DB”,但问题是用户登录时。链接将被更改,我无法从url检索数据库,全局变量“DB”将被删除empty@MikeMiller我无法定义68个数据库,因为我忽略了未来我将拥有多少数据库。你把我带到这里可能有600多个,但为什么我在我的_控制器中使用它时会出错呢?我必须创建另一个控制器?你将我的_控制器放在application/core文件夹?我有一个问题,应用程序仍然使用config/database.php文件的配置关闭数据库自动加载。要检查您正在使用的数据库,只需echo$this->databaseYes我将其放在核心文件夹中,现在在我将$config=[替换为$config=array时,它不会显示错误(但现在的问题是,应用程序仍在使用config/database.phpThank you,Wait我现在将测试它并添加注释。请稍等,显示错误:消息:未定义属性:用户::$session在$CI->session->userdata('db')行中)会话库是否在自动加载中设置?是在自动加载中设置请在$CI=&get_instance();
之后添加$CI->load->library('session');