PHP生成随机密码

一个基于Web和CLI两种运行方式的PHP生成随机密码示例代码

<?php
if(isset($_GET['source'])) {
    highlight_file(__FILE__);
    exit;
}

//CLI Usage: php createPassword.php length=32\&type=all\&splitChar=_\&splitLength=4\×=5

//动态开启错误提示
ini_set('display_errors','on');        // 'off' 关闭
error_reporting(E_ALL);

//默认长度和类型
$length         = 32;
$type           = 'number-lower-upper';
$customStr      = '';
$splitChar      = '';
$splitLength    = 4;
$times          = 1;
$isCli          = (PHP_SAPI=='cli') ? true : false;

if($isCli) {
    if(!empty($argv[1]) && (($argv[1] == '--help') || ($argv[1] == '-h'))) {
        echo "Usage:        php createPassword.php length=32\\&type=number-lower-upper\\&splitChar=_\\&splitLength=4\×=5\n";
        echo "length:       length of password\n";
        echo "type:         password with which type of char, can be: number/lower/upper/special join by '-', or 'all' for all char and 'custom' for custom str\n";
        echo "customStr:    you can use type=custom to custom string for you password just like customStr=123456, and it will create password by use 123456\n";
        echo "splitChar:    split char for password, default is empty so that password just like connect one by one\n";
        echo "splitLength:  split password by every splitLength step\n";
        echo "times:        create how many password in one time\n";
        exit;
    }
    
    if(!empty($argv[1])) parse_str($argv[1], $_POST);
    else $_POST = array(
        'length'        => $length,
        'type'          => $type,
        'customStr'    => $customStr,
        'splitChar'     => $splitChar,
        'splitLength'   => $splitLength,
        'times'         => $times,
    );
}

if(!empty($_POST)) {
    /**
     *  获取随机密码函数
     *  @param string $type 密码字符串类型
     *  @param string $customStr 自定义密码组成字符
     *  @param int    $length 密码长度
     *  @param string $splitChar 分隔符,默认不分隔
     *  @param int    $splitLength 密码字符分隔长度 比如:密码为abc-def-ghi时$splitLength = 3
     *
     */
    function getPassword($type = 'number-lower-upper', $customStr = '', $length = 32, $splitChar = '', $splitLength = 4) {
        $strArr         = array(
            'number'    => '0123456789',
            'lower'     => 'abcdefghijklmnopqrstuvwxyz',
            'upper'     => 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
            'special'   => '~!@#$%^&*()_+{}|[]\-=:<>?/',
        );
        
        if($type == 'all') {        //全部
            $str = implode('', $strArr);
        } else if($type == 'custom') {        //自定义类型
            if(empty($customStr)) {    //未填写自定义类型,则默认为数字+大小写字母
                $type    = 'number-lower-upper';
            } else {
                $str     = $customStr;
            }
        }
        
        //custom 没带自定义类型 或 其他类型
        if(empty($str)) {
            $typeParts  = explode('-', $type);
            $str        = '';
            foreach($typeParts as $part) {
                $str   .= $strArr[$part];
            }
        }
        
        $maxLength         = 500;    //最大长度
        $length            = empty($length) ? $maxLength : min((int)$length, $maxLength);
        if(empty($length)) {
            $length = $maxLength;
        }
        
        $passwordStr    = '';
        do {
            $randStr        = str_shuffle($str);
            $passwordStr   .= substr($randStr, 0, 1);        //每次取一个字符
            $passwordLength = strlen($passwordStr);
        } while($passwordLength < $length);
        
        //需要分隔
        if($splitChar != '') {
            $passwordStr = implode($splitChar, str_split($passwordStr, $splitLength));
        }
        
        return $passwordStr;
    }
    
    $length         = empty($_POST['length'])       ? $length       : min(500, $_POST['length']);
    $type           = empty($_POST['type'])         ? $type         : $_POST['type'];
    $customStr      = empty($_POST['customStr'])    ? $customStr    : $_POST['customStr'];
    $splitChar      = empty($_POST['splitChar'])    ? $splitChar    : $_POST['splitChar'];
    $splitLength    = empty($_POST['splitLength'])  ? $splitLength  : $_POST['splitLength'];
    $times          = empty($_POST['times'])        ? $times        : min(100, max(1, (int)$_POST['times']));
    
    $passwordArr    = array();
    for($i = 0; $i < $times; $i++) {
        $passwordArr[] = getPassword($type, $customStr, $length, $splitChar, $splitLength);
    }
    
    $password = implode("\n", $passwordArr);
    
    if($isCli) {
        echo $password;
        exit;
    }
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>密码生成器</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <meta http-equiv="Content-Language" content="zh-CN" />
    <script type="text/javascript" src="http://www.zjmainstay.cn/jquerylib/jquery-1.8.2.min.js"></script>
</head>
<body>
<style type="text/css">
* {
    margin: 5px;
}
</style>
<form action="<?php echo $_SERVER['SCRIPT_NAME']?>" method="POST">
    <div>
    <label>长度:</label>
    <input type="text" name="length" value="<?php echo $length ?>" style="width: 100px;" placeholder="长度为0-32"/>
    </div>
    <div>
        <label>类型:</label>
        <select name="type" style="width: 150px;" id="type">
            <option value="number">纯数字</option>
            <option value="lower">小写字母</option>
            <option value="upper">大写字母</option>
            <option value="lower-upper">大小写字母</option>
            <option value="number-lower">数字+小写字母</option>
            <option value="number-upper">数字+大写字母</option>
            <option value="number-lower-upper">数字+大小写字母</option>
            <option value="special">特殊字符</option>
            <option value="number-special">数字+特殊字符</option>
            <option value="lower-special">小写字母+特殊字符</option>
            <option value="upper-special">大写字母+特殊字符</option>
            <option value="all">全部</option>
            <option value="custom">自定义</option>
        </select>
        <input name="customStr" type="text" size="100" value="<?php echo $customStr ?>"/>
    </div>
    <div>
        <label>密码分隔符(不填写则不分隔)</label>
        <input name="splitChar" type="text" value="<?php echo $splitChar ?>"/>
    </div>
    <div>
        <label>密码分隔长度</label>
        <input name="splitLength" type="text" value="<?php echo $splitLength ?>"/>
    </div>
    <div>
        <label>密码个数</label>
        <input name="times" type="text" value="<?php echo $times ?>"/>
    </div>
    <div>
    <input type="submit" value="生成"/>
    
    <?php if(!empty($password)) { ?>
    <div>
    <textarea id="password" readOnly="true" rows="10" cols="80"><?php echo $password ?></textarea><span class="tips"></span>
    </div>
    <?php } ?>
    </div>
</form>
<script type="text/javascript">
$(document).ready(function(){
    var strArr         = {
        'number'    : '0123456789',
        'lower'     : 'abcdefghijklmnopqrstuvwxyz',
        'upper'     : 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',
        'special'   : '~!@#$%^&*()_+{}|[]\-=:<>?/',
    };
    
    var updateCustomStrSize = function() {
        $("input[name=customStr]").attr("size", $("input[name=customStr]").val().length);
    }
    
    //更新自定义输出串内容
    var updateCustomStr = function() {
        var valueStr    = $("#type").val();
        var customStr   = '';
        if(valueStr == 'custom') {
            updateCustomStrSize();
            return true;
        } else if(valueStr == 'all') {
            $.each(strArr, function(i) {
                customStr  += strArr[i] || '';
            })
        } else {
            var parts       = valueStr.split('-');
            $.each(parts, function(i, type) {
                customStr  += strArr[type] || '';
            })
        }
        $("input[name=customStr]").val(customStr);
        updateCustomStrSize();
    }
    
    $("input[name=customStr]").on('keyup change', function() {
        if($("#type").val() != 'custom') {
            $("#type").get(0).selectedIndex = $("#type option").index($("#type option[value=custom]"));
        }
        updateCustomStrSize();
    });
    
    $("#type").change(function() {
        updateCustomStr();
    });
    
    $("#password").click(function() {
        $(this).select();
        $(".tips").html("请使用 Ctrl + C 复制");
    });
    
    //初始化默认选择数字+大小写字母
    $("#type").get(0).selectedIndex = $("#type option").index($("#type option[value=<?php echo $type ?>]"));
    updateCustomStr();
});
</script>
</body>
</html>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值