Jump to content
php.lv forumi

Ierosinājums


Recommended Posts

<code><font color="#000000">
<font color="#0000CC"><?
</font><font color="#FF9900">//srand just once please 
</font><font color="#0000CC">[url=http://lv.php.net/srand]srand[/url]</font><font color="#006600">((double)</font><font color="#0000CC">[url=http://lv.php.net/microtime]microtime[/url]</font><font color="#006600">()*</font><font color="#0000CC">1000000</font><font color="#006600">); 

</font><font color="#FF9900">//return  a (pseudo) random string of specified length, default=114 
</font><font color="#006600">function </font><font color="#0000CC">RandomString</font><font color="#006600">( </font><font color="#0000CC">$passwordLength</font><font color="#006600">=</font><font color="#0000CC">114</font><font color="#006600">) { 
  </font><font color="#0000CC">$password </font><font color="#006600">= </font><font color="#CC0000">""</font><font color="#006600">; 
  for (</font><font color="#0000CC">$index </font><font color="#006600">= </font><font color="#0000CC">1</font><font color="#006600">; </font><font color="#0000CC">$index </font><font color="#006600"><= </font><font color="#0000CC">$passwordLength</font><font color="#006600">; </font><font color="#0000CC">$index</font><font color="#006600">++) { 
	</font><font color="#FF9900">// Pick random number between 1 and 62 
   </font><font color="#0000CC">$randomNumber </font><font color="#006600">= </font><font color="#0000CC">[url=http://lv.php.net/rand]rand[/url]</font><font color="#006600">(</font><font color="#0000CC">1</font><font color="#006600">, </font><font color="#0000CC">62</font><font color="#006600">); 
  </font><font color="#FF9900">// Select random character based on mapping. 
	</font><font color="#006600">if (</font><font color="#0000CC">$randomNumber </font><font color="#006600">< </font><font color="#0000CC">11</font><font color="#006600">) 
		</font><font color="#0000CC">$password </font><font color="#006600">.= </font><font color="#0000CC">[url=http://lv.php.net/chr]chr[/url]</font><font color="#006600">(</font><font color="#0000CC">$randomNumber </font><font color="#006600">+ </font><font color="#0000CC">48 </font><font color="#006600">- </font><font color="#0000CC">1</font><font color="#006600">); </font><font color="#FF9900">// [ 1,10] => [0,9] 
  </font><font color="#006600">else if (</font><font color="#0000CC">$randomNumber </font><font color="#006600">< </font><font color="#0000CC">37</font><font color="#006600">) 
		 </font><font color="#0000CC">$password </font><font color="#006600">.= </font><font color="#0000CC">[url=http://lv.php.net/chr]chr[/url]</font><font color="#006600">(</font><font color="#0000CC">$randomNumber </font><font color="#006600">+ </font><font color="#0000CC">65 </font><font color="#006600">- </font><font color="#0000CC">10</font><font color="#006600">); </font><font color="#FF9900">// [11,36] => [A,Z] 
	</font><font color="#006600">else 
		 </font><font color="#0000CC">$password </font><font color="#006600">.= </font><font color="#0000CC">[url=http://lv.php.net/chr]chr[/url]</font><font color="#006600">(</font><font color="#0000CC">$randomNumber </font><font color="#006600">+ </font><font color="#0000CC">97 </font><font color="#006600">- </font><font color="#0000CC">36</font><font color="#006600">); </font><font color="#FF9900">// [37,62] => [a,z] 
  </font><font color="#006600">} 
 return </font><font color="#0000CC">$password</font><font color="#006600">; 
} 

</font><font color="#FF9900">//Returns randomness as string 
</font><font color="#006600">function </font><font color="#0000CC">NewSessionIdentifier</font><font color="#006600">() 
{ 
  </font><font color="#FF9900">//MD5 it (always 32 bytes long) 
</font><font color="#006600">return (string) </font><font color="#0000CC">[url=http://lv.php.net/md5]md5[/url]</font><font color="#006600">(</font><font color="#0000CC">uniqid </font><font color="#006600">(</font><font color="#0000CC">RandomString</font><font color="#006600">(</font><font color="#0000CC">114</font><font color="#006600">),</font><font color="#0000CC">1 </font><font color="#006600">)); 
}
</font><font color="#0000CC">?>
</font>
</font>
</code>

Link to comment
Share on other sites

×
×
  • Create New...