have a codeigniter site config using multiple databases? - php

Not sure if this is the right way to do things, but was wondering if anyone had some ideas or advice on how to achieve this.
I want to create a subdomain called demo.mysite.com
and i want this to load my codeigniter application which is in public_html, however if the sub domain is demo. then it loads another database instead of the main one
The following is from config/database.php
$db['default']['username'] = "main";
$db['default']['password'] = "password!";
$db['default']['database'] = "mainsite";
The reason I want to do this is i still make changes to my site and dont want to have to keep copying my entire site into a demo folder everytime i make a change.
Hope this makes sense

Create two subdirectories in config dir
aplication
config
site1
site2
Move diiferent files of config dir to that subdirs
In index.php set ENVIRONMENT constant to site1 for one site and site2 for another. To set correct error reporting, add site1 and site2 into switch operator below with a desired state of reporting
define('ENVIRONMENT', 'site1');
switch (ENVIRONMENT)
{
case 'development':
case 'site1':
...
CI get config files from subdir named as ENVIRONMENT

In config database.php you may create different db groups, the one that already exists is the default group:
$db['default'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => '',
...
You can create another group with any name:
$db['another_group'] = array(
'dsn' => '',
'hostname' => 'localhost',
'username' => '',
'password' => '',
'database' => '',
...
You can then connect to another database by using:
$this->load->database('another_group', TRUE);
You will find more details in the docs here

Related

Codeigniter: using a database to store database names for multiple users

Using Codeigniter 3.0.6
Until now I have been using the standard setup for Codeigniter. I have a database and all connects just fine. The question that I'm having trouble with is that I want to add a 'global_accounts' database, from which, depending on the user currently logged in will choose the database to load.
Global_Accounts
--------------------------------------------------------
user | coolUser | user2
pass | coolPassword | passy
url | coolestsite.mysite.com | u2.mysite.com
database_name | coolestsite_application_db | u2_application_db
The application data/schema doesn't really matter as far as I'm concerned. What I want (and am going to set up, but outside of the scope for this question) is for when a user registers, they will fill out user, pass and url, and db_name will be automatically created. The script will then create a new database with that name, creating a blank copy of the application db for the user.
So for now, we're hard coding that, so we can assume that it's working perfectly.
What I want is inside Codeigniter's database.php file:
$db['default'] = array(
'dsn' => '',
'hostname' => 'mysite.com',
'username' => 'root',
'password' => 'pass',
'database' => 'coolsite_application_db'
...etc
);
Instead of hard coding this file, I want to be able to do something like this:
$db['global'] = array(
'dsn' => '',
'hostname' => 'mysite.com',
'username' => 'root',
'password' => 'pass',
'database' => 'global_accounts'
...
);
$db['default'] = array(
'dsn' => '',
'hostname' => 'mysite.com',
'username' => 'root',
'password' => 'pass',
'database' => $this->db['global']->get_db_name($loggedInUserId);
...etc
);
Hopefully that makes sense - I need to get the db name based on the logged in user. Obviously I need to build either a Model or just a function to grab it, but the question is where? Do I add a model, connect to the global db, and then load the model in the database config? Is there some other, easier, more productive way to do this, or is that it? Will the codeigniter core files be able to be called from inside the codeigniter config files?
Use 2 database connections. 1 for the global user accounts and 1 for the actual user database. Then create a helper function to grab the user databases. Just an idea.

CakePHP merge multi apps,one database and multi domain to access

The Situation
Currently, there are two separate cakePHP projects(apps) where implementation is done and running smoothly of course with different database.
Now, the requirement is to merge these two separate cakePHP apps into one with same database and to run on different domains.
The Problem
1)There exists two different login functions which should be one after merging.Like this there may be some other huge code should be written common.
So, please provide solution or direct me to right way how can one manage common code for multiple apps with different domain
e.g., https://www.sample-1.com/login
and https://www.sample-2.com/login
which is using same database to validate users.
2) How to distinguish routes?
3) How to manage such projects which will grow later?
For both domian you may call common database file and common app folder.
Then you can switch database depends upon request action
Try this
if (isset($_SERVER) && isset($_SERVER['SERVER_NAME'])) {
if (strpos($_SERVER['SERVER_NAME'], 'localhost') === false) {
$config = ConnectionManager::getDataSource('defaultCompany')->config;
}
}
inside database.php file
var $defaultCompany = array(
'driver' => 'mysql',
'persistent' => false,
'encoding' => 'utf8',
'host' => 'localhost',
'login' => '',
'password' => '',
'database' => '',
'prefix' => '',
'port' => '',
);
Or if you want common file in both domain. Create plugins outside app folder directory and put inside it.

How to set an environment variable in Ubuntu and Windows and store it securely?

I am working on an application based on laravel. I need to set my database password in the config so that laravel can use it to connect to the database.
Now I have two problems:
1) I don't want to set password for every other local environment I use.
2) I don't want to set the password in the config file because when I share the code with my team members on git, they can view my password.
I may have figured out a way around this which is to store the password in an environment variable and then use the getenv() PHP function to retrieve it in the config file. So now I can just set the environment variable in any number of environments and the code in the config file will remain the same, and also the other members won't be able to see my password.
Now I don't know how to set environment variables, and other stuff like global/local environment variables and temporary/persistent variables.
And if I am storing the password in the environment variables, should I hash these passwords? Will they be accessible to other users who log onto my machine?
And if I do hash them and store them, how am i supposed to use them in the config file?
Is there a different, a more ideal way around my problem?
Laravel Way
Laravel uses .env.php files to solve this for you: http://laravel.com/docs/configuration#protecting-sensitive-configuration. But as you still need to set the environment in your start.php file, I prefer to do it differenlty:
My Way
I create a .environment file in my app root folder with things like:
<?php
return [
'LARAVEL_ENV' => 'development',
'DOMAIN' => 'myapp.com',
'DEBUG_MODE' => true,
'MAIN.DATABASE_HOST' => 'localhost',
'MAIN.DATABASE_NAME' => 'databasename',
'MAIN.DATABASE_USER' => 'myusername',
'MAIN.DATABASE_PASSWORD' => 'basswort',
];
I have a class to load the environment file:
<?php
namespace PragmaRX\Support;
use Exception;
class Environment {
protected static $loaded = false;
public static function load($file = null)
{
if ( ! static::$loaded)
{
if ( ! file_exists($file))
{
throw new Exception('Environment file (.environment) was not set or does not exists: '.$file);
}
foreach(require $file as $key => $value)
{
if ($value === false)
{
$value = '(false)';
}
else
if ($value === null)
{
$value = '(null)';
}
else
if (empty($value))
{
$value = '(empty)';
}
putenv(sprintf('%s=%s', $key, $value));
}
static::$loaded = true;
}
}
public static function getDetectionClosure($file = null)
{
static::load($file);
return function() { return getenv('LARAVEL_ENV'); };
}
}
Then in my app/bootstrap/start.php I just need to load it this way:
$env = $app->detectEnvironment(
\App\Environment::getDetectionClosure(__DIR__.'/../.environment')
);
As you can see, the closure will return the current LARAVEL_ENV, stored in my .environment file. But it also will load all keys to the PHP environment, so, now, in my application I just need to
<?php
return [
'fetch' => PDO::FETCH_CLASS,
'default' => 'main',
'connections' => [
'main' => [
'driver' => 'pgsql',
'host' => getenv('MAIN.DATABASE_HOST'),
'database' => getenv('MAIN.DATABASE_NAME'),
'username' => getenv('MAIN.DATABASE_USER'),
'password' => getenv('MAIN.DATABASE_PASSWORD'),
'charset' => 'utf8',
'prefix' => '',
'schema' => 'public',
],
],
];
Add the .environment file to your gitignore file and you should be safe, but you'll have to, of course, create (or copy and edit) the file every time you set a new server.
About Security
Everything in your system is a file, you are protected if your files are protected. It's that simple. A VirtualHost file is a file accessible by your webserver, if someone hacks your webserver you'll have not only your .environment file exposed, but also your VirtualHost one, so IMO you're not securer using one or another.
I'm not an expert on security by any means but I think you should avoid trying to do what you want to do and just define your environments. If you really want to set an environmental variable this is how you do it in Ubuntu:
In your vhost:
<VirtualHost *:80>
SetEnv APP_ENV "test"
...
</VirtualHost>
I assume Windows is your client machine?
There are many good answers already, but as a side note. You can include your DB config file in .gitignore so it won't be shared among the development team by git. Something like config.php.orig in the project files and your own copy config.php listed in .gitignore should do the trick.

Problems loading Zends Session in bootstrap

I am having trouble getting Zend to store my session in MySQL table. I have followed the Zend Framework guide step by step, and am not sure if is where am putting the code in my bootstrap file, but once the code snippet is in place and I load my site Apache just crashes. Literally crashes. My logs don't say anyhing.
Here is my code:
$db = Zend_Db::factory( 'Pdo_Mysql', array(
'host' => 'localhost',
'username' => 'root',
'password' => '*****',
'dbname' => 'drecords'
));
Zend_Db_Table_Abstract::setDefaultAdapter( $db );
$config = array(
'name' => 'sessions',
'primary' => 'id',
'modifiedColumn' => 'modified',
'dataColumn' => 'data',
'lifetimeColumn' => 'lifetime'
);
Zend_Session::setSaveHandler( new Zend_Session_SaveHandler_DbTable( $config ) );
//start your session!
Zend_Session::start();
I am running this code right after at the end of my Bootstrap file.
My question is what am I doing wrong if am following Zends documentation? Is there something I need to know like an extra configuration option in my MySQL or PHP.ini that am not aware of?
Did you create the table in MySQL?
your user have insert/update/delete privileges on the table
do your php setups output errors, what environment are your running production/development
I think the code should probably output some error but if your disabled the output of those you cannot see them.

cakephp : separate database access for admin users

I have been using cakephp for creating web application. In my current project there are two database users one for admin another for site users, how can I configure cakephp so that the admin can login to the site with more database operations power ?
Thank you
I agree. Sometimes, it's better having permissions handled in your application layer than the database layer. However, if you really, really want to have that extra layer of security in your database as well, then you should set up multiple database connections:
var $default = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'normaluser',
'password' => '',
'database' => 'db',
'prefix' => '',
);
var $admin = array(
'driver' => 'mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'adminuser',
'password' => '',
'database' => 'db',
'prefix' => '',
);
You can then use $this->ModelName->setDataSource('admin') if the user is in the admin section, or whatever condition that you might impose.
I would suggest that you look at the admin_ prefix routing. CakePHP lets you handle admin powers quite easily. Prefix Routing Additionally, you can add a field in your users table to indicate the role of the user, and check that against the current prefix.
the most robust solution will likely be setting up Access Control Lists(ACLs). This will allow you to delegate permissions based on a user role that you designate.
For example admin has a group_id of 1, and users have a group_id of 2. Then you can allow admins to have access to certain operations within your web app.
Here's the cake documentation on this feature.
http://book.cakephp.org/view/1242/Access-Control-Lists

Categories