网络营销电子商务研究中心  
How to buy the best prescription safety glasses in Canada? Let's study!
Go Back   网络营销电子商务研究中心 > 网站设计 > PHP
User Name
Password
 
FAQ Members List Calendar Cheap Glasses

Reply
 
Thread Tools Display Modes
  #1   IP: 180.125.50.74
Old 2015-01-16, 09:17 AM
Natalia Natalia is offline
初级会员
 
Join Date: 2009-11-18
Posts: 1
Natalia 现在声名狼藉
Default Php生成随机用户名和密码的实现代码

使用PHP生成随机数可以应用在许多地方,比如可以设计程序的随机密码、模拟掷骰子游戏的应用程序、石头剪子布游戏应用程序等等

有时候我们需要在应用程序中使用随机生成用户名和密码,这样可以大大提高应用程序的安全,在PHP中生成随机用户名和密码可以使用 mt_rand 函数或者是 rand 函数, rand 函数在验证码中的应用多一些,而生成长字符的随机码一般都需要 mt_rand 函数。

使用PHP生成随机数可以应用在许多地方,比如可以设计程序的随机密码、模拟掷骰子游戏的应用程序、石头剪子布游戏应用程序等等。

下面是PHP生成随机数的两个函数方法:

Code:
<?php
header("Content-type: text/html; charset=utf-8"); 

//自动为用户随机生成用户名(长度6-13) 
        function create_password($pw_length = 4){
            $randpwd = '';
            for ($i = 0; $i < $pw_length; $i++){
                $randpwd .= chr(mt_rand(33, 126));
            }
            return $randpwd;
        }
        function generate_username( $length = 6 ) {
            // 密码字符集,可任意添加你需要的字符 
            $chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789!@#$%^&*()-_ []{}<>~`+=,.;:/?|';
            $password = '';
            for ( $i = 0; $i < $length; $i++ )
            {
                // 这里提供两种字符获取方式
                // 第一种是使用substr 截取$chars中的任意一位字符;
                // 第二种是取字符数组$chars 的任意元素
                // $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
                $password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
            }
            return $password;
        }
        // 调用该函数 
        $userId = 'user'.generate_username(6);
        $pwd = create_password(13);
		echo '用户名:'.$userId.' 用户密码:'.$pwd;
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Prescription-glasses.com offers prescription glasses online at discount prices.
All times are GMT +8. The time now is 10:52 PM.


Powered by vBulletin Version 3.8.7
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.