Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Thread Contributor: adminHow to create a unique key using php
#1
Hello friends every web application or android app has user registration function we provide username and password for user verification with this verification we can easily add a simple unique token.
Today i am teaching you to how to create a unique token using php 

create following files

 token.php
PHP Code:
<?php
   function crypto_rand_secure
($min$max)
{
    $range $max $min;
    if ($range 1) return $min// not so random...
    $log ceil(log($range2));
    $bytes = (int) ($log 8) + 1// length in bytes
    $bits = (int) $log 1// length in bits
    $filter = (int) (<< $bits) - 1// set all lower bits to 1
    do {
        $rnd hexdec(bin2hex(openssl_random_pseudo_bytes($bytes)));
        $rnd $rnd $filter// discard irrelevant bits
    } while ($rnd $range);
    return $min $rnd;
}

function 
getToken($length)
{
    $token "";
    $codeAlphabet "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $codeAlphabet.= "abcdefghijklmnopqrstuvwxyz";
    $codeAlphabet.= "0123456789";
    $max strlen($codeAlphabet); // edited

    for ($i=0$i $length$i++) {
        $token .= $codeAlphabet[crypto_rand_secure(0$max-1)];
    }

    return $token;
}

?>

in which file do you want to create token add following code
PHP Code:
include "token.php";
 
$uniquetoken=md5(getToken(20));

echo 
$uniquetoken


Note:If you want to create 15digit token key just replace 20 in getToken with your 15
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)