Actions

J1.5

Difference between revisions of "Retrieving and Filtering GET and POST requests with JRequest::getVar"

From Joomla! Documentation

m
m (Rearranged contents)
(17 intermediate revisions by 8 users not shown)
Line 1: Line 1:
{{stub}}
+
{{Notice|In Joomla! 1.7 {{JVer|1.7}} and newer, <code>JRequest</code> has been superseded by <code>JInput</code>. See [[Retrieving request data using JInput]].}}
  
 +
When writing any web application, it is crucial that you filter input data before using it. Joomla! provides a filtering library to help you accomplish this.
  
== Summary ==
+
You can access the filtered request data using the <code>JRequest</code> class. Even though PHP allows you to access the data from the request using the superglobal arrays <code>$_GET</code>, <code>$_POST</code> and <code>$_REQUEST</code>, it is highly recommended to use <code>JRequest</code> '''instead of''' these superglobals. By using <code>JRequest</code> properly, you make sure that the data has the right format and its default value makes sense. This can prevent serious security holes such as SQL injection vulnerabilities.
  
When writing any web application, it is crucial that you filter input data before using it. Joomla! provides a set of filtering libraries to help you accomplish this.
+
===Defined in===
 +
libraries\joomla\environment\request.php
  
 +
==Methods==
  
== JRequest functions ==
+
=== 'getVar' ===
 +
To retrieve GET/POST request data, Joomla! uses the getVar method of the JRequest class (JRequest::getVar()).
  
Under most circumstances, you will want to use member functions the JRequest class to get request variables. The most common case is when you want to use a specific variable found in either the GET or POST portion of the HTTP request. If you have a form variable named 'address', you would want to use this code to get it:
+
'''Retrieving Data'''
  
<pre>
+
If you have a form variable named 'address', you would want to use this code to get it:
 +
<source lang="php">
 
$address = JRequest::getVar('address');
 
$address = JRequest::getVar('address');
</pre>
+
</source>
  
By setting $address this way, getVar() strips out all HTML and trailing whitespace. If you want to specify a default value in the event that 'address' is not in the request or is unset, use this code:
+
Unless other parameters are set, all HTML and trailing whitespace will be filtered out.
  
<pre>
+
'''The DEFAULT Parameter'''
$address = JRequest::getVar('address', 'default value goes here');
+
 
</pre>
+
If you want to specify a default value in the event that 'address' is not in the request or is unset, use this code:
 +
 
 +
<source lang="php">
 +
$address = JRequest::getVar('address', 'Address is empty');
 +
echo $address;  // Address is empty
 +
</source>
 +
 
 +
'''The SOURCE Parameter'''
  
 
Frequently, you will expect your variable to be found in a specific portion of the HTTP request (POST, GET, etc...). If this is the case, you should specify which portion; this will slightly increase your extension's security. If you expect 'address' to only be in POST, use this code to enforce that:
 
Frequently, you will expect your variable to be found in a specific portion of the HTTP request (POST, GET, etc...). If this is the case, you should specify which portion; this will slightly increase your extension's security. If you expect 'address' to only be in POST, use this code to enforce that:
  
<pre>
+
<source lang="php">
 
$address = JRequest::getVar('address', 'default value goes here', 'post');
 
$address = JRequest::getVar('address', 'default value goes here', 'post');
</pre>
+
</source>
  
The fourth parameter of getVar() can be used to specify certain filters to force validation of specific value types for the variable. Here is a list of types you can validate:
+
'''The VARIABLE TYPE Parameter'''
 +
 
 +
The fourth parameter of getVar() can be used to specify certain filters to force validation of specific value types for the variable.  
 +
 
 +
<source lang="php">
 +
$address = JRequest::getVar('address', 'default value goes here', 'post','variable type');
 +
</source>
 +
 
 +
Here is a list of types you can validate:
  
 
*INT
 
*INT
Line 43: Line 63:
 
*PATH
 
*PATH
 
*USERNAME
 
*USERNAME
 +
 +
'''The FILTER MASK Parameter'''
  
 
Finally, there are some mask constants you can pass in as the fifth parameter that allow you to bypass portions of the filtering:
 
Finally, there are some mask constants you can pass in as the fifth parameter that allow you to bypass portions of the filtering:
 +
<source lang="php">
 +
$address = JRequest::getVar('address', 'default value goes here', 'post','validation type',mask type);
 +
</source>
  
 
*JREQUEST_NOTRIM - prevents trimming of whitespace
 
*JREQUEST_NOTRIM - prevents trimming of whitespace
 
*JREQUEST_ALLOWRAW - bypasses filtering
 
*JREQUEST_ALLOWRAW - bypasses filtering
 
*JREQUEST_ALLOWHTML - allows most HTML. If this is not passed in, HTML is stripped out by default.
 
*JREQUEST_ALLOWHTML - allows most HTML. If this is not passed in, HTML is stripped out by default.
 +
 +
Note. These are static variables not strings. Do not use quotes around them
 +
 +
=== 'get' ===
 +
To receive a whole array filtered. If you would want to get the POST data, you can use this.
 +
<source lang="php">JRequest::get( 'post' )</source>
 +
<nowiki>This returns the standard POST array.  You can use it on a template page if needed, or in the models section if convenient. It returns the most recent POST.
 +
Methods of this object were not found in the Framework section where one would expect to find them. </nowiki>
 +
 +
==See also==
 +
* [http://api.joomla.org/1.5/Joomla-Framework/Environment/JRequest.html JRequest on api.joomla.org]
 +
* [[JURI]]
 +
* [[JRoute]]
  
 
[[Category:Development]]
 
[[Category:Development]]

Revision as of 14:15, 14 July 2012

Replacement filing cabinet.png
This Namespace has been archived - Please Do Not Edit or Create Pages in this namespace. Pages contain information for a Joomla! version which is no longer supported. It exists only as a historical reference, will not be improved and its content may be incomplete.
Info non-talk.png
General Information

In Joomla! 1.7 Joomla 1.7 and newer, JRequest has been superseded by JInput. See Retrieving request data using JInput.

When writing any web application, it is crucial that you filter input data before using it. Joomla! provides a filtering library to help you accomplish this.

You can access the filtered request data using the JRequest class. Even though PHP allows you to access the data from the request using the superglobal arrays $_GET, $_POST and $_REQUEST, it is highly recommended to use JRequest instead of these superglobals. By using JRequest properly, you make sure that the data has the right format and its default value makes sense. This can prevent serious security holes such as SQL injection vulnerabilities.

Contents

Defined in

libraries\joomla\environment\request.php

Methods

'getVar'

To retrieve GET/POST request data, Joomla! uses the getVar method of the JRequest class (JRequest::getVar()).

Retrieving Data

If you have a form variable named 'address', you would want to use this code to get it:

$address = JRequest::getVar('address');

Unless other parameters are set, all HTML and trailing whitespace will be filtered out.

The DEFAULT Parameter

If you want to specify a default value in the event that 'address' is not in the request or is unset, use this code:

$address = JRequest::getVar('address', 'Address is empty');
echo $address;  // Address is empty

The SOURCE Parameter

Frequently, you will expect your variable to be found in a specific portion of the HTTP request (POST, GET, etc...). If this is the case, you should specify which portion; this will slightly increase your extension's security. If you expect 'address' to only be in POST, use this code to enforce that:

$address = JRequest::getVar('address', 'default value goes here', 'post');

The VARIABLE TYPE Parameter

The fourth parameter of getVar() can be used to specify certain filters to force validation of specific value types for the variable.

$address = JRequest::getVar('address', 'default value goes here', 'post','variable type');

Here is a list of types you can validate:

  • INT
  • INTEGER
  • FLOAT
  • DOUBLE
  • BOOL
  • BOOLEAN
  • WORD
  • ALNUM
  • CMD
  • BASE64
  • STRING
  • ARRAY
  • PATH
  • USERNAME

The FILTER MASK Parameter

Finally, there are some mask constants you can pass in as the fifth parameter that allow you to bypass portions of the filtering:

$address = JRequest::getVar('address', 'default value goes here', 'post','validation type',mask type);
  • JREQUEST_NOTRIM - prevents trimming of whitespace
  • JREQUEST_ALLOWRAW - bypasses filtering
  • JREQUEST_ALLOWHTML - allows most HTML. If this is not passed in, HTML is stripped out by default.

Note. These are static variables not strings. Do not use quotes around them

'get'

To receive a whole array filtered. If you would want to get the POST data, you can use this.

JRequest::get( 'post' )

This returns the standard POST array. You can use it on a template page if needed, or in the models section if convenient. It returns the most recent POST. Methods of this object were not found in the Framework section where one would expect to find them.

See also