【php开源代码栏目提醒】:网学会员鉴于大家对php开源代码十分关注,论文会员在此为大家搜集整理了“Http.class.php”一文,供大家参考学习
<?php
// +----------------------------------------------------------------------
// | Think
PHP // +----------------------------------------------------------------------
// | Copyright (c) 2008 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------
// $Id$
/**
+------------------------------------------------------------------------------
* Http 宸ュ叿绫?
* 鎻愪緵涓绯诲垪鐨凥ttp鏂规硶
+------------------------------------------------------------------------------
* @category ORG
* @package ORG
* @subpackage Net
* @author liu21st <liu21st@gmail.com>
* @version $Id$
+------------------------------------------------------------------------------
*/
class Http extends Base
{//绫诲畾涔夊紑濮?
/**
+----------------------------------------------------------
* 涓嬭浇鏂囦欢
* 鍙互鎸囧畾涓嬭浇鏄剧ず鐨勬枃浠跺悕锛屽苟鑷姩鍙戦佺浉搴旂殑Header淇℃伅
* 濡傛灉鎸囧畾浜哻ontent鍙傛暟锛屽垯涓嬭浇璇ュ弬鏁扮殑鍐呭
+----------------------------------------------------------
* @static
* @access public
+----------------------------------------------------------
* @param string $filename 涓嬭浇鏂囦欢鍚?
* @param string $showname 涓嬭浇鏄剧ず鐨勬枃浠跺悕
* @param string $content 涓嬭浇鐨勫唴瀹?
* @param integer $expire 涓嬭浇鍐呭娴忚鍣ㄧ紦瀛樻椂闂?
+----------------------------------------------------------
* @return void
+----------------------------------------------------------
* @throws ThinkExecption
+----------------------------------------------------------
*/
static function download ($filename, $showname='',$content='',$expire=180) {
if(file_exists($filename)) {
$length = filesize($filename);
}elseif(is_file(UPLOAD_PATH.$filename)) {
$filename = UPLOAD_PATH.$filename;
$length = filesize($filename);
}elseif($content != '') {
$length = strlen($content);
}else {
throw_exception($filename.L('涓嬭浇鏂囦欢涓嶅瓨鍦紒'));
}
if(empty($showname)) {
$showname = $filename;
}
$showname = basename($showname);
if(empty($filename)) {
$type = mime_content_type($filename);
}else{
$type = "application/octet-stream";
}
//鍙戦丠ttp Header淇℃伅 寮濮嬩笅杞?
header("Pragma: public");
header("Cache-control: max-age=".$expire);
//header('Cache-Control: no-store, no-cache, must-revalidate');
header("Expires: " . gmdate("D, d M Y H:i:s",time()+$expire) . "GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s",time()) . "GMT");
header("Content-Disposition: attachment; filename=".$showname);
header("Content-Length: ".$length);
header("Content-type: ".$type);
header('Content-Encoding: none');
header("Content-Transfer-Encoding: binary" );
if($content == '' ) {
readfile($filename);
}else {
echo($content);
}
exit();
}
/**
+----------------------------------------------------------
* 鏄剧ずHTTP Header 淇℃伅
+----------------------------------------------------------
* @return string
+----------------------------------------------------------
*/
static function get_header_info($header='',$echo=true)
{
ob_start();
$headers = getallheaders();
if(!empty($header)) {
$info = $headers[$header];
echo($header.':'.$info."\n"); ;
}else {
foreach($headers as $key=>$val) {
echo("$key:$val\n");
}
}
$output = ob_get_clean();
if ($echo) {
echo (nl2br($output));
}else {
return $output;
}
}
/**
* HTTP Protocol defined status codes
* @param int $num
*/
static function send_http_status($code) {
static $_status = array(
// Informational 1xx
100 => 'Continue',
101 => 'Switching Protocols',
// Success 2xx
200 => 'OK',
201 => 'Created',
202 => 'Accepted',
203 => 'Non-Authoritative Information',
204 => 'No Content',
205 => 'Reset Content',
206 => 'Partial Content',
// Redirection 3xx
300 => 'Multiple Choices',
301 => 'Moved Permanently',
302 => 'Found', // 1.1
303 => 'See Other',
304 => 'Not Modified',
305 => 'Use Proxy',
// 306 is deprecated but reserved
307 => 'Temporary Redirect