文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>常用函数

常用函数

时间:2010-12-25  来源:ly4885806

/**
 * 将常用日期格式转化为unix时间戳
 */
function GmmktimeFromDate($date) {
    $temp1=explode(" ",$date);
    $temp2=explode("-",$temp1[0]);
    $temp3=explode(":",$temp1[1]);
    if($temp2[0]<=0) $temp2[0]=0;
    if($temp2[1]<=0) $temp2[1]=0;
    if($temp2[2]<=0) $temp2[2]=0;
    if($temp3[0]<=0) $temp3[0]=0;
    if($temp3[1]<=0) $temp3[1]=0;
    if($temp3[2]<=0) $temp3[2]=0;
    return @mktime($temp3[0],$temp3[1],$temp3[2],$temp2[1],$temp2[2],$temp2[0]);
}


/**
 * 函数功能:下载网络图片,返回值:数组(包括图片路径,大小,宽*高)
 */
function GrabImage($url,$i) {
    ob_start();
    if($url=="") {
        return false;
    }
    $ext=strrchr($url,".");
    if($ext!=".gif" && $ext!=".jpg" && $ext!=".png") {
        return false;
    }
    $filename=$i.$ext;
    readfile($url);
    $img = ob_get_contents();
    ob_end_clean();
    $size = strlen($img);
    $fp2=@fopen("getimage/".$filename, "a"); //此处可加上图片存放路径
    fwrite($fp2,$img);
    fclose($fp2);
    $imgwh=getimagesize("getimage/".$filename);
    return array($filename,$size,"{$imgwh[0]}*{$imgwh[1]}");
}


/**
 * 下载文件到本地
 */
function download($swf_url) {
    $destination_folder = 'swf/'; // 文件夹保存下载文件。必须以斜杠结尾
    $newfname = time().basename($swf_url);
    $file = @fopen ($swf_url, "rb");
    if ($file) {
        $newf = @fopen ($destination_folder.$newfname, "wb");
        if ($newf)
            while(!feof($file)) {
                fwrite($newf, fread($file, 1024 * 8 ), 1024 * 8 );
            }
    }else {
        return false;
    }
    if ($file) {
        fclose($file);
    }
    if ($newf) {
        fclose($newf);
    }
    return $newfname;
}


/**
 * 获取用户的真实IP地址
 *
 */
function get_real_ip() {
    $ip=false;
    if(!empty($_SERVER["HTTP_CLIENT_IP"])) {
        $ip = $_SERVER["HTTP_CLIENT_IP"];
    }
    if (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
        $ips = explode (", ", $_SERVER['HTTP_X_FORWARDED_FOR']);
        if ($ip) {
            array_unshift($ips, $ip);
            $ip = FALSE;
        }
        for ($i = 0; $i < count($ips); $i++) {
            if (!eregi ("^(10|172\.16|192\.168)\.", $ips[$i])) {
                $ip = $ips[$i];
                break;
            }
        }
    }
    return ($ip ? $ip : $_SERVER['REMOTE_ADDR']);
}


/**
 * 获取URL内容
 */
function get_page_content($url) {
    $url = eregi_replace('^http://', '', $url);
    $temp = explode('/', $url);
    $host = array_shift($temp);
    $path = '/'.implode('/', $temp);
    $temp = explode(':', $host);
    $host = $temp[0];
    $port = isset($temp[1]) ? $temp[1] : 80;
    $fp = @fsockopen($host, $port, &$errno, &$errstr, 30);
    if ($fp) {
        @fputs($fp, "GET $path HTTP/1.1\r\nHost: $host\r\nAccept: */*\r\nReferer:$url\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)\r\nConnection: Close\r\n\r\n");
    }
    $Content = "";
    while ($str = @fread($fp, 4096)) {
        $Content .= $str;
    }
    @fclose($fp);
    //重定向
    if(preg_match("/^HTTP\/\d.\d 301 Moved Permanently/is",$Content)) {
        if(preg_match("/Location:(.*?)\r\n/is",$Content,$murl)) {
            return get_page_content($murl[1]);
        }
    }
    //读取内容
    if(preg_match("/^HTTP\/\d.\d 200 OK/is",$Content)) {
        preg_match("/Content-Type:(.*?)\r\n/is",$Content,$murl);
        $contentType=trim($murl[1]);
        $Content=explode("\r\n\r\n",$Content,2);
        $Content=$Content[1];
    }
    return $Content;
}


相关阅读 更多 +
排行榜 更多 +
辰域智控app

辰域智控app

系统工具 下载
网医联盟app

网医联盟app

运动健身 下载
汇丰汇选App

汇丰汇选App

金融理财 下载