Summary of Protocol and encapsulation protocols supported by PHP (recommended)

Source: Internet
Author: User
Tags glob
This article mainly introduces to you about the PHP Support Protocol and encapsulation Protocol related Materials, the text through the sample code introduced in very detailed, for everyone to learn or use PHP has a certain reference learning value, the need for friends below with the small series to learn together.

Objective

Today's Web program development technology is really a contention, ASP, PHP, Jsp,perl, AJAX and so on. Regardless of how web technologies evolve in the future, it is important to understand the basic protocols for communicating between web programs, because it allows us to understand the internal work of Web applications.

PHP has a number of built-in URL-style encapsulation protocols that can be used for file system functions like fopen (), copy (), File_exists (), and FileSize (). In addition to these encapsulation protocols, a custom encapsulation protocol can be registered through Stream_wrapper_register ().

Note: The URL syntax used to describe an encapsulation protocol only supports scheme://... 's syntax. scheme:/ and scheme: The syntax is not supported.

PHP protocol type

    • file://-accessing the local file system

    • http://-access to HTTP (s) URLs

    • ftp://-access to FTP (s) URLs

    • php://-access to individual input/output streams (I/O streams)

    • zlib://-Compressed Stream

    • data://-data (RFC 2397)

    • glob://-Find matching file path mode

    • phar://-php Archive

    • Ssh2://-secure Shell 2

    • Rar://-rar

    • ogg://-Audio Stream

    • expect://-Processing of interactive streams

Ini

    • Allow_url_fopen:on default on this option is on, which activates the Fopen encapsulation protocol in the form of URLs so that URL object files can be accessed.

    • Allow_url_include:off is off by default, this option is on to allow the inclusion of URL object files, etc.

FILE://protocol

file://-access to the local file system, not affected by Allow_url_fopen and Allow_url_include

How to use

file://[Absolute path and filename of file]

http://127.0.0.1/code/1.php?file=file:///e:\phpstudy\www\code\phpinfo.php

PHP://protocol

php://-access to individual input/output streams (I/O streams)

No need to open allow_url_fopen, only Php://input, Php://stdin, php://memory, and php://temp need to turn on allow_url_include.

Php://stdin, Php://stdout and Php://stderr

Php://stdin, Php://stdout, and Php://stderr allow direct access to the appropriate input or output streams of the PHP process.

Php://stdin is read-only, php://stdout and Php://stderr are only written.

Php://stdin

<?php while ($line = fopen (' Php://stdin ', ' r ')) {//open The file pointer to the read from stdin Echo $line. " \ n "; Echo fgets ($line);//Read}?>

Php://stdout

<?php $fd = fopen (' php://stdout ', ' W '); if ($FD) {echo $fd. " \ n "; Fwrite ($FD, "test"); Fwrite ($FD, "\ n"); Fclose ($FD); }?>

Php://stderr

<?php $stderr = fopen (' Php://stderr ', ' W '); echo $stderr. " \ n "; Fwrite ($stderr, "Uknow"); Fclose ($stderr);? >

Php://filter

One of the most commonly used pseudo-protocols can be used to read arbitrary files.

Php://filter is a meta-wrapper designed for filtering and filtering applications when data flow is turned on. This is useful for all-in-one (all-in-one) file functions, such as ReadFile (), file (), and file_get_contents (), without the opportunity to apply other filters before the data stream content is read.

Parameters


name Description
resource=< data stream to be filtered > This parameter is required. It specifies the data stream that you want to filter for filtering.
Filter List of read=< read chain > This parameter is optional. You can set one or more filter names, separated by pipe characters.
write=< filter List of the Write chain > This parameter is optional. You can set one or more filter names, separated by pipe characters.
<; filter List of two chains > Any filter list that does not have a prefix of read= or write= will be applied to read or write chains as appropriate.

<?phpinclude ($_get[' file ')?>

http://127.0.0.1/code/1.php?file=php://filter/read=convert.base64-encode/resource=./phpinfo.php

Php://input

Php://input can access the read-only stream of the requested raw data and execute the data in the POST request as PHP code.

    • Allow_url_fopen:off/on

    • Allow_url_include:on

zip://, bzip2://, zlib://protocol

zip://, bzip2://, zlib://protocol in the case of double off can also be used normally;

zip://, bzip2://, zlib://all belong to the compressed stream, you can access the files in the compressed file, more importantly, you do not need to specify a suffix name.

    • Allow_url_fopen:off/on

    • Allow_url_include:off/on

How to use

Zip://archive.zip#dir/file.txt

zip://[File]#[in compressed file absolute path]

Test

The PHP code will be executed first to write the file name Phpcode.txt, the phpcode.txt zip compression, compressed file name File.zip, if you can upload a zip file to upload directly, If you cannot rename File.zip to File.jpg after uploading, several other compression formats can do the same.

Since # is ignored in the GET request, the URL is encoded as%23 when the GET request is used, and the relative path tested here is not possible, so only the absolute path can be used.

Http://127.0.0.1/code/1.php?file=zip://e:\phpstudy\www\code/1.zip%231.txt

DATA://protocol

DATA://protocol must be dual on to normal use;

    • Allow_url_fopen:on

    • Allow_url_include:on

http://127.0.0.1/code/1.php?file=data://text/plain,<?php phpinfo ()? >http://127.0.0.1/code/1.php?file=data ://text/plain;base64,pd9wahagcghwaw5mbygppz4=

GLOB://protocol

glob://-Find matching file path mode

<?php$it = new Directoryiterator ($_get[' file ']), foreach ($it as $f) {printf ("%s", $f->getfilename ()); Echo ' </ Br> '; }?>

EXPECT://protocol

expect://-Processing of interactive streams

The encapsulation protocol is not turned on by default

In order to use the expect://wrapper, you must install the»expect extension on the» pecl.

Usage

Expect://command

Attached: HTTP protocol is stateless and connection:keep-alive difference

Stateless means that the protocol has no memory capacity for transactions, and the server does not know what the client state is. On the other hand, there is no connection between opening a Web page on a server and the pages you have previously opened on this server

HTTP is a stateless, connection-oriented protocol, stateless does not mean that HTTP cannot maintain TCP connections, and it cannot represent HTTP using UDP protocol (no connection)

From http/1.1 onwards, the default is to open the keep-alive, to maintain the connection characteristics, in short, when a Web page opens, the client and server for the transmission of HTTP data between the TCP connection will not be closed, if the client again access to the Web page on this server, will continue to use this established connection

Keep-alive does not permanently keep the connection, it has a hold time and can be set in different server software (such as Apache) this time

Summarize

Reference

    • Php

    • Seven poses for command execution with PHP pseudo-protocol

Related Article

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.