[HowTo]: Random Key Gen. in PHP menu

User Tag List

Results 1 to 4 of 4
  1. #1
    GrooN's Avatar Banned
    Reputation
    7
    Join Date
    Sep 2006
    Posts
    68
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [HowTo]: Random Key Gen. in PHP

    How to Create a Random Key Gen. in PHP
    by GrooN

    This is a tutorial on how to create a simple keygen, that generates keys with random characters (a-z, A-Z and 0-9).

    First off we're going to create the HTML code.
    I dont know why, we have to start somewhere.

    Code:
    <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        Colums: <input type="text" name="colums" /><br />
        Rows: <input type="text" name="amount" /><br />
        <input type="submit" />
    </form>
    Okay, you should know HTML, but im gonna explain it anyway.
    Here we're making a <form>, that makes it possible for us to send information from the user to the php script.
    Sets the method to get, and the action to $_SERVER['PHP_SELF'] (which returns the name of the current file.)

    Now, here comes the php part:
    PHP Code:
    <?php

    if ($_REQUEST['columns'] && $_REQUEST['amount'])
        {
        
    $library "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0987654321";
        for (
    $i 0$i $_REQUEST['columns']; $i++) // Loop one
        
    {
            for (
    $in 0$in $_GET['amount']; $in++) // Loop two
                
    $return .= $library[rand(0strlen($library))]; // Takes random char from the library string.
            
            
    if ($i < ($_REQUEST['columns'] - 1)) // Set the '-' tag between every column
                
    $return .= "-";
        }
        echo 
    $return// Print result
    }

    ?>
    Now that should be that hard to understand:
    first we see if the server has received the information sent from the form, by using the $_REQUEST[], which receives both GET and POST.

    Then we declare a library string, that contains all the possible characters, that the keygen are going to write.

    After that i create a for loop, that repeats itself as many times the variable $_REQUEST['columns'] says it.

    Then i make yet another for loop inside that one where i do the same just with the $_REQUEST['amount'] variable.

    Inside that for loop, we write to the "$return" variable. We write a random character from the library string, by using the function rand(min, max).

    When that second for loop ends the script adds a "-" tag if its not the last time it runs.

    At last we write the result by using the echo as you learned when you read some, hello world tutorial

    Mixing the two blocks of code, it should look like this:

    Code:
    <?php
    
    if ($_REQUEST['columns'] && $_REQUEST['amount'])
        {
        $library = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0987654321";
        for ($i = 0; $i < $_REQUEST['columns']; $i++) // Loop one
        {
            for ($in = 0; $in < $_GET['amount']; $in++) // Loop two
                $return .= $library[rand(0, strlen($library))]; // Takes random char from the library string.
    
            if ($i < ($_REQUEST['columns'] - 1)) // Set the '-' tag between every column
                $return .= "-";
        }
        echo $return; // Print result
    }
    
    ?>
    
    <form method="get" action="<?php echo $_SERVER['PHP_SELF']; ?>">
        Colums: <input type="text" name="colums" /><br />
        Rows: <input type="text" name="amount" /><br />
        <input type="submit" />
    </form>
    Dont be afraid to ask questions ^^
    Last edited by GrooN; 01-21-2010 at 05:47 PM.

    [HowTo]: Random Key Gen. in PHP
  2. #2
    colincancer's Avatar Active Member
    Reputation
    63
    Join Date
    Dec 2007
    Posts
    509
    Thanks G/R
    0/1
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    hmm looks pretty good... not at my home computer atm but when I am I'll give it a shot!
    #JODYS'WATCHiN

  3. #3
    GrooN's Avatar Banned
    Reputation
    7
    Join Date
    Sep 2006
    Posts
    68
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yea, good luck with that. If you run into any problems just ask, I don't bite ^^

  4. #4
    natt_'s Avatar Contributor
    Reputation
    145
    Join Date
    Dec 2007
    Posts
    391
    Thanks G/R
    13/0
    Trade Feedback
    3 (100%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Not to destroy your sex but this could be done easier by those fine 7 lines of code

    PHP Code:
    <?PHP
    $BrakeAfter 
    5;# When to put -
    $Width 25;# Key length
    $Rows 5;# how many keys
    $str "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0987654321";
    for(
    $i=0;$i<=$Rows;$i++){
      echo 
    substr(wordwrap(str_shuffle($str),$BrakeAfter,'-',TRUE),0,$Width).'<br />';
    }
    ?>
    Last edited by natt_; 02-03-2010 at 07:43 AM.

Similar Threads

  1. 50 Steam Random Keys - Giveaway
    By Merkur in forum Contests
    Replies: 42
    Last Post: 10-03-2015, 12:37 PM
  2. counter strike source key-gen
    By chyne1 in forum Gaming Chat
    Replies: 5
    Last Post: 02-12-2007, 08:48 PM
All times are GMT -5. The time now is 04:35 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search