Cuando estamos haciendo un proyecto muy grande y necesitamos capturar varios elementos GET, POST y SESSION, se nos vuelve complicado hacer un ISSET a cada uno, asignarlos a una variable o inicializarlos. Es por ello que muestro una clase que hice para poder capturar todos estos elementos y generar sus respectivas variables, con posibilidad de hacer debug.
<?php
/**
*
* @author Horacio Romero Mendez (angelos)
* @License Copyleft 2011
* @since Apr 4, 2011 3:36:21 PM
* @Internal GNU/Linux Arch 2010.05 Notebook
*
*/
class getVars {
private static $arreglo = array();
public static function getAll($debug=false){
self::getGET($debug);
self::getSESSION($debug);
self::getPOST($debug);
return (self::$arreglo);
}
public static function getGET($debug=false){
foreach($_GET as $campo => $texto){
self::$arreglo["tmp_$campo"] = $texto;
if ($debug) echo "\$tmp_".$campo."='".$texto."';";
}
return (self::$arreglo);
}
public static function getSESSION($debug=false){
@session_start();
foreach($_SESSION as $campo => $texto){
self::$arreglo["tmp_$campo"] = $texto;
if ($debug) echo "\$tmp_".$campo."='".$texto."';";
}
return (self::$arreglo);
}
public static function getPOST($debug=false){
foreach($_POST as $campo => $texto){
self::$arreglo["tmp_$campo"] = $texto;
if ($debug) echo "\$tmp_".$campo."='".$texto."';";
}
return (self::$arreglo);
}
}
?>
<?PHP //INCLUIMOS LA CLASE include_once(“getVars.php”); //PARA ATRAPAR POST extract(getVars::getPOST(), EXTR_OVERWRITE); //PARA ATRAPAR GET extract(getVars::getGET(), EXTR_OVERWRITE); //PARA ATRAPAR SESSION extract(getVars::getSESSION(), EXTR_OVERWRITE); //PARA ATRAPAR TODOS extract(getVars::getAll(), EXTR_OVERWRITE); //SI QUEREMOS MOSTRAR LAS VARIABLES QUE CACHAMOS, AGREGAMOS UN true extract(getVars::getPOST(true), EXTR_OVERWRITE); ?>
<input type="text" name="usuario"/>
1 Comentario
Formularios en PHP – Angelinux · 25 noviembre, 2016 a las 17:56
[…] Obtener post, get y session facilmente en PHP […]