Application example of using PHP to obtain a 6-digit random number that does not exist in redis

陈政宽~
Release: 2023-03-11 20:34:01
Original
1029 people have browsed it

This article mainly introduces the method of PHP to obtain 6-digit random numbers that do not exist in redis. It can set a 24-hour expiration limit and involves php string and database related operation skills. Friends who need it can Refer to the following

The example of this article describes how PHP obtains a 6-digit random number that does not exist in redis. Share it with everyone for your reference, the details are as follows:

PHP gets a 6-digit random number

PHP <a href="http://www.php.cn/wiki/1373.html" target="_blank">str_shuffle</a>() Function

str_shuffle() function randomly shuffles all characters in the string.

Parameters Description
string Required. Specifies the string to be scrambled.

Use PHP's str_shuffle function:

<?php
$randStr = str_shuffle(&#39;ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890&#39;);
$rand = substr($randStr,0,6);
?>
Copy after login

Example: Get a 6-digit random number that does not exist in redis (set 24 hours Outdated)

$port_number = &#39;1605D1BCC6C8027BA0223147652D67D6&#39;;
$send_number = $this->getSixRandNumber();
$rs = $this->redis->setKeyValue(&#39;ports:&#39; . $send_number,$port_number);
//以秒为最小单位
$this->redis->setTimeout(&#39;ports:&#39; . $send_number,24*3600);
/**
* 获取6位数随机数
*/
protected function getSixRandNumber(){
$randStr = str_shuffle(&#39;1234567890&#39;);
$rand = substr($randStr,0,6);
$port = $this->redis->getItemByKey(&#39;ports:&#39; .$rand);
//存在的重新取
if($port != null){
return $this->getSixRandNumber();
}
return $rand;
}
Copy after login

The above is the detailed content of Application example of using PHP to obtain a 6-digit random number that does not exist in redis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!