龙盟编程博客 | 无障碍搜索 | 云盘搜索神器
快速搜索
主页 > web编程 > php编程 >

php 用php实现vpn用户随机生成

时间:2014-07-17 02:00来源:网络整理 作者:网络 点击:
分享到:
用php实现vpn用户随机生成 [代码片段(77行)]
<?php

session_start();

@$ComeUrl = $_SERVER['HTTP_REFERER'];
$MyHost = "outofmemory.cn
$file = '/etc/ppp/chap-secrets';
$getime = 2;

//随机生成字符.我这里用户是5位数,密码是8位数
function get_randstr( $length = 8 ) {
    // 密码字符集,可任意添加你需要的字符
    $chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789';

    $str = '';
    for ( $i = 0; $i < $length; $i++ )
    {
        // 这里提供两种字符获取方式
        // 第一种是使用 substr 截取$chars中的任意一位字符;
        // 第二种是取字符数组 $chars 的任意元素
        // $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        $str .= $chars[ mt_rand(0, strlen($chars) - 1) ];
    }

    return $str;
}

//写入vpn账号密码到ppp的密码文件
function write_user( $user,$password ) {
    global $file;
    $fd = fopen($file,'a+');
    if(!$fd)
        exit(1);
    $str = "$user pptpd $password *\\n";

    $re = fwrite($fd,$str);
    if(!$re)
        exit(2);
    echo "your user : $user";
    echo "<br />";
    echo "your password : $password ";
    fclose($fd);
    @$_SESSION['time']++;
    return 0;
}

function get_host( $url ) {
    $arr = explode('/',$url,4);
    if(@$arr[2])
        return $arr[2];
}       

//如果不是从我网站过来.就告诉他要去我网站逛逛.
$host = get_host($ComeUrl);
//print "$host = $MyHost <br />";

if(@$_SESSION['time'] > $getime){
    echo "你已经获取过 ".($getime+1)." 次,请休息一下吧!";
    echo "<br />";
    echo "随便看看? -> <a href=http://$MyHost>$MyHost</a>";
    exit(4);
}

if($host != $MyHost) {
    echo "you come from $ComeUrl";
    echo "<br />";
    echo "but you must go My site";
    echo " <a href=http://$MyHost>$MyHost</a>";
} else {
    $user = get_randstr(5);
    $password = get_randstr(11);
    $re = write_user($user,$password);
}

?>
//该片段来自于http://outofmemory.cn
精彩图集

赞助商链接