Codeigniter哈希值不会在循环中更改

问题描述:

I am attempting to use the codeigniter security class to generate a unique long code which I can use to update an online db securely:

http://mydomain.com/myproject/index.php/my_controller/index/***3d51693c8c4***..

I noticed that when I create a hash for individual records in a loop using:

$hash= $this->security->get_csrf_hash();

the same hash keeps getting generated. I need a unique hash/security code to be generated for each record in my loop. I don't see anything in http://ellislab.com/codeigniter%20/user-guide/libraries/security.html discussing how to do this.

What is the best way to do this in CI?

我正在尝试使用codeigniter安全类生成一个唯一的长代码,我可以使用它来更新在线数据库 牢固地: p>

  http://mydomain.com/myproject/index.php/my_controller/index/***3d51693c8c4***..
 代码>  
 
 

我注意到当我使用以下代码在循环中为单个记录创建哈希时: p>

  $ hash = $ this-> security-  > get_csrf_hash(); 
  code>  pre> 
 
 

生成相同的哈希值。 我需要为循环中的每条记录生成一个唯一的哈希/安全代码。 我在 http://ellislab.com/codeigniter%中看不到任何内容 20 / user-guide / libraries / security.html 讨论如何执行此操作。 p>

在CI中执行此操作的最佳方法是什么? p>

If it's a unique code that you want to generate, why not build one yourself with built-in php function?

<?php

for ($i = 0; $i <= 1000; $i++) {
   $hash = uniqid(md5(time()), true);
   var_dump($hash);
}

?>

Sample output: http://pastebin.com/eSWaLhUt

As you can see, it's unique, regardless of how fast you iterate through the loop.

The uniqid method will supply a unique security code (be sure to use the more_entropy argument however this isn't particulary secure so depending on what you're using it for consider using mt_rand.

uniqid docs

mt_rand docs

I prefer to use the OS to generate unique values. This solution is a *nix only solution, but one of my favs

/** 
 * A Linux-dependent password generator.  This seems to be about 1000x better than
 * typical PHP-based password generation, and from what I hear, /dev/urandom should
 * be secure enough for temporary passwords.
 *
 * Lifted from:
 * http://stackoverflow.com/questions/6101956/generating-a-random-password-in-php
 */
static public function generatePassword($iLength=0)
{   
    $rFp = fopen('/dev/urandom', 'r');
    if(!$rFp)
        throw new RuntimeException("Can't access /dev/urandom to get random data.");

    // 1024 bytes should be enough
    $sRandom = fread($rFp, 1024);
    fclose($rFp);

    return substr(trim(base64_encode(md5($sRandom, true)), '='), $iLength * -1);
}