My host has a limit of 50K MySQL connections per hour. I am told that I can get around this by creating additional users and switching among them on a quasi-random basis. My script currently identifies the user and password like this: $db_user = 'username'; $db_passwd = 'password'; Code (markup): I want to change this to something like: [Code{if less than some random number} then $db_user = 'username'; $db_passwd = 'password'; else {if greater than number 1 and less than some other random number} $db_user = 'username2'; $db_passwd = 'password2'; else {if greater than number 2 and less than some other random number} $db_user = 'username3'; $db_passwd = 'password3'; ... and etc. [/code] How do I do this?
UPDATED <?php [COLOR="Red"][B]UPDATED[/B][/COLOR] $accounts = array ( '1' => array('username' => 'hello_1', 'password' => 'bye_1'), '2' => array('username' => 'hello_2', 'password' => 'bye_2'), '3' => array('username' => 'hello_3', 'password' => 'bye_3') ); $account = rand(1, count($accounts)); $username = $accounts[$account]['username']; // hello_1/2/3 $password = $accounts[$account]['password']; // bye_1/2/3 ?> PHP: And in return I would like you to give me a few points to increase my rep.
You could do something like this which would use a different user for each third of an hour. (I like being methodical while rotating things like this to make it easier to troubleshoot and verify that all username/passwords are working as expected.) You could easily modify this if you needed to add more users to split between as well by going in 10 minute increments, etc. $current_minute = date("i"); if ($current_minute < 20) { // minutes from 00-19 $db_user = 'username'; $db_passwd = 'password'; } elseif ($current_minute >=20 && $current_minute < 40) { // minutes 20-39 $db_user = 'username2'; $db_passwd = 'password2'; } else { / all other minutes in the hour $db_user = 'username3'; $db_passwd = 'password3'; } PHP:
Got it! Thanks, webviz and green rep coming your way in return. Added: Saw your solution afterwards, tflight. Thanks.
What webviz did was to put all of the three username/password combinations into one array, then used the array_rand function to randomly pull one of the combinations out of the array. I was going to suggest something similar at first (it is more elegant), however if something were to go wrong with one of the accounts it will be easier to troubleshoot if you know which username/password should be in effect (based on the time) rather than pulling one purely at random.