如何可能生成一个随机的,唯一的字符串使用数字和字母用于验证链接?就像你在一个网站上创建了一个账户,它会给你发一封带有链接的电子邮件,你必须点击那个链接才能验证你的账户

如何使用PHP生成其中一个?


当前回答

我总是使用这个my函数来生成一个自定义随机字母数字字符串…希望这对你有所帮助。

<?php
  function random_alphanumeric($length) {
    $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ12345689';
    $my_string = '';
    for ($i = 0; $i < $length; $i++) {
      $pos = random_int(0, strlen($chars) -1);
      $my_string .= substr($chars, $pos, 1);
    }
    return $my_string;
  }
  echo random_alphanumeric(50); // 50 characters
?>

例如,它生成:Y1FypdjVbFCFK6Gh9FDJpe6dciwJEfV6MQGpJqAfuijaYSZ86g

如果你想与另一个字符串进行比较,以确保它是唯一的序列,你可以使用这个技巧…

$string_1 = random_alphanumeric(50);
$string_2 = random_alphanumeric(50);
while ($string_1 == $string_2) {
  $string_1 = random_alphanumeric(50);
  $string_2 = random_alphanumeric(50);
  if ($string_1 != $string_2) {
     break;
  }
}
echo $string_1;
echo "<br>\n";
echo $string_2;

它生成两个唯一的字符串:

qsBDs4JOoVRfFxyLAOGECYIsWvpcpMzAO9pypwxsqPKeAmYLOi

Ti3kE1WfGgTNxQVXtbNNbhhvvapnaUfGMVJecHkUjHbuCb85pF

希望这就是你想要的……

其他回答

生成一个随机数使用 你最喜欢的随机数 发电机 乘除 得到一个与数字匹配的数字 你的代码字母表中的字符 将该索引处的项插入 你的代码字母。 重复1),直到你有你想要的长度 想要

例如(伪代码)

int myInt = random(0, numcharacters)
char[] codealphabet = 'ABCDEF12345'
char random = codealphabet[i]
repeat until long enough
function codeGenerate() {
  $randCode  = (string)mt_rand(1000,9999);
  $randChar  = rand(65,90);
  $randInx   = rand(0,3);
  $randCode[$randInx] = chr($randChar);
  return $randCode;
}
echo codeGenerate();

输出

38I7
33V7
E836
736U

以下是我使用的方法:

md5(time() . rand());    
// Creates something like 0c947c3b1047334f5bb8a3b7adc1d97b

PHP 7标准库提供了random_bytes($length)函数,该函数生成加密安全的伪随机字节。

例子:

$bytes = random_bytes(20);
var_dump(bin2hex($bytes));

上面的例子将输出类似于:

string(40) "5fe69c95ed70a9869d9f9af7d8400a6673bb9ce9"

更多信息:http://php.net/manual/en/function.random-bytes.php

PHP 5(过时)

我只是在寻找如何解决这个相同的问题,但我也希望我的函数创建一个令牌,可以用于密码检索以及。这意味着我需要限制令牌的猜测能力。因为uniqid是基于时间的,而根据php.net“返回值与microtime()相差不大”,uniqid不符合条件。PHP建议使用openssl_random_pseudo_bytes()来生成加密安全的令牌。

一个快速、简短、直截了当的答案是:

bin2hex(openssl_random_pseudo_bytes($bytes))

它将生成一个长度= $bytes * 2的字母数字字符的随机字符串。不幸的是,这只有一个字母[a-f][0-9],但它是有效的。


Below is the strongest function I could make that satisfies the criteria (This is an implemented version of Erik's answer).
function crypto_rand_secure($min, $max)
{
    $range = $max - $min;
    if ($range < 1) return $min; // not so random...
    $log = ceil(log($range, 2));
    $bytes = (int) ($log / 8) + 1; // length in bytes
    $bits = (int) $log + 1; // length in bits
    $filter = (int) (1 << $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;
}

Crypto_rand_secure ($min, $max)作为rand()或mt_rand的替换。它使用openssl_random_pseudo_bytes来帮助创建一个介于$min和$max之间的随机数。

getToken($length)创建一个要在令牌中使用的字母表,然后创建一个长度为$length的字符串。

来源:http://us1.php.net/manual/en/function.openssl-random-pseudo-bytes.php # 104322

function random_string($length = 8) {
    $alphabets = range('A','Z');
    $numbers = range('0','9');
    $additional_characters = array('_','=');
    $final_array = array_merge($alphabets,$numbers,$additional_characters);
       while($length--) {
      $key = array_rand($final_array);

      $password .= $final_array[$key];
                        }
  if (preg_match('/[A-Za-z0-9]/', $password))
    {
     return $password;
    }else{
    return  random_string();
    }

 }