Created
July 20, 2017 14:56
-
-
Save psywhale/5bfec8bde6ba8a6a347ab2a554c2e66d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* genRedeemCode($length) | |
* | |
* Returns a random $length (default 7) | |
* characters to use a code for user to redeem a badge | |
* | |
*/ | |
function genRedeemCode($length = 7) | |
{ | |
$redeemer =""; | |
for($x=0;$x <= $length;$x++){ | |
// as of php 4.3.x you dont need to seed randomizer. WOOT! | |
$modo = rand() % 3; | |
switch ($modo) | |
{ | |
case '0': | |
$code =genRandomNumber(); | |
break; | |
case '1': | |
$code = genRandomCapital(); | |
break; | |
case '2': | |
$code = genRandomLower(); | |
break; | |
default: | |
# code... | |
break; | |
} | |
$redeemer .=$code; | |
} | |
return $redeemer; | |
} | |
/** | |
* genRandomNumber | |
* | |
* called from genRedeemCode, returns a random number based on ascii | |
*/ | |
function genRandomNumber() | |
{ | |
$randomascii = rand(48,57); | |
return chr($randomascii); | |
} | |
/** | |
* genRandomCapital | |
* | |
* called from genRedeemCode, returns a random capital letter | |
*/ | |
function genRandomCapital() | |
{ | |
$randomascii = rand(65,90); | |
return chr($randomascii); | |
} | |
/** | |
* genRandomLower | |
* | |
* called from genRedeemCode, returns a random lowercase letter | |
*/ | |
function genRandomLower() | |
{ | |
$randomascii = rand(97,122); | |
return chr($randomascii); | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment