PHP常用函数

1.产生随机字符串函数

<?php
function random($length) {
    $hash = '';
    $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
    $max = strlen($chars) – 1;
    mt_srand((double)microtime() * 1000000);
    for($i = 0; $i < $length; $i++) {
        $hash .= $chars[mt_rand(0, $max)];
    }
    return $hash;
}

2.取得客户端IP地址

function getIp(){
    if(getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"),"unknown")){
        $ip = getenv("HTTP_CLIENT_IP");
    }elseif(getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")){
        $ip = getenv("HTTP_X_FORWARDED_FOR");
    }elseif(getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) {
        $ip = getenv("REMOTE_ADDR");
    }elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'],"unknown")){
        $ip = $_SERVER['REMOTE_ADDR'];
    }else {
        $ip = 'unkown';
    }
    return $ip;
}

3、创建文件目录

function createdir($dir = '')
{

    if (!is_dir($dir)) {

        $temp = explode('/', $dir);

        $cur_dir = '';

        for ($i = 0; $i < count($temp); $i++) {

            $cur_dir .= $temp[$i] . '/';

        }

        if (!is_dir($cur_dir)) {

            @mkdir($cur_dir, 07777);

        }

    }

}

4.判断邮箱地址

function checkEmail($inAddress){
    return (ereg("^([a - zA - Z0 - 9_ -])+@([a - zA - Z0 - 9_ -])+(\.[a - zA - Z0 - 9_ -])+",$inAddress));
}

5.跳转

function gotourl($message = "",$url = "",$title = "")
{
    $html = "<html><head>";
    $html .="<meta http-equiv=’refresh’ content=\"3;url=’".$url."‘\">";
    $html .="<link href=’../templates/style.css’ type=text/css rel=stylesheet>";
    $html .="</head><body><br><br><br><br>";
    $html .="<table cellspacing=’0′ cellpadding=’0′ border=’1′ width=’450′ align=’center’>";
    $html .="<tr><td bgcolor=’#ffffff’>";
    $html .="<table border=’1′ cellspacing=’1′ cellpadding=’4′ width=’100%’>";
    $html .="<tr class=’m_title’>";
    $html .="<td>".$title."</td></tr>";
    $html .="<tr class=’line_1′><td align=’center’ height=’60′>";
    $html .="<br>".$message."<br><br>";
    if (!empty($url)){
        $html .="系统将在3秒后返回<br>如果您的浏览器不能自动返回,请点击[<a href=".$url." target=_self>这里</a>]进入";
    }else{
        $html .="[<a href='#' onclick='history.go(-1)'>返回</a>]";
        $html .="</td></tr></table></td></tr></table>";
        $html .="</body></html>";
    }
    echo $html;
    exit;
}

 

6.Whois query using PHP ——利用PHP获取Whois请求 

function whois_query($domain)
{
    // fix the domain name:
    $domain = strtolower(trim($domain));
    $domain = preg_replace('/^http:\/\//i', '', $domain);
    $domain = preg_replace('/^www\./i', '', $domain);
    $domain = explode('/', $domain);
    $domain = trim($domain[0]);
    // split the TLD from domain name
    $_domain = explode('.', $domain);
    $lst = count($_domain) - 1;
    $ext = $_domain[$lst];
    // You find resources and lists
    // like these on wikipedia:
    //
    // <a href="http://de.wikipedia.org/wiki/Whois">http://de.wikipedia.org/wiki/Whois</a>
    //

    $servers = array(
        "biz" => "whois.neulevel.biz",
        "com" => "whois.internic.net",
        "us" => "whois.nic.us",
        "coop" => "whois.nic.coop",
        "info" => "whois.nic.info",
        "name" => "whois.nic.name",
        "net" => "whois.internic.net",
        "gov" => "whois.nic.gov",
        "edu" => "whois.internic.net",
        "mil" => "rs.internic.net",
        "int" => "whois.iana.org",
        "ac" => "whois.nic.ac",
        "ae" => "whois.uaenic.ae",
        "at" => "whois.ripe.net",
        "au" => "whois.aunic.net",
        "be" => "whois.dns.be",
        "bg" => "whois.ripe.net",
        "br" => "whois.registro.br",
        "bz" => "whois.belizenic.bz",
        "ca" => "whois.cira.ca",
        "cc" => "whois.nic.cc",
        "ch" => "whois.nic.ch",
        "cl" => "whois.nic.cl",
        "cn" => "whois.cnnic.net.cn",
        "cz" => "whois.nic.cz",
        "de" => "whois.nic.de",
        "fr" => "whois.nic.fr",
        "hu" => "whois.nic.hu",
        "ie" => "whois.domainregistry.ie",
        "il" => "whois.isoc.org.il",
        "in" => "whois.ncst.ernet.in",
        "ir" => "whois.nic.ir",
        "mc" => "whois.ripe.net",
        "to" => "whois.tonic.to",
        "tv" => "whois.tv",
        "ru" => "whois.ripn.net",
        "org" => "whois.pir.org",
        "aero" => "whois.information.aero",
        "nl" => "whois.domain-registry.nl"
    );


    if (!isset($servers[$ext])) {
        die('Error: No matching nic server found!');
    }

    $nic_server = $servers[$ext];
    $output = '';
    // connect to whois server:

    if ($conn = fsockopen($nic_server, 43)) {
        fputs($conn, $domain . "\r\n");
        while (!feof($conn)) {
            $output .= fgets($conn, 128);
        }
        fclose($conn);
    } else {
        die('Error: Could not connect to ' . $nic_server . '!');
    }
    return $output;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值