DokuWiki

It's better when it's simple

User Tools

Site Tools


devel:request_vars

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
devel:request_vars [2012-07-06 13:08] – created andidevel:request_vars [2023-09-20 23:27] (current) Klap-in
Line 1: Line 1:
 ====== Accessing Request Variables ====== ====== Accessing Request Variables ======
  
-[[deveonly]] 
  
-DokuWiki does not prohibit access to the ''$_POST'', ''$_GET'' and ''$_REQUEST'' super globals. However we strongly recommend to access them **not** directly, but instead use our [[xref>Input]] class. A global instance of that class is available everywhere as [[xref>$INPUT]].+DokuWiki does not prohibit access to the ''$_POST'', ''$_GET''''$_REQUEST'' and ''$_SERVER'' super globals. However we strongly recommend to access them **not** directly, but instead use our [[xref>Input]] class. A global instance of that class is available everywhere as [[xref>$INPUT]].
  
 The class gives you type safe access to the request variables, makes sure they are correctly initialized and allows you to set defaults. The class gives you type safe access to the request variables, makes sure they are correctly initialized and allows you to set defaults.
Line 11: Line 10:
 If you want to access ''$_GET'' or ''$_POST'' explicitly, call the methods on the ''get'' and ''post'' members of ''$INPUT''. Eg. ''%%$INPUT->get->int('foo')%%'' or ''%%$INPUT->post->int('foo')%%''. If you want to access ''$_GET'' or ''$_POST'' explicitly, call the methods on the ''get'' and ''post'' members of ''$INPUT''. Eg. ''%%$INPUT->get->int('foo')%%'' or ''%%$INPUT->post->int('foo')%%''.
  
-All the access function allow for a second parameter to set a default value. This value will be returned when the variable was not set or had the wrong type (eg. an expected integer was an array instead). All access function have reasonable defaults matching the type of the function. Eg. ''int()'' returns ''0''''arr()'' returns an empty array as default.+Access to ''$_SERVER'' variables is always through the ''server'' member of ''$INPUT'': Eg. ''%%$INPUT->server->str('REMOTE_USER')%%''.
  
-A third parameter tells the function if an [[phpfn>empty]] parameter should be returned as default as well. This third parameter defaults to ''false''.+All the access functions allow for a second parameter to set a default value. This value will be returned when the variable was not set or had the wrong type (eg. an expected integer was an array instead). All access functions have reasonable defaults matching the type of the function. Eg. ''int()'' returns ''0'', ''arr()'' returns an empty array as default. 
 + 
 +A third parameter tells the function if an [[phpfn>empty()]] parameter should be returned as default as well. This third parameter defaults to ''false''.
  
 ===== Examples ===== ===== Examples =====
  
-Here are a couple of examples to give you an idea how the Input class works. For detailed info, please refer to [[xref>inc/Input.class.php]].+Here are a couple of examples to give you an idea how the Input class works. For detailed info, please refer to [[xref>inc/Input/Input.php]].
  
 <code php> <code php>
 global $INPUT; global $INPUT;
  
-$_REQUEST = array('foo' => '3');+$_REQUEST = ['foo' => '3'];
  
 var_dump($INPUT->int('foo')); // expect an integer var_dump($INPUT->int('foo')); // expect an integer
Line 42: Line 43:
 // array(0) { // array(0) {
 // } // }
 +
 +var_dump($INPUT->has('foo')); // is set
 +// bool(true)
  
  
-$_POST = array('foo' => '0', 'bar' => '');+$_POST = ['foo' => '0', 'bar' => ''];
  
 // now accessing $_POST and trying the third parameter  // now accessing $_POST and trying the third parameter 
Line 59: Line 63:
 var_dump($INPUT->post->int('bar', -1, true)); var_dump($INPUT->post->int('bar', -1, true));
 // int(-1) // int(-1)
 +
 +
 +$_SERVER 
 +
 +var_dump($INPUT->server->str('REMOTE_USER'));
 +// string(8) "username"
 </code> </code>
devel/request_vars.1341572914.txt.gz · Last modified: 2012-07-06 13:08 by andi

Except where otherwise noted, content on this wiki is licensed under the following license: CC Attribution-Share Alike 4.0 International
CC Attribution-Share Alike 4.0 International Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki