34

PHP を使用して、DB に保存して電子メールの確認に使用できるランダムな確認コードを生成する方法は何ですか? 私は一生、ユーザーのプロファイルから生成できる一意の番号を生成する方法を考えることはできません。そうすれば、関数を使用して、URL に含めるのに十分な数にすることができます (このリンクを参照してください)。ユーザーは自分のアカウントを「確認/有効化」するためにリンクをクリックする必要があることに注意してください。数字が使えなくても、文字も数字も問題なく使えます。

そうは言っても、ユーザー名と「塩」をハッシュしてランダムコードを生成しようとしました。もっと良い方法があるはずだとわかっているので、聞いてみましょう。

4

5 に答える 5

57
$random_hash = md5(uniqid(rand(), true));

これは、32文字の英数字で一意になります。短くしたい場合は、substr()を使用してください。

$random_hash = substr(md5(uniqid(rand(), true)), 16, 16); // 16 characters long

ランダムデータを生成する別の方法は次のとおりです。

$random_hash = md5(openssl_random_pseudo_bytes(32));
$random_hash = md5(mcrypt_create_iv(32, MCRYPT_DEV_URANDOM));

// New in PHP7
$random_hash = bin2hex(random_bytes(32));
于 2010-01-18T20:30:54.057 に答える
10

1)データベースにアクティブ化されたフィールドを作成する

2)登録後、Eメールが送信されます

3)メールに含めるリンクを作成し、一意の識別子を使用する次のようになります

ようこそユーザー名登録していただきありがとうございます。

以下のリンクをクリックして、アカウントを有効にしてください

domain.com/register.php?uid=100&activate=1

4)アクティブ化されたフィールドをtrueに更新します

代替テキスト
(出典:jackborn.com

$email_encrypt = urlencode($email);
$special_string = 'maybeyourcompanynamereversed?';
$hash = md5($email_encrypt.$special_string);

Here is the link that is sent to the email that was provided:

http://yourdoman.com/confirm.php?hash='.$hash.'

The actual link will look something like this:

http://yourdomain.com/confirm.php?hash=00413297cc003c03d0f1ffe1cc8445f8
于 2010-01-18T20:45:37.267 に答える
4

受け入れられた答えは、PHP の のハッシュを使用することを提案していますuniqid()uniqidのドキュメントでは、「ランダムまたは予測不可能な文字列」を作成しないことを明示的に警告し、「この関数はセキュリティ目的で使用してはならない」と強調しています。

確認コードが推測される可能性に懸念がある場合 (それがコード発行の要点です) openssl_random_pseudo_bytes()、. を使用bin2hex()して、適切な英数字に変換できます。以下は、ジョン・コンデの答えの出力のように見えますが、(おそらく) よりランダムで推測しにくいものです:

// generate a 16 byte random hex string
$random_hash = bin2hex(openssl_random_pseudo_bytes(16))

後半の補遺: Oleg Abrazhaev が指摘しているように、システムが実行時に暗号的に強力なランダム値を実際に生成できることを確認したい場合は、openssl_random_pseudo_bytesこれを報告するために bool への参照を受け入れます。phpinspectionsea docsからのコード:

$random = openssl_random_pseudo_bytes(32, $isSourceStrong);
if (false === $isSourceStrong || false === $random) {
    throw new \RuntimeException('IV generation failed');
}

次に、生成されたランダム値を前と同じように使用します。

$random_hash = bin2hex($random)
于 2014-08-30T01:51:04.737 に答える
3

もう少し堅牢で機能を追加したものが必要だと判断しました。これが私が思いついたものです。

/**
 * Hash Gen 
 * @author Kyle Coots
 * @version    1.0
 * Allow you to create a unique hash with a maximum value of 32.
 * Hash Gen uses phps substr, md5, uniqid, and rand to generate a unique 
 * id or hash and allow you to have some added functionality.
 * 
 * @see subtr()
 * @see md5()
 * @see uniqid()
 * @see rand()
 *  
 * You can also supply a hash to be prefixed or appened
 * to the hash. hash[optional] is by default appened to the hash 
 * unless the param prefix[optional] is set to prefix[true].     
 * 
 * @param start[optional]
 * @param end[optional]
 * @param hash[optional]
 * @param prefix bool[optional]
 * 
 * @return string a unique string max[32] character
 */
function hash_gen($start = null, $end = 0, $hash = FALSE, $prefix = FALSE){

    // start IS set NO hash
    if( isset($start, $end) && ($hash == FALSE) ){

        $md_hash = substr(md5(uniqid(rand(), true)), $start, $end);
        $new_hash = $md_hash;

    }else //start IS set WITH hash NOT prefixing
    if( isset($start, $end) && ($hash != FALSE) && ($prefix == FALSE) ){

        $md_hash = substr(md5(uniqid(rand(), true)), $start, $end);
        $new_hash = $md_hash.$hash;

    }else //start NOT set WITH hash NOT prefixing 
    if( !isset($start, $end) && ($hash != FALSE) && ($prefix == FALSE) ){

        $md_hash = md5(uniqid(rand(), true));
        $new_hash = $md_hash.$hash;

    }else //start IS set WITH hash IS prefixing 
    if( isset($start, $end) && ($hash != FALSE) && ($prefix == TRUE) ){

        $md_hash = substr(md5(uniqid(rand(), true)), $start, $end);
        $new_hash = $hash.$md_hash;

    }else //start NOT set WITH hash IS prefixing
    if( !isset($start, $end) && ($hash != FALSE) && ($prefix == TRUE) ){

        $md_hash = md5(uniqid(rand(), true));
        $new_hash = $hash.$md_hash;

    }else{

        $new_hash = md5(uniqid(rand(), true));

    }

    return $new_hash;

 } 
于 2013-03-04T02:15:28.270 に答える