• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to dynamically change database ip depending on the user_agent type? (e.g. connect to a replica if googlebot)


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.
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

It's in the CodeIgniter Users Guide.

CodeIgniter USers Guide - Database Connecting

after I wrote about this. I recalled what I did earlier.

basically define replica in config/database.php

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

then in CI_Model.php somewhere in __construct
if ($this->agent->is_robot())
    $this->r_db = $this->load->database('replica', TRUE);
    $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.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.