php常用经典函数代码

/*生成随机字符函数*/
function random($length) {
    $hash="";
    $chars="#@#&*abcdefghijklmnopqrstuvwxyz1234567890";
    $max=strlen($chars)-1;
    mt_srand((double)microtime(null)*1000000); //自PHP4.2.0起不再需要使用mt_srand()或srand(),生成随机数种子,可直接使用rand()自动生成
    for($i=0;$i<$length;$i++){
        $hash.=$chars[mt_rand(0,$max)];
    }
    return $hash;
}

/*取得用户IP*/
function getip(){
    if(getenv("http_client_ip")&&strcasecmp(getenv("http_client_ip"), "unknown")){
        $ip=getenv("http_client_ip");
    }else if(getenv("http_x_forwarded_for")&&strcasecmp(getenv("http_x_forwarded_for"),"unknown")){
        $ip=getenv("http_x_forwarded_for");
    }else if(getenv("remote_addr")&&strcasecmp(getenv("remote_addr"),"unknown"))
        $ip=getenv("remote_addr");
    else if(isset($_SERVER['REMOTE_ADDR'])&&$_SERVER['REMOTE_ADDR']&&strcasecmp($_SERVER['REMOTE_ADDR'],"unknown"))
        $ip=$_SERVER['REMOTE_ADDR'];
    else $ip="unknown";
    return ($ip);
}
echo "<br/>".getip();

发表评论