Hướng dẫn dùng get stream trong PHP

Must be an associative array of associative arrays in the format $arr['wrapper']['option'] = $value, or null. Refer to context options for a list of available wrappers and options.

Defaults to null.

params

Must be an associative array in the format $arr['parameter'] = $value, or null. Refer to context parameters for a listing of standard stream parameters.

file_get_contents() is the preferred way to read the contents of a file into a string. It will use memory mapping techniques if supported by your OS to enhance performance.

Note:

If you're opening a URI with special characters, such as spaces, you need to encode the URI with urlencode().

Parameters

filename

Name of the file to read.

$filename0

Note:

The $filename1 constant can be used to trigger search. This is not possible if is enabled, since $filename1 is an int. Use $filename3 instead.

$filename4

A valid context resource created with stream_context_create(). If you don't need to use a custom context, you can skip this parameter by null.

$filename6

The offset where the reading starts on the original stream. Negative offsets count from the end of the stream.

Seeking ($filename6) is not supported with remote files. Attempting to seek on non-local files may work with small offsets, but this is unpredictable because it works on the buffered stream.

$filename8

Maximum length of data read. The default is to read until end of file is reached. Note that this parameter is applied to the stream processed by the filters.

Return Values

The function returns the read data or false on failure.

Warning

This function may return Boolean false, but may also return a non-Boolean value which evaluates to false. Please read the section on Booleans for more information. Use the === operator for testing the return value of this function.

Errors/Exceptions

An $use_include_path2 level error is generated if filename cannot be found, $filename8 is less than zero, or if seeking to the specified $filename6 in the stream fails.

When file_get_contents() is called on a directory, an $use_include_path2 level error is generated on Windows, and as of PHP 7.4 on other operating systems as well.

Changelog

VersionDescription8.0.0$filename8 is nullable now.7.1.0Support for negative $filename6s has been added.

Examples

Example #1 Get and output the source of the homepage of a website

$use_include_path9

Example #2 Searching within the include_path

false0

Example #3 Reading a section of a file

false1

The above example will output something similar to:

string(14) "lle Bjori Ro" 

Example #4 Using stream contexts

false2

false3

false4

Notes

Note: This function is binary-safe.

Tip

A URL can be used as a filename with this function if the have been enabled. See fopen() for more details on how to specify the filename. See the Supported Protocols and Wrappers for links to information about what abilities the various wrappers have, notes on their usage, and information on any predefined variables they may provide.

Warning

When using SSL, Microsoft IIS will violate the protocol by closing the connection without sending a false5 indicator. PHP will report this as "SSL: Fatal Protocol Error" when you reach the end of the data. To work around this, the value of should be lowered to a level that does not include warnings. PHP can detect buggy IIS server software when you open the stream using the false6 wrapper and will suppress the warning. When using fsockopen() to create an false7 socket, the developer is responsible for detecting and suppressing this warning.

Form là 1 đối tượng (hoặc cụ thể hơn là thẻ <form>) trong HTML dùng để nhận dữ liệu từ người dùng, giúp gửi yêu cầu của người dùng đến trang xử lý trong trang web.

Hướng dẫn dùng get stream trong PHP
Form đăng ký tài khoản

Thẻ <form> trong HTML là một container chứa các thành phần nhập liệu, các thành phần trong form gọi là Form Field như:

  • Text Field.
  • Password Field.
  • Check Box.
  • Button.
  • ...

Cú pháp: 

<form name="..." action="..." method="...">
    Các thành phần của form
</form>

Các thuộc tính của form:

  • name: tên của form.
  • action: chỉ định trang web sẽ nhận dữ liệu từ khi form có sự kiện click button submit.
  • <html>
    	<head></head>
    	<title>Demo Post and Get</title>
    	<body>
    		<form name="frmLogin" action="b.php" method="post">
    			<label>User Name: </label>
    			<input type="text" name="txtUserName"/><br/>
    			<label>Password: </label>
    			<input type="password" name="txtPassword" /><br/>
    			<input type="submit" value="Log in"/><br/>
    		</form>
    	</body>
    </html>
    0: xác định phương thức truyền dữ liệu (POST, GET).

Tìm hiểu POST và GET trong PHP

POST, GET là phương thức truyền dữ liệu trong form từ trang này qua trang khác và khối lượng dữ liệu truyền đi của form bị giới hạn bởi chiều dài tối đa của một URL (chiều dài tối đa của một URL là 2048 bytes và giới hạn này cũng phụ thuộc vào trình duyệt web).

Để truyền được dữ liệu trong form từ trang này sang trang khác có những yêu cầu sau:

  • Dữ liệu cần truyền phải nằm trong cặp thẻ
    <html>
    	<head></head>
    	<title>Demo Post and Get</title>
    	<body>
    		<form name="frmLogin" action="b.php" method="post">
    			<label>User Name: </label>
    			<input type="text" name="txtUserName"/><br/>
    			<label>Password: </label>
    			<input type="password" name="txtPassword" /><br/>
    			<input type="submit" value="Log in"/><br/>
    		</form>
    	</body>
    </html>
    1.
  • Dữ liệu cần truyền phải nhập vào các form field.
  • Dữ liệu chỉ được truyền đi khi nhấn button submit.

Để demo trong quá trình trình bày tạo 2 file

<html>
	<head></head>
	<title>Demo Post and Get</title>
	<body>
		<form name="frmLogin" action="b.php" method="post">
			<label>User Name: </label>
			<input type="text" name="txtUserName"/><br/>
			<label>Password: </label>
			<input type="password" name="txtPassword" /><br/>
			<input type="submit" value="Log in"/><br/>
		</form>
	</body>
</html>
2 là
<html>
	<head></head>
	<title>Demo Post and Get</title>
	<body>
		<form name="frmLogin" action="b.php" method="post">
			<label>User Name: </label>
			<input type="text" name="txtUserName"/><br/>
			<label>Password: </label>
			<input type="password" name="txtPassword" /><br/>
			<input type="submit" value="Log in"/><br/>
		</form>
	</body>
</html>
3 và
<html>
	<head></head>
	<title>Demo Post and Get</title>
	<body>
		<form name="frmLogin" action="b.php" method="post">
			<label>User Name: </label>
			<input type="text" name="txtUserName"/><br/>
			<label>Password: </label>
			<input type="password" name="txtPassword" /><br/>
			<input type="submit" value="Log in"/><br/>
		</form>
	</body>
</html>
4 trong thư mục
<html>
	<head></head>
	<title>Demo Post and Get</title>
	<body>
		<form name="frmLogin" action="b.php" method="post">
			<label>User Name: </label>
			<input type="text" name="txtUserName"/><br/>
			<label>Password: </label>
			<input type="password" name="txtPassword" /><br/>
			<input type="submit" value="Log in"/><br/>
		</form>
	</body>
</html>
5.

Hướng dẫn dùng get stream trong PHP


Trong đó file

<html>
	<head></head>
	<title>Demo Post and Get</title>
	<body>
		<form name="frmLogin" action="b.php" method="post">
			<label>User Name: </label>
			<input type="text" name="txtUserName"/><br/>
			<label>Password: </label>
			<input type="password" name="txtPassword" /><br/>
			<input type="submit" value="Log in"/><br/>
		</form>
	</body>
</html>
3 sẽ có một form chứa các trường dữ liệu là
<html>
	<head></head>
	<title>Demo Post and Get</title>
	<body>
		<form name="frmLogin" action="b.php" method="post">
			<label>User Name: </label>
			<input type="text" name="txtUserName"/><br/>
			<label>Password: </label>
			<input type="password" name="txtPassword" /><br/>
			<input type="submit" value="Log in"/><br/>
		</form>
	</body>
</html>
7 và
<html>
	<head></head>
	<title>Demo Post and Get</title>
	<body>
		<form name="frmLogin" action="b.php" method="post">
			<label>User Name: </label>
			<input type="text" name="txtUserName"/><br/>
			<label>Password: </label>
			<input type="password" name="txtPassword" /><br/>
			<input type="submit" value="Log in"/><br/>
		</form>
	</body>
</html>
8 do người dùng nhập vào. File
<html>
	<head></head>
	<title>Demo Post and Get</title>
	<body>
		<form name="frmLogin" action="b.php" method="post">
			<label>User Name: </label>
			<input type="text" name="txtUserName"/><br/>
			<label>Password: </label>
			<input type="password" name="txtPassword" /><br/>
			<input type="submit" value="Log in"/><br/>
		</form>
	</body>
</html>
4 sẽ lấy các thông tin từ form bên trang 
<html>
	<head></head>
	<title>Demo Post and Get</title>
	<body>
		<form name="frmLogin" action="b.php" method="post">
			<label>User Name: </label>
			<input type="text" name="txtUserName"/><br/>
			<label>Password: </label>
			<input type="password" name="txtPassword" /><br/>
			<input type="submit" value="Log in"/><br/>
		</form>
	</body>
</html>
3 truyền sang và hiển thị lên trình duyệt khi nhất button submit. Để nhận dữ liệu từ form truyền qua sử dụng biến toàn cục trong PHP.

  • Nếu dùng phương thức POST:
    <html>
    	<head>
    	</head>
    	<body>
    		<?php
    			$username = $_POST['txtUserName'];
    			$password = $_POST['txtPassword'];
    			echo "<p>UserName : ".$username."</p>";
    			echo "<p>Password : ".$password."</p>";
    		?>
    	</body>
    </html>
    1.
  • Nếu dùng phương thức GET:
    <html>
    	<head>
    	</head>
    	<body>
    		<?php
    			$username = $_POST['txtUserName'];
    			$password = $_POST['txtPassword'];
    			echo "<p>UserName : ".$username."</p>";
    			echo "<p>Password : ".$password."</p>";
    		?>
    	</body>
    </html>
    2.
  • Nếu không xác định được phương thức truyền:
    <html>
    	<head>
    	</head>
    	<body>
    		<?php
    			$username = $_POST['txtUserName'];
    			$password = $_POST['txtPassword'];
    			echo "<p>UserName : ".$username."</p>";
    			echo "<p>Password : ".$password."</p>";
    		?>
    	</body>
    </html>
    3.

Giống nhau

Phương thức POST và GET đều là cơ chế truyền dữ liệu trong form từ trang này sang khác.

Khác nhau

Phương thức POST

Các dữ liệu của form được truyền ngầm và dữ liệu truyền đi không phụ thuộc vào URL.

Ưu điểm
  •  Bảo mật hơn phương thức GET.
  •  Không giới hạn dung lượng dữ liệu truyền đi.
Nhược điểm

 Dữ liệu truyền đi không tường mình (cơ chế truyền ngầm định) do đó sẽ phát sinh lỗi tiềm ẩn.

Demo 

File

<html>
	<head></head>
	<title>Demo Post and Get</title>
	<body>
		<form name="frmLogin" action="b.php" method="post">
			<label>User Name: </label>
			<input type="text" name="txtUserName"/><br/>
			<label>Password: </label>
			<input type="password" name="txtPassword" /><br/>
			<input type="submit" value="Log in"/><br/>
		</form>
	</body>
</html>
3 sử dụng phương thức POST:

<html>
	<head></head>
	<title>Demo Post and Get</title>
	<body>
		<form name="frmLogin" action="b.php" method="post">
			<label>User Name: </label>
			<input type="text" name="txtUserName"/><br/>
			<label>Password: </label>
			<input type="password" name="txtPassword" /><br/>
			<input type="submit" value="Log in"/><br/>
		</form>
	</body>
</html>

Kết quả khi chạy trên trình duyệt file

<html>
	<head></head>
	<title>Demo Post and Get</title>
	<body>
		<form name="frmLogin" action="b.php" method="post">
			<label>User Name: </label>
			<input type="text" name="txtUserName"/><br/>
			<label>Password: </label>
			<input type="password" name="txtPassword" /><br/>
			<input type="submit" value="Log in"/><br/>
		</form>
	</body>
</html>
3

Hướng dẫn dùng get stream trong PHP

File

<html>
	<head></head>
	<title>Demo Post and Get</title>
	<body>
		<form name="frmLogin" action="b.php" method="post">
			<label>User Name: </label>
			<input type="text" name="txtUserName"/><br/>
			<label>Password: </label>
			<input type="password" name="txtPassword" /><br/>
			<input type="submit" value="Log in"/><br/>
		</form>
	</body>
</html>
4 sử dụng biến toàn cục 
<html>
	<head>
	</head>
	<body>
		<?php
			$username = $_POST['txtUserName'];
			$password = $_POST['txtPassword'];
			echo "<p>UserName : ".$username."</p>";
			echo "<p>Password : ".$password."</p>";
		?>
	</body>
</html>
7 để lấy dữ liệu từ form truyền sang.

<html>
	<head>
	</head>
	<body>
		<?php
			$username = $_POST['txtUserName'];
			$password = $_POST['txtPassword'];
			echo "<p>UserName : ".$username."</p>";
			echo "<p>Password : ".$password."</p>";
		?>
	</body>
</html>

Nhập

<html>
	<head>
	</head>
	<body>
		<?php
			$username = $_POST['txtUserName'];
			$password = $_POST['txtPassword'];
			echo "<p>UserName : ".$username."</p>";
			echo "<p>Password : ".$password."</p>";
		?>
	</body>
</html>
8 và
<html>
	<head>
	</head>
	<body>
		<?php
			$username = $_POST['txtUserName'];
			$password = $_POST['txtPassword'];
			echo "<p>UserName : ".$username."</p>";
			echo "<p>Password : ".$password."</p>";
		?>
	</body>
</html>
9.

Nhấn submit sẽ thấy dữ liệu được truyền ngầm định mà không hiện lên ở URL.

Hướng dẫn dùng get stream trong PHP

Phương thức GET

Các dữ liệu đường truyền đi được hiển thị lên URL của trình duyệt và dữ liệu truyền đi phụ thuộc vào độ dài đối đa của URL.

Ưu điểm
  • Người dùng có thể bookmark lại địa chỉ URL.
  • Người dùng có thể giả lập lại phương thức GET để truyền dữ liệu mà không cần thông qua form (sử dụng query string).
Nhược điểm
  • Không thích hợp truyền dữ liệu có tính bảo mật như password (do dữ liệu có thể xuất hiện rõ ràng trên trình duyệt web).
  • Dung lượng dữ liệu truyền đi có giới hạn (phụ thuộc vào độ dài tối đa của URL).
Demo

File a sử dụng phương thức GET

<html>
	<head></head>
	<title>Demo Post and Get</title>
	<body>
		<form name="frmLogin" action="b.php" method="get">
			<label>User Name: </label>
			<input type="text" name="txtUserName"/><br/>
			<label>Password: </label>
			<input type="password" name="txtPassword" /><br/>
			<input type="submit" value="Log in"/> <br/>
		</form>
	</body>
</html>

File

<html>
	<head></head>
	<title>Demo Post and Get</title>
	<body>
		<form name="frmLogin" action="b.php" method="post">
			<label>User Name: </label>
			<input type="text" name="txtUserName"/><br/>
			<label>Password: </label>
			<input type="password" name="txtPassword" /><br/>
			<input type="submit" value="Log in"/><br/>
		</form>
	</body>
</html>
4 sử dụng biến toàn cục 
<html>
	<head></head>
	<title>Demo Post and Get</title>
	<body>
		<form name="frmLogin" action="b.php" method="get">
			<label>User Name: </label>
			<input type="text" name="txtUserName"/><br/>
			<label>Password: </label>
			<input type="password" name="txtPassword" /><br/>
			<input type="submit" value="Log in"/> <br/>
		</form>
	</body>
</html>
2 để lấy dữ liệu từ form truyền sang.

<html>
	<head>
	</head>
	<body>
		<?php
			$username = $_GET['txtUserName'];
			$password = $_GET['txtPassword'];
			echo "<p>UserName : ".$username."</p>";
			echo "<p>Password : ".$password."</p>";
		?>
	</body>
</html>

Chạy file

<html>
	<head></head>
	<title>Demo Post and Get</title>
	<body>
		<form name="frmLogin" action="b.php" method="post">
			<label>User Name: </label>
			<input type="text" name="txtUserName"/><br/>
			<label>Password: </label>
			<input type="password" name="txtPassword" /><br/>
			<input type="submit" value="Log in"/><br/>
		</form>
	</body>
</html>
3 sau đó nhập nội dung như trường hợp sử dụng phương thức POST sẽ thấy kết quả là các dữ liệu được truyền đi được hiển thị lên URL của trình duyệt. Lưu ý là dữ liệu truyền đi là có giới hạn phụ thuộc vào độ dài của URL (tối đa 2048 bytes).