PHP Gets an example of a 6-bit random number application that does not exist in Redis

Source: Internet
Author: User
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;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.