Keyword
Token akses function generateRandomString randomString dev random fclose fread trigger_error guidv4 str_split openssl_random_pseudo_bytes random_bytes
<?php
function get_token($panjang){
$token = array(
range(1,9),
range('a','z'),
range('A','Z')
);
$karakter = array();
foreach($token as $key=>$val){
foreach($val as $k=>$v){
$karakter[] = $v;
}
}
$token = null;
for($i=1; $i<=$panjang; $i++){
// mengambil array secara acak
$token .= $karakter[rand($i, count($karakter) - 1)];
}
return $token;
}
// panjang 15 karakter
echo get_token(15);
echo '<hr />';
// panjang 50 karakter
echo get_token(50);
function generateRandomString($length = 10) {
$characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$charactersLength = strlen($characters);
$randomString = '';
for ($i = 0; $i < $length; $i++) {
$randomString .= $characters[rand(0, $charactersLength - 1)];
}
return $randomString;
}
function generateRandomString($len)
{
$fp = @fopen('/dev/random','rb');
$result = '';
if ($fp !== FALSE) {
$result .= @fread($fp, $len);
@fclose($fp);
}
else
{
trigger_error('Can not open /dev/urandom.');
}
// convert from binary to string
$result = base64_encode($result);
// remove none url chars
$result = strtr($result, '+/', '-_');
// Remove = from the end
$result = str_replace('=', ' ', $result);
return $result;
}
function guidv4()
{
if (function_exists('com_create_guid') === true)
return trim(com_create_guid(), '{}');
$data = openssl_random_pseudo_bytes(16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
<?php
$bytes = random_bytes(5);
$str = bin2hex($bytes);
var_dump($str);
// string(10) "385e33f741"
?>
<?php
$r = unpack('v*', fread(fopen('/dev/random', 'r'),16));
$uuid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
$r[1], $r[2], $r[3], $r[4] & 0x0fff | 0x4000,
$r[5] & 0x3fff | 0x8000, $r[6], $r[7], $r[8]);
?>
Sumber: Cah Biyen
Komentar
Posting Komentar