Exemple #1
0
function createAPIKey($valid_chars, $length)
{
    // Global variables provided by config.php for DB connection.
    global $con, $host, $user, $password, $db, $called;
    // start with an empty random string
    $random_string = "";
    // count the number of chars in the valid chars string so we know how many choices we have
    $num_valid_chars = strlen($valid_chars);
    // repeat the steps until we've created a string of the right length
    for ($i = 0; $i < $length; $i++) {
        // pick a random number from 1 up to the number of valid chars
        $random_pick = mt_rand(1, $num_valid_chars);
        // take the random character out of the string of valid chars
        // subtract 1 from $random_pick because strings are indexed starting at 0, and we started picking at 1
        $random_char = $valid_chars[$random_pick - 1];
        // add the randomly-chosen char onto the end of our string so far
        $random_string .= $random_char;
    }
    //MySQL Query to check if access code exists.
    $query = "SELECT API_Key FROM users WHERE API_Key = '{$random_string}'";
    //Execute Query.
    $result = mysqli_query($con, $query);
    //Get number of rows from DB query.
    $count = mysqli_num_rows($result);
    //Free Result Set.
    mysqli_free_result($result);
    //Check if code already exists.
    if ($count != 0) {
        $called++;
        createAPIKey($valid_chars, $length);
    } elseif ($called >= 5) {
        die('Error Code 100: An Error Occurred. Please Contact an Administrator.');
    } else {
        return $random_string;
    }
}
Exemple #2
0
    } else {
        //Create captcha
        $securimage = new Securimage();
        //Check if captcha was entered correctly. Display error if it is not entered correctly.
        if ($securimage->check($_POST['captcha']) == false) {
            header('location: wrongcaptcha.php');
            exit;
        }
        //Hash password.
        $hash = hash('sha256', $pwd);
        //Create Salt
        $salt = createSalt();
        //Hash Salt and Hash
        $hash = hash('sha256', $salt . $hash);
        //Generate API_Key
        $api_key = createAPIKey($valid_chars, 15);
        //Registration Query.
        $query = "INSERT INTO users (Username, Password, Salt, Name, Email, Gender, DOB, Height, Weight, Location, StepGoal, API_Key) \n\t\tVALUES ('{$username}', '{$hash}', '{$salt}', '{$name}', '{$email}', '{$gender}', '{$dob}', '{$height}', '{$weight}', '{$location}', '{$stepgoal}', '{$api_key}')";
        //Execute Query
        if (mysqli_query($con, $query)) {
            echo '	<p>
					Account was successfully created.
					<br />
					<br />
					<a href="login.php">Click Here</a> to go login.
				</p>';
        } else {
            die('Error: ' . mysqli_error($con));
        }
    }
    echo '													</li>