Welcome Guest, Not a member yet? Register   Sign In
how to dynamically change database ip depending on the user_agent type? (e.g. connect to a replica if googlebot)
#1

[eluser]Unknown[/eluser]
Hello,

I googled for this but couldn't find it.

What I want to achieve is the following.

Say I have 2 database servers. (say, A & A' - which is the replica of A) I want to direct to A' if the user_agents are bots (googlebot, bingbot etc)

Can I achieve this in codeigniter by changing the config/database.php file? or do I have to change config item values in model (e.g. in __construct) using config library?

I tried something like the following, but of course I can't use $this 'cos it's a config file.
Code:
if ($this->agent->is_robot())
{
            $db['live']['hostname'] = 'XXX.XXX.XXX.XXX'// database A';            
} else
{
            $db['live']['hostname'] = 'localhost'; // database A
}

I know this sounds silly, 'cos I'm trying to use a library in config file... (guess no library is loaded in this stage)

Any advice is appreciated & thanks in advance! Big Grin
#2

[eluser]InsiteFX[/eluser]
It's in the CodeIgniter Users Guide.

CodeIgniter USers Guide - Database Connecting
#3

[eluser]Unknown[/eluser]
after I wrote about this. I recalled what I did earlier.

basically define replica in config/database.php

Code:
$db['main']['hostname'] = 'XXX.XXX.XXX.XXX';
$db['replica']['hostname'] = 'XXX.XXX.XXX.XXX';

then in CI_Model.php somewhere in __construct
Code:
if ($this->agent->is_robot())
{
    $this->r_db = $this->load->database('replica', TRUE);
}else
{
    $this->r_db = $this->load->database('main', TRUE);
}

Then I don't need to change model code if written like '$this->r_db->query' etc.

This would work nicely I think.




Theme © iAndrew 2016 - Forum software by © MyBB