php 生成随机帐号密码邮箱

 先创建一个方法

 $username = get_new_user($con);

这个方法先生成一个随机帐号,然后去数据库查询是否已经有了 如果有了就再次递归调用本方法

注意 这个递归在错误的时候没有出口,所以请求该接口的时候应当设置超时退出

function get_new_user($con)
{

    //获取唯一用户名 如果存在就再次随机查询
    $user_name = randStr();

    $sql = "select * from `user` where `username`='{$user_name}'";
    if (!$obj = mysqli_query($con, $sql)) {
        $resData['msg'] = mysqli_errno($con) . $sql;
        $resData['status'] = false;
        $res = json_encode($resData, true);
        exit($res);
    }
    if (mysqli_affected_rows($con) == 0) {
        return $user_name;
    } else {
        get_new_user($con);
    }
}

核心方法来了

function randStr($len = 6, $format = 'default')
{
    switch ($format) {
        case 'ALL':
            $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-@#~';
            break;
        case 'CHAR':
            $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-@#~';
            break;
        case 'NUMBER':
            $chars = '0123456789';
            break;
        default :
            $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
            break;
    }
    mt_srand((double)microtime() * 1000000 * getmypid());
    $password = "";
    while (strlen($password) < $len)
        $password .= substr($chars, (mt_rand() % strlen($chars)), 1);
    return $password;
}

邮箱和密码的生成更为简单

    $password = randStr();
    $email = $key_type . randStr() . "@fake.com";

 

代码简单干净,易于扩展。 随机生成用户名样例: 339063 77188 fengjr1980@yeah.net 97133 xupe19870102@sohu.com 18955990722 2145028 25070167 299966 13329809029 18739552501 77223 725525865 cheng1991 chub1974 13290062609 wangf19750725 caot19850619 545121 qianih19760611@msn.com 15208376363 xieww1979 shixd0904@126.com 18035677437 80892 18967033182 18923271615 15506730128 19741 fangi0713 15829813954 58285 yuanr19730429 shenpk 79607 15430 15226507276 13905108731 71852 qianmp19900629 15578331045 28116 18636868380 41560 zhengk1009@qq.com chuh19901023 269195 5678146 29585 18416167445 konghs1014 13404997586 30505 13647749758 13153649262 18779413966 wangqq1985 18906839566 18915522113 13392180833 1539516 zhaod19960502@ask.com 422424 heee0615 fengc@163.com 39174 xuyv19780415 15463681074 700812 73013 xuh0513 15505384990 hedd19940702 54021 xum19930812 youy1991 qiner 18033283015 yangwc 57195 qinu shenk0513 zhoufg 94597 95435 zhanvn1985 qiankh0104@ask.com kongw1993 zhuhv 15922309734 15015131852 15617928674 13135363801 18249225151 81438 13163635474 18615772400 hes0510 460915 wum19781014 491159 shenh1973 625701 34613 15732006897 fengdk1985 18517435664 13412117745 10157 chuza1993@163.com zhaokk0810@sina.com 34270 7744626 sunb0122 71114 91762 18506579548 yangmy0227 18791566645 13250565847 jiangvk1977 15250922291 37579 wangci1965 wangpq wango19850516 18531237843 18589683471 hegl1990 18714299209 zhant@gmail.com 29016 zhux1969 xiez1113 zhengtw 15490897758 18917969768 18149215132 18608279756 17149 18644736737 zhaov19880816 xuy@aol.com 5854142 18408454618 qinl1988 xubl1990 18852302621 18985792169 42928 18622907192 fanggs19960616@163.com 25150 47585 15086377222 617299 13447565144 668953950 4342797 18638890078 fangiq0615 zhuo19701127@ask.com 421528 15690966759 18058824538 18194138008 15770603106 heag0813 15793392577 470979 18402649812 1791830 18397977938 shukj19960528 87052642 chulu0119 jiangj1986 13210747847 47359 80638214 zhuey0405 94017 13636960571 18939715988 yuani0529@msn.com 58081 caodm1989 18420167788 13951603849 10205 164883 15513277337 29553 15011669462 87997 zhanji 29763568 13032055621 13617051541 lit
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值