Jump to content

Simple 3 code modification needed (random to sequential)


djpurpose

Recommended Posts

Below is some code I use for an osCommerce from the "configure.php" file store to rotate the database user so I don't exceed my very low "max_questions" limit. Right now it's using random to cycle between what database user is used to connect. I want to change it from being random to being sequential since the random causes un-even distribution of what user gets used.

 

$database_user_array[] = "user_01";
$database_user_array[] = "user_02";
$database_user_array[] = "user_03";
$database_user_array[] = "user_04";
$database_user = $database_user_array[ rand( 0, ( sizeof($database_user_array) -1 ) ) ];

 

Go easy on me as I'm a newbie. I'm just not sure how this thing works. If an increment variable is added will the value be kept track of? I'm assuming I need something like this sudo code (be gentle).

 

$N=number of database users
if $user_number variable does not exist in database then create it and initialize to 0
database user = the text "user" + $user_number (with leading 0 if less than 10)
if variable is less than $N then $user_number++
else $user_number = 0

 

 

Link to comment
Share on other sites

//select all of the users in the database
$query = "SELECT * FROM users";

//query the database
$result = mysql_query($query);

//
$fetcharray = mysql_fetch_array($result);

//get the number of users in the database
$N = mysql_num_rows($result);

$userarray[N] = new array(N); //dont know arrays in PHP, but make an N element array

for(int i = 0;i<$N;i++)
{
     userarray[i] = fetcharray[i];
}

 

Sorry i havent looked at PHP in a while, but something like this should work to load an array with the users, choose from there

I am beat for time right now but i'll be on in a little bit to finish this.

Link to comment
Share on other sites

I guess I should have explained this a little bit better. The "user" is the database user that is used to gain access to the database. So every time a connection needs to be made to the database I want to use a different user.

The only thing that needs to be in the database is the increment variable, all the rest of the stuff can happen the the php code.

 

I'll try to comment my sudocode so it's easier to understand and rename my variables so they are easier to read. Please excuse my sudocode. I'm a c++ coder in case the code looks weird....

 

 

/*
Variable descriptions
$number_of_users //keeps hold the total number of user names I'm going to create, Lets make it 50 for example's sake
$current_user_number//increment variable used to get the last part of the user name (ie. to get  the text "user_01" I would concatenate "user_" with $current_user_number (formated with a leading 0 for numbers less than 10 ie: 01, 02...10)
*/

//Variable declaration
static int $number_of_users=50;   //total number of users (hard coded)
string $database_user;   //used to hold the text of the user name (ie: "user_01")
if $current_user_number does not exist in database then create it and initialize to 0   //will happen only once

$database_user = the text "user" + $user_number   //concatenated
if ($current_user_number < $number_of_users)    //if current user is less than 50
   $current_user_number++;   //increment to the next user for next time
else 
   $user_number = 0;   //Once we hit the 50th user we start back at 0

Link to comment
Share on other sites

I'm surprised I haven't gotten a response. In c++ I know this would take less than 10 lines of code. At the very least how would I concatenate a string with a int formated to 2 digits.

 

in ANSI C it would look like this:

char s[6];                                     //create an empty string
int current_user_number = 3                    //create int and initialize it to 3 for illustration
sprintf(s,"user_%02d",current_user_number);    //add the text "user_" and the int with a leading zero if it's less than 10

 

After the above code s = "user_03"

Link to comment
Share on other sites

The problem is that a web app is stateless. Your values aren't going to persist between requests.

 

A basic solution would be to

 

1. set the current user number.

1. serialize() the current_user_number.

2. on the next request, unserialize() the current_user_number.

3. ++current_user_number

4. return to 1

 

You will of course want to check that incrementing the current_user_number doesn't take you out of the bounds of your array. If it does, reset it 0.

 

Serialize

Link to comment
Share on other sites

I also wrote this which is a bit different to what I described above but works out the same.

<?php
$usr[] = "bob";
$usr[] = "john";
$usr[] = "lisa";
$usr[] = "roy";
$usr[] = "alice";
$usr[] = "sarah";
$usr[] = "nick";
$usr[] = "berry";
$usr[] = "chuck";
$usr[] = "charlie";
$usr[] = "the very last user";

$thisDir = dirname(__FILE__);
define("DB_USR_TRACK_DIR","$thisDir".DIRECTORY_SEPARATOR.'counter'.DIRECTORY_SEPARATOR); //save path for 'tracking' file

$size    = count($usr);
$matches = glob(DB_USR_TRACK_DIR."[0-9]*"); //look for files with a numeric name

$new = 0; 
if (empty($matches)) {
    
    touch(DB_USR_TRACK_DIR.$new); //create file
    
} else {
    
    $current = basename($matches[0]); //get current index
    $new     = $current + 1;//grab next index
    
    if ($new == $size) { //bounds check
       $new = 0; //restart
    }
    
    rename(DB_USR_TRACK_DIR.$current,DB_USR_TRACK_DIR.$new); //rename current to new
}
echo $usr[$new];
?>

Link to comment
Share on other sites

Thinking about it, serializing it is overkill for such a simple value, its really more useful for objects and arrays.

Here's a really simple example you could expand on...

$var = 0;
//save value in file
file_put_contents("myvar",$var);//this is a php5 function
$newvar = file_get_contents("myvar"); //read value out of file
$newvar++;
echo "$newvar"; //outputs 1

 

Link to comment
Share on other sites

Ok I think I'm really close  ;D. The code should look something like this but this doesn't work. I haven't gotten the file stuff to work at all and I'm not sure why. Check the cose out and let me know what's wrong. The concatenation is the only thing I can confirm as working.

 

/*******************************************************************
Cycles through user names "user_1" through "user_50" then resets and starts over. The
current number is stored in a text file.
*******************************************************************/

  $user_file_name = 'current_user.txt'; 				//File name
  if(($current_user_file = fopen($user_file_name, 'r')) ===FLASE){    	//Open file to read the current user number
      die('Failed to open file for reading!');}                              
  $current_user_number = fgets($current_user_file, 2);               	//reads the user number from the file
  $database_user_string= "user_" . $current_user_number;    		//add the text "user_" and number (ie "user_3)
  fclose($current_user_file);						//close the file
  if(($current_user_file = fopen($user_file_name, 'w')) ===FLASE){    	//Open file to write the current user number
        die('Failed to open file for writing!');}
  $current_user_number++;  						//incement $current_user_number by 1
  if($current_user_number > 49)
$current_user_number = 0;					//reset $current_user_number if it gets to 50
  else
  	fwrite($current_user_file, $current_user_number);		//Write the over the new $current_user_number
  fclose($current_user_file);						//close the file

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.