In this article we'll learn how to use variables and filter variables in thinkphp.
In the webhttp://www.aliyun.com/zixun/aggregation/17799.html "> 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.