Home > php教程 > php手册 > php生成指定长度验证码(用于短信验证)

php生成指定长度验证码(用于短信验证)

WBOY
Release: 2016-06-07 11:39:49
Original
1322 people have browsed it

刚才在写短信验证码模块,需要用到指定位数的随机数,然后网上一找发现太可怕了这么简单的事情竟然用了好几十行多个循环嵌套……看来没有好脑仁儿真的不适合当程序员。

自写了一行版本:<?php <br /> function generate_code($length = 6) {<br>     return rand(pow(10,($length-1)), pow(10,$length)-1);<br> }


为了便于理解,同时也为了这篇水文可以凑点字数,这是多行版:<?php <br /> function generate_code($length = 6) {<br>     $min = pow(10 , ($length - 1));<br>     $max = pow(10, $length) - 1;<br>     return rand($min, $max);<br> }

AD:真正免费,域名+虚机+企业邮箱=0元

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 Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template