How to read page array in php

WBOY
Release: 2023-05-11 09:52:36
Original
540 people have browsed it

PHP is a server-side programming language widely used in website development. Its flexibility and easy scalability are highly praised by developers in the industry. In the process of web development, we often need to read and process data in the page, among which array is a common data structure. This article will introduce how PHP reads arrays in the page and give actual code examples.

1. Get the page array

Normally, we need to open a page or file and read out the array in order to operate on it. PHP provides a convenient way to use the serialize() function in a page to serialize and save the array contents to a file in the form of a string, and then use in another page The file_get_contents() function reads the file contents and deserializes it into an array using the unserialize() function.

For example, we have an array:

$cars = array("Volvo", "BMW", "Toyota");
Copy after login

We can serialize it and save it to a file:

$serialized_cars = serialize($cars);
file_put_contents('cars.txt', $serialized_cars);
Copy after login

Then in another page, we can read file and deserialize it into an array:

$serialized_cars = file_get_contents('cars.txt');
$cars = unserialize($serialized_cars);
Copy after login

Now, the variable $cars contains the serialized array contents. We can use the print_r() function to output the array:

print_r($cars);
Copy after login

The output result is as follows:

Array
(
    [0] => Volvo
    [1] => BMW
    [2] => Toyota
)
Copy after login

2. Get the POST array in the page

During the form submission process, we usually use the POST method to send data to the server, and the data will be stored in the $_POST variable in the form of an array. To get these POST data in PHP, simply access the corresponding key of the $_POST array.

For example, we have a login form with username and password:

<form method="post" action="login.php">
    <input type="text" name="username">
    <input type="password" name="password">
    <input type="submit" value="Login">
</form>
Copy after login

Here we use the POST method to send data to the login.php page and get the username and password in that page:

$username = $_POST['username'];
$password = $_POST['password'];
Copy after login

3. Get the GET array in the page

The GET array is similar to the POST array, but the data is sent as part of the URL. Similarly, the method of obtaining the GET array is also very simple, we only need to access the corresponding key from the $_GET array.

For example, we have a URL that contains a parameter named "id":

http://example.com/page.php?id=123
Copy after login

In the page.php page, we can get the value of this parameter:

$id = $_GET['id'];
Copy after login

Note that when using the GET method to send data to the server, the data will appear in the URL in clear text, so sensitive information (such as passwords, etc.) should not be sent through the GET method.

4. Get the COOKIE array in the page

COOKIE is a technology that saves data in the client (such as a browser). It is usually used to save user session information, login status, etc. . To get COOKIE array in PHP we can use $_COOKIE array.

For example, we have a COOKIE named "username":

setcookie("username", "John Doe", time() + 3600); // 设置COOKIE,有效期为1小时
Copy after login

In the page, we can get the value of the COOKIE:

$username = $_COOKIE['username'];
Copy after login

5. Get the page The SESSION array

SESSION is a technology for saving data on the server side. It is usually used to save user session information, login status, etc. To get SESSION array in PHP we can use $_SESSION array.

For example, we set a SESSION named "loggedin" in the login page:

session_start();
$_SESSION['loggedin'] = true;
Copy after login

In other pages, we can get the value of the SESSION:

session_start();
$loggedin = isset($_SESSION['loggedin']) ? $_SESSION['loggedin'] : false;
Copy after login

Here, we use the isset() function to check whether the SESSION exists. If it exists, assign its value to the variable $loggedin, otherwise set it to false.

Summary

This article introduces how PHP reads arrays in the page and gives actual code examples. Whether getting data from serialized files, POST, GET, COOKIE or SESSION array, PHP provides a simple way to easily read and manipulate various data in the page. At the same time, we also need to pay attention to security, do not send sensitive information through the GET method, and correctly set the validity period of COOKIE and SESSION, etc.

The above is the detailed content of How to read page array in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template