源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

PHP获取redis里不存在的6位随机数应用示例【设置24小时过时】

  • 时间:2020-04-02 23:36 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:PHP获取redis里不存在的6位随机数应用示例【设置24小时过时】
本文实例讲述了PHP获取redis里不存在的6位随机数的方法。分享给大家供大家参考,具体如下: [b]PHP获取6位数随机数[/b] PHP [code]str_shuffle()[/code] 函数 str_shuffle() 函数随机打乱字符串中的所有字符。
参数 描述
[i]string[/i] 必需。规定要打乱的字符串。
用php的str_shuffle函数:
<?php
$randStr = str_shuffle('ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890');
$rand = substr($randStr,0,6);
?>

[b]实例:获取redis里面不存在的6位随机数(设置24小时过时)[/b]
$port_number = '1605D1BCC6C8027BA0223147652D67D6';
$send_number = $this->getSixRandNumber();
$rs = $this->redis->setKeyValue('ports:' . $send_number,$port_number);
//以秒为最小单位
$this->redis->setTimeout('ports:' . $send_number,24*3600);
/**
* 获取6位数随机数
*/
protected function getSixRandNumber(){
$randStr = str_shuffle('1234567890');
$rand = substr($randStr,0,6);
$port = $this->redis->getItemByKey('ports:' .$rand);
//存在的重新取
if($port != null){
return $this->getSixRandNumber();
}
return $rand;
}
[b]PS:这里再为大家提供两款功能类似的在线工具供大家参考:[/b] [b]在线随机数字/字符串生成工具: [/b][url=http://tools.jb51.net/aideddesign/suijishu]http://tools.jb51.net/aideddesign/suijishu[/url] [b]高强度密码生成器: [/b][url=http://tools.jb51.net/password/CreateStrongPassword]http://tools.jb51.net/password/CreateStrongPassword[/url] 更多关于PHP相关内容感兴趣的读者可查看本站专题:《[url=http://www.1sucai.cn/Special/47.htm]php字符串(string)用法总结[/url]》、《[url=http://www.1sucai.cn/Special/614.htm]PHP数据结构与算法教程[/url]》、《[url=http://www.1sucai.cn/Special/111.htm]php程序设计算法总结[/url]》、《[url=http://www.1sucai.cn/Special/150.htm]php排序算法总结[/url]》、《[url=http://www.1sucai.cn/Special/242.htm]PHP常用遍历算法与技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/630.htm]PHP数学运算技巧总结[/url]》、《[url=http://www.1sucai.cn/Special/623.htm]PHP数组(Array)操作技巧大全[/url]》及《[url=http://www.1sucai.cn/Special/231.htm]php常见数据库操作技巧汇总[/url]》 希望本文所述对大家PHP程序设计有所帮助。
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部