You are here

function regcode_generate_code in Registration codes 6

Generate a code

1 call to regcode_generate_code()
regcode_admin_create_submit in ./regcode.admin.php
Submit handler for regcode_admin_create

File

./regcode.api.php, line 206
A generic set of functions for interacting with and creating regcodes

Code

function regcode_generate_code($length, $output, $case) {
  static $seeded = FALSE;

  // Possible seeds
  $outputs['alpha'] = 'abcdefghijklmnopqrstuvwqyz';
  $outputs['numeric'] = '0123456789';
  $outputs['alphanum'] = 'abcdefghijklmnopqrstuvwqyz0123456789';
  $outputs['hexadec'] = '0123456789abcdef';

  // Choose seed
  if (isset($outputs[$output])) {
    $output = $outputs[$output];
  }

  // Seed generator (only do this once per invocation)
  if (!$seeded) {
    list($usec, $sec) = explode(' ', microtime());
    $seed = (double) $sec + (double) $usec * 100000;
    mt_srand($seed);
    $seeded = TRUE;
  }

  // Generate
  $str = '';
  $output_count = strlen($output);
  for ($i = 0; $length > $i; $i++) {
    $str .= $output[mt_rand(0, $output_count - 1)];
  }
  if ($case) {
    $str = strtoupper($str);
  }
  return $str;
}