Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 Programming Discussions
  #1  
Old 11-29-2007, 03:46 AM
Mythotical Mythotical is offline
 
Join Date: Jun 2004
Location: Booneville, AR, USA
Posts: 1,428
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default Generate random key

I haven't figured out yet how to generate a random key that consists of letters and numbers. I managed to get it to generate numbers but for the life of me I can't seem to figure out how to generate a random alphanumeric key.

Here is what I am using for numeric only:
PHP Code:
mt_rand(); 
I know, thats simple, but I am using it temporarily till I figure out how to generate a key that consists of alphanumeric and is length is 16, no matter how many key's it generates.

Any help greatly appreciated

EDIT: Also, I have read quite a few articles on php.net but alas have come up empty handed.

Thanks in advance
Steve M
Reply With Quote
  #2  
Old 11-29-2007, 03:53 AM
MoT3rror MoT3rror is offline
 
Join Date: Mar 2007
Posts: 423
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

This page should help you.
http://www.i-fubar.com/random-string-generator.php
Reply With Quote
  #3  
Old 11-29-2007, 12:39 PM
Hossie Hossie is offline
 
Join Date: Jun 2005
Posts: 8
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Is the link a joke? 2 lines are enough:

PHP Code:
<?php
function randomstr($length){
  for(
$i=0$i<$length$i++)$str[$i]=chr(rand(97122));
  return 
implode(""$str);
}
echo 
randomstr(16);
?>
Reply With Quote
  #4  
Old 11-29-2007, 12:58 PM
Mythotical Mythotical is offline
 
Join Date: Jun 2004
Location: Booneville, AR, USA
Posts: 1,428
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Thanks MoT3rror and Hossie.

Hossie, what you gave me worked but it gave me a 16 letter string. I need it to be a mix of letters and numbers.
Reply With Quote
  #5  
Old 11-29-2007, 01:08 PM
Hossie Hossie is offline
 
Join Date: Jun 2005
Posts: 8
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

So why don't you just use some hash algorithm?

PHP Code:
<?php
function randomstr($length){
  return 
substr(md5(time()*rand(1100)), 0$length);
}
echo 
randomstr(16);
?>
Reply With Quote
  #6  
Old 11-29-2007, 01:12 PM
Mythotical Mythotical is offline
 
Join Date: Jun 2004
Location: Booneville, AR, USA
Posts: 1,428
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

Perfect, thats what I was trying to figure out last night but couldn't. You just saved me a headache Hossie. Thanks.

That link above though seemed good but alot of lines of code. Glad you made a post.
Reply With Quote
  #7  
Old 11-29-2007, 07:20 PM
MoT3rror MoT3rror is offline
 
Join Date: Mar 2007
Posts: 423
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default

You must not have read the first part of the page.

PHP Code:
function assign_rand_value($num)
{
// accepts 1 - 36
  
switch($num)
  {
    case 
"1":
     
$rand_value "a";
    break;
    case 
"2":
     
$rand_value "b";
    break;
    case 
"3":
     
$rand_value "c";
    break;
    case 
"4":
     
$rand_value "d";
    break;
    case 
"5":
     
$rand_value "e";
    break;
    case 
"6":
     
$rand_value "f";
    break;
    case 
"7":
     
$rand_value "g";
    break;
    case 
"8":
     
$rand_value "h";
    break;
    case 
"9":
     
$rand_value "i";
    break;
    case 
"10":
     
$rand_value "j";
    break;
    case 
"11":
     
$rand_value "k";
    break;
    case 
"12":
     
$rand_value "l";
    break;
    case 
"13":
     
$rand_value "m";
    break;
    case 
"14":
     
$rand_value "n";
    break;
    case 
"15":
     
$rand_value "o";
    break;
    case 
"16":
     
$rand_value "p";
    break;
    case 
"17":
     
$rand_value "q";
    break;
    case 
"18":
     
$rand_value "r";
    break;
    case 
"19":
     
$rand_value "s";
    break;
    case 
"20":
     
$rand_value "t";
    break;
    case 
"21":
     
$rand_value "u";
    break;
    case 
"22":
     
$rand_value "v";
    break;
    case 
"23":
     
$rand_value "w";
    break;
    case 
"24":
     
$rand_value "x";
    break;
    case 
"25":
     
$rand_value "y";
    break;
    case 
"26":
     
$rand_value "z";
    break;
    case 
"27":
     
$rand_value "0";
    break;
    case 
"28":
     
$rand_value "1";
    break;
    case 
"29":
     
$rand_value "2";
    break;
    case 
"30":
     
$rand_value "3";
    break;
    case 
"31":
     
$rand_value "4";
    break;
    case 
"32":
     
$rand_value "5";
    break;
    case 
"33":
     
$rand_value "6";
    break;
    case 
"34":
     
$rand_value "7";
    break;
    case 
"35":
     
$rand_value "8";
    break;
    case 
"36":
     
$rand_value "9";
    break;
  }
return 
$rand_value;

This function defines what the random characters going to be.

The second funtion.
PHP Code:
function get_rand_id($length)
{
  if(
$length>0
  { 
  
$rand_id="";
   for(
$i=1$i<=$length$i++)
   {
   
mt_srand((double)microtime() * 1000000);
   
$num mt_rand(1,36);
   
$rand_id .= assign_rand_value($num);
   }
  }
return 
$rand_id;

This function uses the characters and numbers to output a random string on letters and numbers.

Call on this to come with 16.
PHP Code:
get_rand_id(16); 
Reply With Quote
  #8  
Old 11-29-2007, 07:27 PM
Guest190829
Guest
 
Posts: n/a
Default

Using one of the hashing algorithms would be the most efficient solution - that is what hashing is for.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 08:15 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.08401 seconds
  • Memory Usage 2,255KB
  • Queries Executed 11 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (6)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (8)post_thanks_box
  • (8)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (8)post_thanks_postbit_info
  • (8)postbit
  • (7)postbit_onlinestatus
  • (8)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete