This article mainly introduces PHP to obtain the 6-bit random number in the Redis method, can set 24-hour outdated restrictions, involving PHP string and database-related operations skills, the need for friends can refer to the next
In this paper, we describe a method for PHP to obtain a 6-bit random number that does not exist in Redis. Share to everyone for your reference, as follows:
PHP gets 6-digit random number
PHP str_shuffle()
functions
The Str_shuffle () function randomly scrambles all characters in a string.
parameter |
description |
String |
Necessary. Specifies the string to be scrambled. |
Using PHP's Str_shuffle function:
<?php$randstr = Str_shuffle (' ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890 '); $rand = substr ($randStr, 0,6);? >
Example: Get a 6-bit random number that does not exist in Redis (set 24-hour obsolete)
$port _number = ' 1605d1bcc6c8027ba0223147652d67d6 '; $send _number = $this->getsixrandnumber (); $rs = $this->redis- >setkeyvalue (' Ports: '. $send _number, $port _number);//in seconds the smallest unit $this->redis->settimeout (' ports: '. $send _ number,24*3600);/*** gets a 6-digit random number */protected function Getsixrandnumber () {$randStr = Str_shuffle (' 1234567890 '); $rand = substr ($RANDSTR, 0,6); $port = $this->redis->getitembykey (' Ports: '. $rand);//The presence of a re-fetch if ($port! = null) {return $ This->getsixrandnumber ();} return $rand;}