Jump to content


Photo

Generating keys


  • Please log in to reply
7 replies to this topic

#1 roc13x

roc13x

    Advanced Member

  • +Clients
  • 360 posts

Posted 12 March 2011 - 09:21 PM

Hi,
In the API docs, you post the code for generating the md5 license keys.
Can you please post the code for the standard method? I would like to modify it slightly to customize the first block of characters.

Thanks :)

#2 roc13x

roc13x

    Advanced Member

  • +Clients
  • 360 posts

Posted 14 March 2011 - 02:30 PM

I want to make the first 3 characters of the standard key to be a certain 3 letters that do not change, in case you were wondering why I wanted to see the code :)

#3 Francismori7_2

Francismori7_2

    IPB Full Member

  • Visitors
  • PipPipPip
  • 185 posts

Posted 14 March 2011 - 02:58 PM

I'm gonna go with a simple code I just wrote, don't know if it's that one.

<?php

function genRandomString($length) {
	$characters = '0123456789abcdefghijklmnopqrstuvwxyz';
	$string = '';

	for ($p = 0; $p < $length; $p++) {
	$string .= $characters[mt_rand(0, strlen($characters)-1)];
	}
	return strtoupper($string);
}

// Generate the code itself. (ABC##-#####-#####-#####)
function generateCode() {
	$code = 'ABC';
	$code .= genRandomString(2);
	$code .= '-';
	$code .= genRandomString(5);
	$code .= '-';
	$code .= genRandomString(5);
	$code .= '-';
	$code .= genRandomString(5);

	return $code;
}

echo generateCode();

?>


#4 roc13x

roc13x

    Advanced Member

  • +Clients
  • 360 posts

Posted 14 March 2011 - 03:45 PM

That's quite helpful, actually. Thanks! :)

#5 Mark

Mark

    I dropped the "iggy"

  • IPS Staff
  • 8,209 posts

Posted 14 March 2011 - 04:13 PM

I'll post this for you tomorrow. PM me if I forget :)
  • roc13x likes this
Mark Wade
Developer

Posted Image Posted Image

#6 roc13x

roc13x

    Advanced Member

  • +Clients
  • 360 posts

Posted 14 March 2011 - 04:19 PM

I'll post this for you tomorrow. PM me if I forget :)


Great, thanks :)

#7 Mark

Mark

    I dropped the "iggy"

  • IPS Staff
  • 8,209 posts

Posted 15 March 2011 - 04:54 AM


<?php

/**

 * @file		standard.php	Generates an Standard License Key (XXXX-XXXX-XXXX-XXXX-XXXX)

 *

 * $Copyright: $

 * $License: $

 * $Author: mark $

 * $LastChangedDate: 2011-03-11 09:49:17 +0000 (Fri, 11 Mar 2011) $

 * $Revision: 8028 $

 * @since 		30th December 2010

 */



/**

 *

 * @class	licenseKey_standard

 * @brief	Generates an Standard License Key (XXXX-XXXX-XXXX-XXXX-XXXX)

 *

 */

class licenseKey_standard

{

	/**

	 * Number of blocks

	 *

	 * @var		$blocks

	 */

	private static $blocks		= 5;

	

	/**

	 * Number of characters in a block

	 *

	 * @var		$characters

	 */

	private static $characters = 4;

	

	/**

	 * Lowest allowed ASCII number

	 *

	 * @var		$low

	 */

	private static $low		= 48; // 0

	

	/**

	 * Highest allowed ASCII number

	 *

	 * @var		$low

	 */

	private static $high		= 90; // Z

	

	/**

	 * Disallowed ASCII numbers

	 *

	 * @var		$disallowed

	 */

	private static $disallowed	= array( 58, 59, 60, 61, 62, 63, 64 ); // Various non A-Z / 0-9 characters

	

	/**

	 * Seperator between blocks

	 *

	 * @avr		$seperator

	 */

	private static $seperator	= '-';



	/**

	 * Generates a License Key

	 *

	 * @param	array		$member		Member Data

	 * @param	array		$purchase	Purchase Data

	 * @return	@e string 				License Key

	 */

	public static function generate( $member, $purchase )

	{

		$key = array();

		foreach ( range( 1, self::$blocks ) as $i )

		{

			$_k = '';

			foreach ( range( 1, self::$characters ) as $j )

			{

				do

				{

					$chr = rand( self::$low, self::$high );

				}

				while ( in_array( $chr, self::$disallowed ) );

				$_k .= chr( $chr );

			}

			$key[] = $_k;

		}

		

		return implode( self::$seperator, $key );

	}

}


Mark Wade
Developer

Posted Image Posted Image

#8 roc13x

roc13x

    Advanced Member

  • +Clients
  • 360 posts

Posted 15 March 2011 - 05:36 AM

Awesome, thanks :)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users