* @copyright 2011 Bartolomé Sintes Marco * @license http://www.gnu.org/licenses/agpl.txt AGPL 3 or later * @version 2011-05-28 * @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; } define("DURACION_MIN", 1); define("DURACION_MAX", 60); $accion = recoge("accion"); $duracion = recoge("duracion"); $duracionOk = ctype_digit($duracion) && (DURACION_MIN<=$duracion) && ($duracion <= DURACION_MAX); $accionOk = ($accion == "Crear" || $accion == "Destruir" || $accion == "Comprobar"); if ($accion == "Crear" && $duracionOk) { setcookie("cookieTemporal", time()+$duracion, time()+$duracion); } elseif ($accion == "Destruir") { setcookie ("cookieTemporal", "", time() - 3600); } print "\n"; print "\n"; print "\n"; print " \n"; print " \n"; print " Creación y destrucción de cookies. 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 "

Creación y destrucción de cookies

\n"; print "\n"; if ($accion == "Crear") { if ($duracionOk) { print "

Se ha creado la cookie. Se destruirá en $duracion "; if ($duracion == 1) { print "segundo.

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

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

La duración no es correcta. No se ha creado la cookie.

\n"; print "\n"; } } elseif ($accion == "Comprobar") { if (isset($_COOKIE["cookieTemporal"])) { $tiempoRestante = $_COOKIE["cookieTemporal"] - time(); print "

La cookie se destruirá en $tiempoRestante "; if ($tiempoRestante == 1) { print "segundo.

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

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

No existe la cookie.

\n"; print "\n"; } } elseif ($accion == "Destruir") { print "

Se ha destruido la cookie.

\n"; print "\n"; } elseif ($accion != "") { print "

Error en la opción elegida. Elija de nuevo, por favor.

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

Elija una opción

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