public
Last active

A small MODX snippet to extract POST, GET, SESSION & COOKIE values

  • Download Gist
argv.php
PHP
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
<?php
/*
* A small MODX snippet to extract POST, GET, SESSION & COOKIE values - (c) 2012-2013 ackwa.fr
*
* @version : 1.0.3
* @see : https://gist.github.com/gists/2853625
* @name : argv.php
* @author : g.noel@ackwa.fr
* @usage : [[!argv?key=`myparam`&default=`1`]] -> return the value
* [[!argv?key=`myparam`&default=`1`&toph=`1`]] -> set [[+argv.myparam]]
* [[!argv?key=`myparam`&from=`GP`]] -> return the value from GET or POST
* [[!argv?key=`key1,key2`]] -> set [[+argv.key1]] & [[+argv.key2]]
* [[!argv?key=`*`]] -> set all [[+argv.xxx]]
*
* @history : 31/05/12 - 1.0.0 - Initial revision
* 31/08/12 - 1.0.1 - Add a source(s) priority parameter (default PGSC)
* 21/09/12 - 1.0.2 - Change default initialization
* 07/11/13 - 1.0.3 - Add support for more than one key
*/
$sDefault = $modx->getOption('default' , $scriptProperties, '');
$sDefaults = $modx->getOption('defaults', $scriptProperties, $sDefaults);
$sKeys = $modx->getOption('key' , $scriptProperties, '');
$sKeys = $modx->getOption('keys' , $scriptProperties, $sKeys);
$bPHolder = $modx->getOption('toph' , $scriptProperties, false);
$sFrom = $modx->getOption('from' , $scriptProperties, 'PGSC');
$bDebug = false;
 
/*
* Initialisations
*/
$bMulti = false;
$lKeys = array();
$lDefaults = array();
$lValues = array();
 
/*
* Data sources
*/
$lSources = array('P' => '_POST', 'G' => '_GET', 'S' => '_SESSION', 'C' => '_COOKIE');
 
/*
* Test requested key
*/
if ($sKeys) {
/*
* Get all data. In this mode, for security, SESSION data are filtered
*/
if ('*' == $sKeys) {
foreach(str_split($sFrom) as $sSource) {
if ('S' != $sSource) $lKeys = array_merge($lKeys, array_keys($GLOBALS[$lSources[$sSource]]));
}
}
else {
$lKeys = explode(',', $sKeys);
$lDefaults = explode(',', $sDefaults);
}
 
/*
* Extract value(s)
*/
foreach($lKeys as $iKey => $sKey) {
if ($sKey) {
$lValues[$sKey] = $lDefaults[$iKey];
foreach(str_split($sFrom) as $sSource) {
if (isset($GLOBALS[$lSources[$sSource]][$sKey])) {
$lValues[$sKey] = $GLOBALS[$lSources[$sSource]][$sKey];
break;
}
}
}
}
if ($bDebug) {echo '<pre style="font-size:9px;">';print_r($lValues);echo '</pre>';}
}
$bMulti = (count($lValues) > 1);
 
/*
* Test output mode : return value or set placeholder
*/
if ($bPHolder || $bMulti) {
$modx->toPlaceholders($lValues, 'argv');
$lValues = array();
}
 
return (($bPHolder || $bMulti) ? '' : ($sKeys ? $lValues[$sKeys] : ''));

Would be nice to give the user the option to define where they want to get the value from - from the POST, GET, SESSION or COOKIE. Or maybe do that optionally.

(BTW: if you name the file argv.php in the gist, it will add syntax highlighting for php.)

What happens if the key I am looking for exist in $_SESSION, $_COOKIE and $_POST, but I want the one from $_COOKIE? Its best to do as @Mark-H presented that the user should be given the option to define where they want the value from.

@Mark-H, @silentworks, Thanks for the comments. This new version support a "source(s) priority" parameter (default PGSC).

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.