Thinkphp 3.1 QuickStart: Variables

Source: Internet
Author: User
Keywords Variables QuickStart thinkphp
Tags access action class aliyun control cookie data default default value

In this article we'll learn how to use variables and filter variables in thinkphp.
In the web "> development process, we often need to obtain system variables or user-submitted data, these variables are complex data, and inadvertently easy to cause security risks , but if you take advantage of the variables that thinkphp provides, you can easily gain and harness variables.

Get variables

First, let's talk about how to get variables.
The first way: the traditional way of acquiring
You can still use the traditional way to get various system variables during the development process, such as:

$id = $_get[' id '];  Gets the Get variable $name = $_post[' name ']; Gets the post variable $value = $_session[' var ']; Gets the session variable $name = $_cookie[' name ']; Gets the cookie variable $file = $_server[' php_self ']; Get the server variable

It is not recommended to use the traditional method directly, because there is no unified security processing mechanism, if the later adjustment, it will be more trouble to change.
The second way: using the dynamic method provided by the action class
The system's action class provides enhanced access to system variables, including the GET, POST, put, REQUEST, session, COOKIE, server, and Globals parameters, in addition to obtaining variable values, also provides variable filtering and default value support, simple to use , just call the following method in the action:

$id = $this->_get (' id '); Gets the Get variable $name = $this->_post (' name '); Gets the post variable $value = $this->_session (' var '); Gets the session variable $name = $this->_cookie (' name '); Gets the cookie variable $file = $this->_server (' php_self '); Get the server variable

The calling format is:
$this-> Method Name (variable name, [filter method],[default value])
Method names can support:

Method name meaning _get gets the get parameter _post gets the post parameter _param automatically determines the request type gets get, post or put parameter _request gets the request parameter _put gets the put parameter _session fetch $_sessi On parameter _cookie get $_cookie parameter _server get $_server parameter _globals get $globals parameter

Variable name: (required) is the name of the system variable to get
Filter method: (optional) You can use any built-in function or custom function name, if not specified, the default Htmlspecialchars function for security filtering (configured by the Default_filter parameter), the parameter is the previous method name to get the value, This means that if you call:

$this->_get ("name");

The result of the final call is Htmlspecialchars ($_get["name"), and if you want to change the filtering method, you can use:

$this->_get ("name", "Strip_tags");

Default value: (optional) is the default value that is set if the parameter variable you want to get does not exist, for example:

$this->_get ("id", "strip_tags", 0);

If $_get["id" does not exist, it returns 0.

If no default value is set, the system returns null by default.

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: 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.