Detailed explanation of H t p in p h p

Source: Internet
Author: User

For PHP files

 

Php can have html css javascript php script flash. Different parts of Php are executed in different places (servers and clients)

 

Http protocol

1. the http protocol is based on the TCP/IP protocol.

2. Our web Development Data Transmission relies on the http protocol.

3. http is short for Hypertext Transfer Protocol

 

 

Http Request

 

Basic Structure:

Request Line

Message Header

 

Message Body (entity content)

 

 

 

 


Accept text/html, application/xhtml + xml, application/xml;

Q = 0.9, */*; q = 0.8

Accept-Encoding gzip, deflate

Accept-Language zh-cn, zh; q = 0.8, en-us; q = 0.5, en; q = 0.3

Cache-Control max-age = 0

Connection keep-alive

Host localhost

User-Agent Mozilla/5.0 (Windows NT 6.1; WOW64; rv: 26.0) Gecko/20100101 Firefox/26.0

 

GET/http2.php HTTP/1.1

Host: localhost

User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv: 26.0) Gecko/20100101 Firefox/26.0

Accept: text/html, application/xhtml + xml, application/xml; q = 0.9, */*; q = 0.8

Accept-Language: zh-cn, zh; q = 0.8, en-us; q = 0.5, en; q = 0.3

Accept-Encoding: gzip, deflate

Referer: http: // localhost/test. php

Connection: keep-alive

 

 

 

Note:

GET override. php indicates to request resources in get Mode

Accept indicates that the client can receive any data

Accept-Language page Language

Accept-Encoding indicates the data compression format to be received.

Host

User-Agent tells us about the server kernel, operating system

Connection indicates that the Connection method should not be disconnected immediately.

Referrer indicates where I used anti-leech protection.

 

 

If I do not want access from 192.168.0.3 in http2.php

 

On the SERVER side, we can get the information we need through a $ _ SERVER.

 

Important:

HTTP_HOST = localhost

REMOTE_ADDR = 127.0.0.1 IP address used to access the page

DOCUMENT_ROOT = G:/zhentuan can obtain the main directory of apache.

REQUEST_URI =/http2.php can get the requested Resource Name

 

 

There are two main methods for http requests:

Get/post differences and connections

 

1. The security get request data is displayed in the address bar, And the post request data is placed in the http message body.

2. The data size that can be submitted is as follows:

The http protocol does not limit the data size.

The browser displays get and post requests. The get request data is not limited to 2 k + 35 post requests.

3. Get requests can be better added to favorites

 

Now we actually use some http requests to complete an anti-leech exercise.

 

If (isset ($ _ SERVER ['HTTP _ referer']) {

// Obtain

// Determine whether $ _ SERVER ['HTTP _ referer'] starts with HTTP: // localhost.

If (strpos ($ _ SERVER ['HTTP _ referer'], "HTTP: // localhost/...") = 0 ){

Echo "you can view information ";

} Else

{

// Jump to the warning page

Header ("Loaction: warning. php ");

}

} Else

{

// Jump to the warning page

Header ("Location: warning. php ");

}

 

 

Http Response

Location: http://www.baidu.org/index.php

HTTP/1.1 200 OK 200 OK indicates that the client request is successful

Server: Microsoft-IIs/5.0 indicates telling the Browser Server Status

Date: Thu, 13 Jul 2000 05:46:53 GMT tell the browser to request the page time

Content-Length 2291 indicates that the returned data contains 2291 bytes.

Content-Type: text/html document Type

Cache-control: private Cache

 

Description of Status Codes

 

Examples show how to apply http Response in practice

 

U 302 status code usage

 

For example, if we want to access the. php page and redirect it to page B automatically

 

Basic usage:

 

<? Php

// A 302 status code will be sent to the client, telling the browser to re-access B. php

// The header can write the information header to the http header-Send a raw HTTP header

Header ("Location: B. php ");

?>

 

Details: 302 status code can also jump to the Internet

 

U 404 status code usage

 

Generally, this page does not exist.

 

U 304 status code usage

 

 

Demonstrate how to control the browser to jump at a certain interval through http Response

 

<? Php

Header ("Refresh: 3; url = http://www.sohu.com ");

?>

 

 

 

 

 

Demonstrate how to control the page cache through http response. By default, the browser caches the page

 

// Use the header to disable caching (ajax)

Header ("Expires:-1 ");

Header ("Cache-control: no-cache ");

Header ("Pragma: no-cache ");

Echo "hellow cache ";

 

 

Http File Download

 

How files are downloaded

 

 

// Description of the Function

// 1. parameter description $ file_name file name

// 2. $ file_sub_dir: the sub-path of the downloaded file './xxx /'

Function down_file ($ file_name, $ file_sub_dir ){

// Click it to download an image.

// If the file is Chinese, the file name must be transcoded.

// Cause: php file function, which is relatively old and needs to be transcoded to Chinese GB2312

$ File_name = iconv ("UTF-8", "gb2312", $ file_name );

// 1. Use relative path

$ File_path = $ file_sub_dir. $ file_name;

// 2. Use absolute path

// $ File_path = $ _ SERVER ['document _ root']. "/down/". $ file_name;

// Open the file

If (! File_exists ($ file_path ))

{

Echo "the file does not exist ";

Return;

}

$ Fp = fopen ($ file_path, "r ");

// Obtain the downloaded file size

$ File_size = filesize ($ file_path );

 

// Returned File

Header ("Content-type: application/octet-stream ");

// Returns the value in bytes.

Header ("Accept-Ranges: bytes ");

// Returns the file size.

Header ("Accept-Length: $ file_size ");

// The pop-up Christmas box on the client, with the corresponding file name

Header ("Content-Disposition: attachment; filename =". $ file_name );

 

// Send data back to the client

$ Buffer = 1024;

// To ensure secure download, we 'd better make a file byte read counter

$ File_count = 0;

// This statement is used to determine whether the object is ended.

While (! Feof ($ fp) & ($ file_size-$ file_count> 0 )){

$ File_data = fread ($ fp, $ buffer );

// Count the number of bytes read

$ File_count + = $ buffer;

// Send some data back to the browser

Echo $ file_data;

}

// Close the file

Fclose ($ fp );

}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.