* @copyright 2011 Bartolomé Sintes Marco * @license http://www.gnu.org/licenses/agpl.txt AGPL 3 or later * @version 2011-05-19 * @link https://www.mclibre.org * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as published by * the Free Software Foundation, either version 3 of the License, or * any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ // Función de recogida de datos function recoge($key, $type = "") { if (!is_string($key) && !is_int($key) || $key == "") { trigger_error("Function recoge(): Argument #1 (\$key) must be a non-empty string or an integer", E_USER_ERROR); } elseif ($type !== "" && $type !== []) { trigger_error("Function recoge(): Argument #2 (\$type) is optional, but if provided, it must be an empty array or an empty string", E_USER_ERROR); } $tmp = $type; if (isset($_REQUEST[$key])) { if (!is_array($_REQUEST[$key]) && !is_array($type)) { $tmp = trim(htmlspecialchars($_REQUEST[$key])); } elseif (is_array($_REQUEST[$key]) && is_array($type)) { $tmp = $_REQUEST[$key]; array_walk_recursive($tmp, function (&$value) { $value = trim(htmlspecialchars($value)); }); } } return $tmp; } $color = recoge("color"); // Si se envía un color se crea la cookie if ($color == "rojo" || $color == "azul" || $color == "verde") { setcookie("cookieColor", $color); // Si se envía el color ninguno se destruye la cookie } elseif ($color == "ninguno") { setcookie ("cookieColor", "", time() - 3600); // Si no se envía ningún color se mira si hay cookie con un color } elseif (isset($_COOKIE["cookieColor"])) { $color = $_COOKIE["cookieColor"]; } print "\n"; print "\n"; print "\n"; print " \n"; print " \n"; print " Selección de colores (selección). Cookies.\n"; print " Ejercicios. PHP. Bartolomé Sintes Marco. www.mclibre.org\n"; print " \n"; print " \n"; print " \n"; print "\n"; print "\n"; print "\n"; print "

Selección de colores (selección)

\n"; print "\n"; if ($color == "") { print "

No se ha elegido ningún color.

\n"; print "\n"; } else { print "

Se ha elegido el color $color.

\n"; print "\n"; } print "

\n"; print " Cambio de color: \n"; print " Rojo -\n"; print " Azul -\n"; print " Verde -\n"; print " Ninguno.\n"; print "

\n"; print "\n"; print "

Ir a otra página para comprobar la cookie

\n"; print "\n"; print " \n"; print "\n"; print "\n"; ?>