pods_url_variable
(Since 1.6.2) -- retrieve a GET, POST, SESSION, or URI segment variable
function pods_url_variable($key = 'last', $type = 'uri')
Retrieve the sanitized value from one of the following:
- $_GET
- $_POST
- $_SESSION
- a URI segment
Parameters
| Parameter | Type | Details |
|---|---|---|
| $key | MIXED | the URL segment (when $type = "uri"), OR the variable name |
| $type | STRING | (optional) "uri", "get", "post", or "session" |
The $key Parameter
- When type = "uri":
- $key can be either "first", "last", or an integer - in the case of an integer it follows the standard PHP array keying starting at 0 for the first value, 1 for the second, 2 for the third, and so on.. with the exception of negative numbers which start from the last and move forward
- To get the first URI variable: $key = "first" or $key = 0
- To get the last URI variable: $key = "last" or $key = -1
- To get the second URI variable: $key = 1
- To get the second to last URI variable: $key = -2
- When type = "get", "post", or "session", $key is the variable name
'last' / Negative $key URI Caveats
There are a number of caveats to using 'last' as your key, we recommend you use the index number such as 0 for the first, 1 for the second, or 2 for the third section of the url. Working with index numbers ensures you always get the part of the URL you are trying to get. Otherwise, if you have a Pod Page with the URI of "mypage/other/*" then someone accessing "mypage/other/awesome/cool" will give you "cool" if you use the 'last' $key, vs "awesome" if you're using the index number 2.
Returns
String, or false.Examples
<?php
/*
Sample URL: http://yoursite.com/us/virginia/alexandria/?thevar=28
*/
// All of these output "alexandria"
echo pods_url_variable(2);
echo pods_url_variable();
echo pods_url_variable('last');
echo pods_url_variable(-1);
// All of these output "us"
echo pods_url_variable(0);
echo pods_url_variable('first');
echo pods_url_variable(-3);
// All of these output "virginia"
echo pods_url_variable(1);
echo pods_url_variable(-2);
// This outputs "28"
echo pods_url_variable('thevar', 'get');
?>
