1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP coding question

Discussion in 'PHP' started by minstrel, Aug 9, 2006.

  1. #1
    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?
     
    minstrel, Aug 9, 2006 IP
  2. webviz

    webviz Peon

    Messages:
    216
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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. ;)
     
    webviz, Aug 9, 2006 IP
    minstrel and vgpowered like this.
  3. tflight

    tflight Peon

    Messages:
    617
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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:
     
    tflight, Aug 9, 2006 IP
    minstrel likes this.
  4. minstrel

    minstrel Illustrious Member

    Messages:
    15,082
    Likes Received:
    1,243
    Best Answers:
    0
    Trophy Points:
    480
    #4
    How does that switch from one "user" to another?
     
    minstrel, Aug 9, 2006 IP
  5. minstrel

    minstrel Illustrious Member

    Messages:
    15,082
    Likes Received:
    1,243
    Best Answers:
    0
    Trophy Points:
    480
    #5
    Got it! Thanks, webviz and green rep coming your way in return. :)


    Added: Saw your solution afterwards, tflight. Thanks. :)
     
    minstrel, Aug 9, 2006 IP
  6. tflight

    tflight Peon

    Messages:
    617
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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.
     
    tflight, Aug 9, 2006 IP