Skip to content

Instantly share code, notes, and snippets.

@krismas
Last active August 3, 2019 16:37
Show Gist options
  • Save krismas/2853625 to your computer and use it in GitHub Desktop.
Save krismas/2853625 to your computer and use it in GitHub Desktop.
A small MODX snippet to extract POST, GET, SESSION & COOKIE values
<?php
/*
* A small MODX snippet to extract POST, GET, SESSION & COOKIE values - (c) 2012-2016 ackwa.fr
*
* @version : 1.0.4
* @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
* 23/11/16 - 1.0.4 - Correction initialisation $sDefaults
*/
$sDefault = $modx->getOption('default' , $scriptProperties, '');
$sDefaults = $modx->getOption('defaults', $scriptProperties, $sDefault);
$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] : ''));
@Mark-H
Copy link

Mark-H commented Aug 31, 2012

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.)

@silentworks
Copy link

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.

@krismas
Copy link
Author

krismas commented Aug 31, 2012

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment