Home > Backend Development > PHP Tutorial > What are the common super global variables in PHP programming?

What are the common super global variables in PHP programming?

WBOY
Release: 2023-06-12 10:52:02
Original
1474 people have browsed it

Super global variables are a very important concept in PHP. They can access variable values ​​anywhere in the program without using functions or other methods to pass variables. In this article, we will discuss several super global variables commonly used in PHP programming.

  1. $_GET

$_GET is one of the super global variables used to collect data submitted by HTML forms. Through $_GET, we can obtain the query string parameters in the specified URL. These parameters can be used for operations such as data filtering or data query on the page.

For example, when the user enters their username and password in the form, the URL submitted by the form may look like this:

http://example.com/login.php?username =xxx&password=yyy

In this case, we can get the value of username and password through $_GET, as shown below:

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

Similar to $_GET, $_POST is also a super global variable used to collect data submitted by HTML forms. But the difference is that $_POST is used in the POST request method, which can avoid data being stored in the URL, making it more secure.

Same example, this time the form uses the POST method:

<form method="POST" action="login.php">
    <input type="text" name="username">
    <input type="password" name="password">
    <button type="submit">登录</button>
</form>
Copy after login

The data of this form will be submitted to login.php. In login.php, we can use $_POST To get the data in the form:

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

$_REQUEST includes all the contents of $_GET, $_POST and $_COOKIE, and can be obtained from GET and POST request and data in cookie. Although it can get all the data, due to security issues, it is recommended that data filtering be performed when using $_REQUEST to avoid security issues such as request forgery and SQL injection.

  1. $_SESSION

$_SESSION is one of the super global variables in PHP used to store user session data. Through $_SESSION, we can save some user data to the server after the user visits a page, and continue to use this data in subsequent visits.

When using $_SESSION, we need to start the session first and operate the data in the session through the $_SESSION array. For example, the following code snippet can save the user ID to $_SESSION:

session_start();
$_SESSION['user_id'] = 1234;
Copy after login

In subsequent requests, we can use the following code to get the user ID saved in $_SESSION:

session_start();
$user_id = $_SESSION['user_id'];
Copy after login
  1. $_COOKIE

$_COOKIE is one of the super global variables in PHP used to store HTTP Cookies. HTTP Cookie is some key-value pair information sent to the client by the server through the HTTP response header, which can be used to store some data related to the current session.

Unlike the data in $_SESSION, which is stored on the server side, the data in $_COOKIE is stored on the client side, and it can be read by the client through JavaScript and other technologies.

The data in $_COOKIE can be obtained through the following code:

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

Summary

Super global variables are a very important part of PHP programming, and the above five types appear. When using these variables, you should follow rules such as data filtering and security to avoid security attacks. In actual use, it is recommended to choose variables that suit you as needed to write a program.

The above is the detailed content of What are the common super global variables in PHP programming?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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