【问题标题】:Codeigniter using sessions in Database classCodeigniter 在数据库类中使用会话
【发布时间】:2016-12-16 18:46:05
【问题描述】:

我需要您在 codeigniter 项目中的帮助。我想使用会话根据配置文件夹中的数据库文件动态更改数据库。 我在配置文件中做了 $db['default']['database'] = $_SESSION['db_name'], 但不起作用。 登录页面应该检查哪些其他数据库访问进行登录。

是否可以在选择数据库之前更改连接?

登录页面

public function login(){
if(!$this->session->userdata('id_funcionario') || !$this->session->userdata('logado')){ 

    $this->db->database= "test";
    $this->session->destroy();
    $subdomain = $_SERVER['HTTP_HOST'];  
    $this->db->select("database");
    $this->db->where("subdomain", $subdomain);
    $access = $this->db->get("table")->row();
    $this->session->set_userdata("database", $access->database);
    $this->load->view('/geral/login'); 

}else{
    $url = base_url('home');
    header("Location: $url ");

}

}

登录页面有效!

AJAX 的数据发送到:

<?php if(!defined('BASEPATH')) exit('No direct script access allowed');
class Ajax extends CI_Controller {

    public function __construct(){
        parent::__construct();
        $this->load->helper('url');
        $this->load->helper('html');
        $this->load->library('session');

    }
public function toLog()
    { 
        $email = $this->input->post('email');
        $pass= md5($this->input->post('pass'));

        $this->db->database = $this->session->userdata('database');
        $this->db->select('*');
        $this->db->from('users');
        $this->db->where('usu_email',$email);
        $this->db->where('usu_pass',$pass);
        $usuario = $this->db->get()->result();

但是我不知道如何在查询时更改数据库的名称。我试过了,还是没变。

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');


/**
 * Database Driver Class
 *
 * This is the platform-independent base DB implementation class.
 * This class will not be called directly. Rather, the adapter
 * class for the specific database will extend and instantiate it.
 *
 */

    class CI_DB_driver {

    var $username;
    var $password;
    var $hostname;
    var $database;

.......

    /**
     * Constructor.  Accepts one parameter containing the database
     * connection settings.
     *
     * @param array
     */
    function __construct($params)
    {
        if (is_array($params))
        {
            foreach ($params as $key => $val)
            {
                $this->$key = $val;
            }
        }
        $CI = & get_instance();
        $this->database = $CI->session->userdata('database');

        log_message('debug', 'Database Driver Class Initialized');
    }....

你能帮帮我吗?

【问题讨论】:

    标签: php ajax codeigniter session


    【解决方案1】:

    我不确定这是否会回答您的问题,但它可能会有所帮助。

    https://www.codeigniter.com/user_guide/database/connecting.html#connecting-to-multiple-databases

    只要使用就可以随时切换数据库

    $this->db->db_select($database2_name);
    

    不过,在您的代码中,您破坏了会话,然后尝试设置会话数据变量。会话已被销毁,因此在创建新会话之前不会在任何地方设置变量。

    我建议您在配置文件中将数据库设置为组并切换组。或者,您可以按照文档中的说明一次连接到两者,并通过使用数据库对象将查询设置为来选择使用哪一个。虽然老实说,我真的认为您可能需要重新审视您的逻辑,因为在会话变量中设置数据库似乎是解决任何问题恕我直言的一种相当奇怪的方式。

    但是,如果在您的控制器中,您读取了会话变量,检查它是系统可识别的数据库,检查用户是否允许访问它,然后连接到它,应该可以正常工作。您不应尝试访问配置文件中的会话数据,因为在读取配置文件时会话数据可能不可用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-11
      • 2011-01-05
      • 1970-01-01
      • 2016-06-10
      • 2011-10-29
      • 1970-01-01
      • 2016-07-12
      相关资源
      最近更新 更多