header函数的详细使用说明

不管页面有多少header,它会执行最后一个,不过是有条件的,例如:
header('Location:http://www.jb51.net');
header('Location:http://www.g.cn');
header('Location:http://www.baidu.com');
这个就会跳到百度
header('Location:http://www.jb51.net');echo '脚本之家';
header('Location:http://www.g.cn');
header('Location:http://www.baidu.com');
这个就会跳到google

 

下面是关于header函数的详细使用说明
一、作用:
~~~~~~~~~
PHP只是以HTTP协议将HTML文档的标头送到浏览器,告诉浏览器具体怎么处理这个页面,至于传送的内容则需要熟悉一下HTTP协议了,与PHP无关了
传统的标头一定包含下面三种标头之一,并只能出现一次。
Location: xxxx:yyyy/zzzz
Content-Type: xxxx/yyyy
Status: nnn xxxxxx

 

二、先来了解一下HTTP协议的运作方式
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
HTTP协议是基于请求/响应范式的。一个客户机与服务器建立连接后,发送一个请求给服务器,请求方式的格式为,统一资源标识符、协议版本号,后边是 MIME信息包括请求修饰符、客户机信息和可能的内容。服务器接到请求后,给予相应的响应信息,其格式为一个状态行包括信息的协议版本号、一个成功或错误的代码,后边是MIME信息包括服务器信息、实体信息和可能的内容。
它分四个过程,在HTTP协议中,服务端是指提供HTTP服务的部分,客户端是指你使用的浏览器或者下载工具等等。在通讯时,由客户端发出请求连接,服务端建立连接;然后,客户端发出HTTP请求(Request),服务端返回响应信息(Respond),由此完成一个HTTP操作。

 

三、HTTP协议状态码表示的意思
~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1××  保留
2××  表示请求成功地接收
3××  为完成请求客户需进一步细化请求
4××  客户错误
5××  服务器错误

 

四、操作实例:
~~~~~~~~~~~~~
<1> 重定向功能,这种最常见
<?php
Header("Location: http://www.php.net");
?>

 

<2> 强制用户每次访问这个页面时获取最新资料,而不是使用存在客户端的缓存。
<?php
//告诉浏览器此页面的过期时间(用格林威治时间表示),只要是已经过去的日期即可。
header("Expires: Mon, 26 Jul 1970 05:00:00 GMT");
//告诉浏览器此页面的最后更新日期(用格林威治时间表示)也就是当天,目的就是强迫浏览器获取最新资料
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT");
//告诉客户端浏览器不使用缓存
header("Cache-Control: no-cache, must-revalidate");
//参数(与以前的服务器兼容),即兼容HTTP1.0协议
header("Pragma: no-cache");
//输出MIME类型
header("Content-type: application/file");
//文件长度
header("Content-Length: 227685");
//接受的范围单位
header("Accept-Ranges: bytes");
//缺省时文件保存对话框中的文件名称
header("Content-Disposition: attachment; filename=$filename");
?>

 

<3> 输出状态值到浏览器,主要用于访问权限控制
<?php
header('HTTP/1.1 401 Unauthorized');
header('status: 401 Unauthorized');
?>
比如要限制一个用户不能访问该页,则可设置状态为404,如下所示,这样浏览器就显示为即该页不存在
<?php
header('HTTP/1.1 404 Not Found');
header("status: 404 Not Found");
?>

注意: 传统的标头一定包含下面三种标头之一,并只能出现一次。 Content-Type: xxxx/yyyy Location: xxxx:yyyy/zzzz Status: nnn xxxxxx 在新的多型标头规格 (Multipart MIME) 方可以出现二次以上。

 

使用范例

范例一: 本例使浏览器重定向到 PHP 的官方网站。
Header("Location: http://www.php.net"); exit; >?
范例二: 要使用者每次都能得到最新的资料,而不是 Proxy 或 cache 中的资料,可以使用下列的标头
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . "GMT"); header("Cache-Control: no-cache, must-revalidate");
header("Pragma: no-cache"); >?

范例三: 让使用者的浏览器出现找不到档案的信息。
header("Status: 404 Not Found"); >?

范例四:让使用者下载档案。
header("Content-type: application/x-gzip");
header("Content-Disposition: attachment; filename=文件名");
header("Content-Description: PHP3 Generated Data"); >?

详细出处参考:http://www.jb51.net/article/20887.htm

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
libjpeg是一种开源的JPEG图像压缩库,它提供了一些函数和工具,可以在应用程序中使用它来读取、写入和压缩JPEG图像。 1. 安装libjpeg库 在使用libjpeg之前,需要先安装libjpeg库。可以使用以下命令在Ubuntu系统中安装: ``` sudo apt-get install libjpeg-dev ``` 2. 读取JPEG图像 使用libjpeg库读取JPEG图像需要以下步骤: - 定义一个jpeg_decompress_struct结构体,用于存储JPEG图像的信息和解压缩后的图像数据。 - 定义一个jpeg_error_mgr结构体,用于处理错误。 - 使用jpeg_create_decompress函数初始化jpeg_decompress_struct结构体。 - 使用jpeg_stdio_src函数将JPEG文件的文件指针关联到jpeg_decompress_struct结构体。 - 使用jpeg_read_header函数读取JPEG文件的头信息。 - 使用jpeg_start_decompress函数开始解压缩JPEG文件。 - 使用jpeg_read_scanlines函数逐行读取JPEG文件的像素数据。 - 使用jpeg_finish_decompress函数完成解压缩过程。 - 使用jpeg_destroy_decompress函数销毁jpeg_decompress_struct结构体。 以下是使用libjpeg库读取JPEG图像的示例代码: ```c #include <stdio.h> #include <jpeglib.h> int main() { struct jpeg_decompress_struct cinfo; struct jpeg_error_mgr jerr; FILE *infile; JSAMPARRAY buffer; int row_stride; if ((infile = fopen("image.jpg", "rb")) == NULL) { printf("Can't open image file\n"); return 0; } cinfo.err = jpeg_std_error(&jerr); jpeg_create_decompress(&cinfo); jpeg_stdio_src(&cinfo, infile); jpeg_read_header(&cinfo, TRUE); jpeg_start_decompress(&cinfo); row_stride = cinfo.output_width * cinfo.output_components; buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); while (cinfo.output_scanline < cinfo.output_height) { jpeg_read_scanlines(&cinfo, buffer, 1); // process the data } jpeg_finish_decompress(&cinfo); jpeg_destroy_decompress(&cinfo); fclose(infile); return 0; } ``` 3. 写入JPEG图像 使用libjpeg库写入JPEG图像需要以下步骤: - 定义一个jpeg_compress_struct结构体,用于存储JPEG图像的信息和压缩后的图像数据。 - 定义一个jpeg_error_mgr结构体,用于处理错误。 - 使用jpeg_create_compress函数初始化jpeg_compress_struct结构体。 - 使用jpeg_stdio_dest函数将JPEG文件的文件指针关联到jpeg_compress_struct结构体。 - 使用jpeg_set_defaults函数设置JPEG图像的默认参数。 - 使用jpeg_set_quality函数设置JPEG图像的压缩质量。 - 使用jpeg_start_compress函数开始压缩JPEG图像。 - 使用jpeg_write_scanlines函数逐行写入JPEG图像的像素数据。 - 使用jpeg_finish_compress函数完成压缩过程。 - 使用jpeg_destroy_compress函数销毁jpeg_compress_struct结构体。 以下是使用libjpeg库写入JPEG图像的示例代码: ```c #include <stdio.h> #include <jpeglib.h> int main() { struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; FILE *outfile; JSAMPROW row_pointer[1]; int row_stride; if ((outfile = fopen("output.jpg", "wb")) == NULL) { printf("Can't open output file\n"); return 0; } cinfo.err = jpeg_std_error(&jerr); jpeg_create_compress(&cinfo); jpeg_stdio_dest(&cinfo, outfile); cinfo.image_width = 640; cinfo.image_height = 480; cinfo.input_components = 3; cinfo.in_color_space = JCS_RGB; jpeg_set_defaults(&cinfo); jpeg_set_quality(&cinfo, 80, TRUE); jpeg_start_compress(&cinfo, TRUE); row_stride = cinfo.image_width * 3; while (cinfo.next_scanline < cinfo.image_height) { row_pointer[0] = &image_buffer[cinfo.next_scanline * row_stride]; jpeg_write_scanlines(&cinfo, row_pointer, 1); } jpeg_finish_compress(&cinfo); jpeg_destroy_compress(&cinfo); fclose(outfile); return 0; } ``` 4. 压缩JPEG图像 使用libjpeg库压缩JPEG图像需要以下步骤: - 定义一个jpeg_compress_struct结构体,用于存储JPEG图像的信息和压缩后的图像数据。 - 定义一个jpeg_error_mgr结构体,用于处理错误。 - 使用jpeg_create_compress函数初始化jpeg_compress_struct结构体。 - 使用jpeg_stdio_dest函数将JPEG文件的文件指针关联到jpeg_compress_struct结构体。 - 使用jpeg_set_defaults函数设置JPEG图像的默认参数。 - 使用jpeg_set_quality函数设置JPEG图像的压缩质量。 - 使用jpeg_start_compress函数开始压缩JPEG图像。 - 使用jpeg_write_scanlines函数逐行写入JPEG图像的像素数据。 - 使用jpeg_finish_compress函数完成压缩过程。 - 使用jpeg_destroy_compress函数销毁jpeg_compress_struct结构体。 以下是使用libjpeg库压缩JPEG图像的示例代码: ```c #include <stdio.h> #include <jpeglib.h> int main() { struct jpeg_compress_struct cinfo; struct jpeg_error_mgr jerr; FILE *infile, *outfile; JSAMPARRAY buffer; int row_stride; if ((infile = fopen("image.jpg", "rb")) == NULL) { printf("Can't open image file\n"); return 0; } if ((outfile = fopen("output.jpg", "wb")) == NULL) { printf("Can't open output file\n"); fclose(infile); return 0; } cinfo.err = jpeg_std_error(&jerr); jpeg_create_compress(&cinfo); jpeg_stdio_dest(&cinfo, outfile); jpeg_read_header(&cinfo, TRUE); jpeg_start_compress(&cinfo, TRUE); row_stride = cinfo.image_width * cinfo.input_components; buffer = (*cinfo.mem->alloc_sarray)((j_common_ptr) &cinfo, JPOOL_IMAGE, row_stride, 1); while (cinfo.next_scanline < cinfo.image_height) { jpeg_read_scanlines(&cinfo, buffer, 1); jpeg_write_scanlines(&cinfo, buffer, 1); } jpeg_finish_compress(&cinfo); jpeg_destroy_compress(&cinfo); fclose(infile); fclose(outfile); return 0; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值