Ion Auth (Codeigniter) - How to use different database - codeigniter

Is it possible to use different database for authenticating user through Ion Auth?
I would like to create a database dedicated only for user authentication. So, it should be separated from transactional database.
How to do that?
Thank you.

In short...yes it is possible for you to use a different database for your authentication. You would need to create a second database configuration in your application/config/database.php file (similar to the below) in addition to your default config.
$db['default']['hostname'] = "localhost";
$db['default']['username'] = "root";
$db['default']['password'] = "";
$db['default']['database'] = "db_name";
$db['default']['dbdriver'] = "mysql";
$db['authentication']['hostname'] = "localhost";
$db['authentication']['username'] = "root";
$db['authentication']['password'] = "";
$db['authentication']['database'] = "auth_db_name";
$db['authentication']['dbdriver'] = "mysql";
For further reference see - http://ellislab.com/codeigniter/user-guide/database/configuration.html
You would then have to modify your ion_auth model to use this second db configuration, which you would set when loading the database.
$auth_db = $this->load->database('authentication', TRUE);
Then change all database queries in the model replacing $this->db with $auth_db.
So, $this->db->select('password, salt') would become $auth_db->select('password, salt').
For further reference see - http://ellislab.com/codeigniter/user-guide/database/connecting.html
Hope that helps!

The simplest way would be to expand on what MY_Mark said.
Create the new DB config
$db['authentication']['hostname'] = "localhost";
$db['authentication']['username'] = "root";
$db['authentication']['password'] = "";
$db['authentication']['database'] = "auth_db_name";
$db['authentication']['dbdriver'] = "mysql";
Then in the constructor of the ion_auth_model.php do this:
$this->db = $this->load->database('authentication', TRUE);
And it should just work after that.

Related

localhost/joomla = Showing only Error not Work

I am facing the issue of localhost/joomla is not showing login page, just show word Error
I have attached permission details and other corresponding files. Please help me to solve this issue.
Thankyou for your time.
Seems like the database the site it's not connected.
Check your configuration.php (in the root of the site) for this parameters:
public $dbtype = 'mysqli'; // Driver you are using to connect to the MySQL database.
public $host = 'localhost'; // The host for the database. If it's in the same server, it's ok like this.
public $user = 'root'; // Username for the database
public $password = 'root'; // Password of the database
public $db = 'NAME_OF_THE_DATABASE'; // Name of the database.

CodeIgniter Cart and Session lost when refresh page

I'm using CodeIgniter v2.1.3 and having a problem with using CI Cart and Session. When I insert an element into Cart, everything gone fine. But when I refresh the page, all saved Cart items disappeared. The same problem happened when I use Session Class.
But everything works well on my localhost. The problem just happends on my Server.
There are some websites on my Server now and they dont have any problem with Session. So I guess it must be caused by CI.
Here is Session configuarations in application/config/config.php :
$config['sess_cookie_name'] = 'blowup_session';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = FALSE;
$config['sess_table_name'] = 'ci_sessions1';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 300;
I tried to print the session_id but it returned nothing. So I guess the Session class did not generate any session_id. Try to start the session manually by using session_start(), the session_id was generated.
I also tried to save something by using $_SESSION, and they are saved without any problem.
Does it mean that the CI_Session and Cart library were not auto loaded?
How could I fix it? Or is there any Session class could replace the current one?
PS: My Server is running CentOS 5, PHP v 5.2.17 , Apache 2.2.23 and MySQL 5.0.96
UPDATED
Below is the function I use in Controller to add an item into Cart. The data ($params) is posted via an AJAX request (using jquery AJAX). The returned data is a HTML view.
public function add_to_cart(){
$this->layout->set_template('ajax');
if ($this->is_post()){
$params = $this->get_all_post_data();
//Debug::dump($this->cart);die;
if (isset($params['id']) && (int)$params['id']>0){
$product = $this->_product_model->get_record_by_id((int)$params['id']);
if (!is_null($product)){
if (count($this->cart->contents())>0){
foreach ($this->cart->contents() as $item){
if ($item['id']==$product->id){
$data = array('rowid'=>$item['rowid'],'qty'=>++$item['qty']);
$this->cart->update($data);
}else{
$data = array('id'=>$product->id,'qty'=>1,'price'=>$product->price,'name'=>$product->id,'options'=>array('image'=>$product->thumb,'product_name'=>$product->title));
$this->cart->insert($data);
}
}
}else{
$data = array('id'=>$product->id,'qty'=>1,'price'=>$product->price,'name'=>$product->id,'options'=>array('image'=>$product->thumb,'product_name'=>$product->title));
$this->cart->insert($data);
}
$this->session->set_userdata(array('test'=>'Session test'));
$this->layout->load('cart/topmenu_cart', $this->data);
}
}
}
}
Have you tried to use database to store session data instead? Based on your settings, I guess because your data is to large to hold in a 4KB cookie.

How we can give permission to a user to create database in phpmyadmin(cPanel)?

I have a database and a user which is created through cpanel(i have added that user to that database and i am able to access that database using that user and its password. i have given all privileges to that user through cPanel(by clicking on the username in 'current database' table in cpanel and clicking on all privileges check box ).
In my application(which is created using PHP- Codeigniter framework) i want to create new database and tables in that database as per users requirement. For that i am using Codeigniter dbforge class for creating database and the tables in that.
$this->load->dbforge();
if ($this->dbforge->create_database($database_name)){
$config['hostname'] = "localhost";
$config['username'] = "user_name";
$config['password'] = "users_password";
$config['database'] = $database_name;
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = TRUE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
$config['swap_pre'] = '';
$config['autoinit'] = TRUE;
$config['stricton'] = FALSE;
$db_new=$this->load->database($config,TRUE);
$this->db=$db_new;
$this->load->dbforge();
$fields=array(
'id'=>array('type'=>'INT','auto_increment'=>TRUE,'constraint'=>'5'),
'first_name'=>array('type'=>'VARCHAR','constraint'=>'60'),
'last_name'=>array('type'=>'VARCHAR','constraint'=>'60','null' => TRUE),
);
$this->dbforge->add_field($fields);
$this->dbforge->add_key('id',TRUE);
$this->dbforge->create_table('clients');
}
In the above code i am creating a database first and then loading that database as the current database . This code works perfectly in local host WAMP server but when i uploaded it to the server and then tried to create the database it gives the following Error
Error No 1044: Access denied for user 'username'#'localhost' to database 'database_name'
The line of code which caused thr error is
if ($this->dbforge->create_database($database_name)){
*In that database config setting shown in the above code the 'username' and 'password' is given as the username and password of the already existing database(which is created through cPanel) . Is that the problem...?* .
the current user using which we are trying to create new database is having all the avilable privileges. but in that 'CREATE DATABASE ' privilege is not there.
For database naming I have used the ' prefix_ ' in the database name(CPanel database naming format)
How can i create the database using dbforge class and access to that database using a existing user ...?
Try Create a Super User first
mysql > GRANT ALL PRIVILEGES on databasename.* TO "username"#"localhost" IDENTIFIED BY "password";

Codeigniter Database - Automatic Selection

I have a multi-tenancy application that I am developing and I'm nearly there.
Currently I am using one database per customer - copying it each time and naming depending based on $_SERVER['http_host']...
A lot of the tables each database never change, and contain the same information.
I would like to have a main application database that contains this information, including a list of clients and what there database is called. This database would then be used to select the correct customers database...
Is this possible and can anyone point me in the direction of a suitable tutorial?
Thanks
OK, so you'll need different database configs for your "administrative" database and your "customer" databases.
Use the examples from this page to generate those separate configs:
http://ellislab.com/codeigniter/user_guide/database/configuration.html
Then, in your main controller (please tell me your extending a MY_Controller.php and not just using CI_Controller.php), you'll want to read your 'admin' database and get all client information. You'll then want to generate a $config array for this client database and reload the database with the new $config array:
// in MY_Controller constructor
// do your thing here to connect to the admin database and get client details
// ...
//
$config['hostname'] = "localhost";
$config['username'] = $client_db_username; // you got this from the 'admin' database
$config['password'] = $client_db_password; // you got this from the 'admin' database
$config['database'] = $client_db_name; // you got this from the 'admin' database
$config['dbdriver'] = "mysql";
$config['dbprefix'] = "";
$config['pconnect'] = FALSE;
$config['db_debug'] = TRUE;
$config['cache_on'] = FALSE;
$config['cachedir'] = "";
$config['char_set'] = "utf8";
$config['dbcollat'] = "utf8_general_ci";
$this->load->database($config);
You'll then have a connection to the client database.
Note
If you'll need to connect to both databases back and forth, then you can connect to multiple databases as explained on this page: http://ellislab.com/codeigniter/user_guide/database/connecting.html

CodeIgniter and autoloading the database library

I've got an issue connecting to the database with CodeIgniter.
In the model, if I do
$this->load->database();
then a query such as
$query = $this->db->get('articles', 10);
works and returns data. However, when I remove the load->database line and try autoloading the database library, using
$autoload['libraries'] = array('database');
in application/config/autoload.php, the above query fails. As it works when I explicitly load the library, it's not a problem with my database connection. I'm using CodeIgniter 2.0.2 (the current latest release).
As most of my pages use the database I'd like to autoload it to avoid having to load it anually in each model. Have I misunderstood the documentation regarding loading the database library or am I doing something wrong?
I know I am probably just answering a dead question but i had the same problem.
if you are using eclipse pdt or similar IDE and modified the system>core>model.php by adding some variables just before the constructor to get code completion then the autoload doesnt work
i dont know why but the fact is it doesnt work. I restored the core>model.php to original and autoload works fine. I dont get autoload now and its really a bad experience for new coder to CI.
If you have a workaround please comment so.
This is just an alternative if you still can't resolve the issue. Just extend the CI_Model and auto load the database library in the constructor. Then you can just make all other models inherit from that base class.
Example:
<?php
class My_Model extends CI_Model{
public function __construct(){
parent::__construct();
$this->load->database();
}
}
And the all others will start like :
<?php
class Some_Model extends MY_Model{}
This method has the other benefit that you don't have to include the loading code line in every model you create. Also you can easily push shared functionalities among models into the parent MY_Model class.
Hope it helps!
This is my database.php from my application/config/ directory
$active_group = 'default';
$active_record = TRUE;
$db['default']['hostname'] = 'mydbhost';
$db['default']['username'] = 'myusername';
$db['default']['password'] = 'mypassword';
$db['default']['database'] = 'mydatabase';
$db['default']['dbdriver'] = 'mysql';
$db['default']['dbprefix'] = '';
$db['default']['pconnect'] = TRUE;
$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'] = FALSE;
Check yours looks like this and rerun through the user guide at http://codeigniter.com/user_guide/database/configuration.html and reread the comments in the autoload.php file in the config directory.
The relevant line in mine reads:
$autoload['libraries'] = array('database', 'form_validation', 'session');
You should be loading the db library like this:
$this->load->library('database');
The only thing I can think of is in your application/config/database.php file, the following line is set to false, instead of true.
//['autoinit'] Whether or not to automatically initialize the database.
$db['default']['autoinit'] = TRUE;
Everything else looks ok.
Shababhsiduque,
Just you we're right, at least in my case!
The workaround to getting the ide to work is creating the reference variables in ANOTHER CI project.
That is leave the system model as it is in the current project but modify it in another project.
Then change the php buildpath (project->properties->phpbuildpath) to the project with the variables.
Note this are settings for Aptana Studio 3.
Worked for me.
You could try PHP Autoload Class Magic function in this case.
function __autoload($class)
{
if(strpos($class, 'CI_') !== 0)
{
#include_once( APPPATH . 'libraries/'. $class . '.php' );
}
}
This code must copied inside the config.php (after the last line) within Application folder.
This code will automatically load libraries for you when its needed.
It might work. Happy coding.
In my case the problem was because there was another occurrence of $autoload['libraries'] inside the same file and it reset the array.

Resources