Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
208 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
/* | ||
Manejador de sesiones propio | ||
Recuerda crear una tabla así: | ||
CREATE TABLE IF NOT EXISTS sesiones( | ||
id VARCHAR(255) NOT NULL PRIMARY KEY, | ||
datos TEXT NOT NULL, | ||
ultimo_acceso BIGINT UNSIGNED NOT NULL | ||
); | ||
@author parzibyte | ||
@see parzibyte.me/blog | ||
@date 2018-06-28 | ||
*/ | ||
class Sesion implements \SessionHandlerInterface | ||
{ | ||
|
||
/** | ||
* @var $base_de_datos \PDO | ||
*/ | ||
private $base_de_datos; #Aquí vamos a guardar nuestra referencia a la base de datos | ||
|
||
public function open($ruta_de_guardado, $nombre_de_sesion) | ||
{ | ||
$this->base_de_datos = BD::obtenerParaSesion(); | ||
return true; | ||
} | ||
|
||
public function close() | ||
{ | ||
#Eliminamos referencia a la base de datos | ||
$this->base_de_datos = null; | ||
return true; | ||
} | ||
|
||
public function write($id_de_sesion, $datos_de_sesion) | ||
{ | ||
$ultimo_acceso = time(); | ||
$sentencia = $this->base_de_datos->prepare("REPLACE INTO sesiones (id, datos, ultimo_acceso) VALUES (?, ?, ?);"); | ||
return $sentencia->execute([$id_de_sesion, $datos_de_sesion, $ultimo_acceso]); | ||
} | ||
|
||
public function read($id_de_sesion) | ||
{ | ||
$sentencia = $this->base_de_datos->prepare("SELECT datos FROM sesiones WHERE id = ?;"); | ||
$sentencia->execute([$id_de_sesion]); | ||
# Recuperar como objeto (con PDO::FETCH_OBJ), para acceder a $fila->datos | ||
$fila = $sentencia->fetch(PDO::FETCH_OBJ); | ||
|
||
# Si no existen datos con ese id, fetch devuelve FALSE | ||
if ($fila === false) { | ||
return ""; # Cadena vacía | ||
} else { | ||
return $fila->datos; | ||
} | ||
} | ||
|
||
public function destroy($id_de_sesion) | ||
{ | ||
$sentencia = $this->base_de_datos->prepare("DELETE FROM sesiones WHERE id = ?;"); | ||
return $sentencia->execute([$id_de_sesion]); | ||
} | ||
|
||
public function gc($tiempo_de_vida) | ||
{ | ||
#Calculamos el tiempo actual menos el tiempo de vida. | ||
$caducidad = time() - $tiempo_de_vida; | ||
|
||
$sentencia = $this->base_de_datos->prepare("DELETE FROM sesiones WHERE ultimo_acceso < ?;"); | ||
return $sentencia->execute([$caducidad]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
class SesionService | ||
{ | ||
|
||
public static function escribir($clave, $datos, $sobrescribir = false) | ||
{ | ||
self::init(); | ||
if (!isset($_SESSION[$clave]) || $sobrescribir) $_SESSION[$clave] = $datos; | ||
} | ||
|
||
/** | ||
* Lee una variable almacenada en la sesión. | ||
* Devuelve la variable, o null si no existe | ||
* @param $clave | ||
* @return mixed|null | ||
*/ | ||
public static function leer($clave) | ||
{ | ||
self::init(); | ||
return $_SESSION[$clave] ?? null; | ||
} | ||
|
||
private static function init() | ||
{ | ||
if (!isset($_SESSION)) | ||
session_set_save_handler(new Sesion()); | ||
if (!self::laSesionEstaIniciada()) { | ||
session_start(); | ||
session_regenerate_id(true); | ||
} | ||
} | ||
|
||
public static function propagarIdUsuario($idUsuario) | ||
{ | ||
self::init(); | ||
$_SESSION["idUsuario"] = $idUsuario; | ||
} | ||
|
||
public static function obtenerIdUsuarioLogueado() | ||
{ | ||
self::init(); | ||
return $_SESSION["idUsuario"] ?? null; | ||
} | ||
|
||
public static function obtenerUsuarioLogueado() | ||
{ | ||
$id = self::obtenerIdUsuarioLogueado(); | ||
if (isset($id)) { | ||
return Usuarios::porId($id); | ||
} | ||
return null; | ||
} | ||
|
||
private static function laSesionEstaIniciada() | ||
{ | ||
return session_status() === PHP_SESSION_ACTIVE ? true : false; | ||
} | ||
|
||
public static function cerrarSesion() | ||
{ | ||
self::init(); | ||
session_start(); | ||
session_destroy(); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,30 @@ | ||
<?php if(SesionService::obtenerIdUsuarioLogueado() !== NULL){ ?> | ||
<nav class="navbar navbar-expand-lg navbar-light bg-light"> | ||
<a class="navbar-brand" href="<?php echo BASE_URL ?>">Cotizaciones</a> | ||
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" | ||
aria-controls="navbarNav" | ||
aria-expanded="false" aria-label="Toggle navigation"> | ||
<span class="navbar-toggler-icon"></span> | ||
</button> | ||
<div class="collapse navbar-collapse" id="navbarNav"> | ||
<ul class="navbar-nav"> | ||
<li class="nav-item <?php echo $_GET["p"] === "clientes" ? 'active' : '' ?>"> | ||
<a class="nav-link" href="<?php echo BASE_URL ?>/?p=clientes">Clientes</a> | ||
</li> | ||
<li class="nav-item <?php echo $_GET["p"] === "cotizaciones" ? 'active' : '' ?>"> | ||
<a class="nav-link" href="<?php echo BASE_URL ?>/?p=cotizaciones">Cotizaciones</a> | ||
</li> | ||
<li class="nav-item <?php echo $_GET["p"] === "editar_ajustes" ? 'active' : '' ?>"> | ||
<a class="nav-link" href="<?php echo BASE_URL ?>/?p=editar_ajustes">Ajustes</a> | ||
</li> | ||
<li class="nav-item <?php echo $_GET["p"] === "creditos" ? 'active' : '' ?>"> | ||
<a class="nav-link" href="<?php echo BASE_URL ?>/?p=creditos">Acerca de</a> | ||
</li> | ||
</ul> | ||
</div> | ||
<a class="navbar-brand" href="<?php echo BASE_URL ?>">Cotizaciones</a> | ||
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarNav" | ||
aria-controls="navbarNav" | ||
aria-expanded="false" aria-label="Toggle navigation"> | ||
<span class="navbar-toggler-icon"></span> | ||
</button> | ||
<div class="collapse navbar-collapse" id="navbarNav"> | ||
<ul class="navbar-nav"> | ||
<li class="nav-item <?php echo $_GET["p"] === "clientes" ? 'active' : '' ?>"> | ||
<a class="nav-link" href="<?php echo BASE_URL ?>/?p=clientes">Clientes</a> | ||
</li> | ||
<li class="nav-item <?php echo $_GET["p"] === "cotizaciones" ? 'active' : '' ?>"> | ||
<a class="nav-link" href="<?php echo BASE_URL ?>/?p=cotizaciones">Cotizaciones</a> | ||
</li> | ||
<li class="nav-item <?php echo $_GET["p"] === "editar_ajustes" ? 'active' : '' ?>"> | ||
<a class="nav-link" href="<?php echo BASE_URL ?>/?p=editar_ajustes">Ajustes</a> | ||
</li> | ||
<li class="nav-item <?php echo $_GET["p"] === "creditos" ? 'active' : '' ?>"> | ||
<a class="nav-link" href="<?php echo BASE_URL ?>/?p=creditos">Acerca de</a> | ||
</li> | ||
<li class="nav-item"> | ||
<a class="nav-link" href="<?php echo BASE_URL ?>/?p=logout">Salir (<?php echo htmlentities(SesionService::obtenerUsuarioLogueado()->correo) ?>)</a> | ||
</li> | ||
</ul> | ||
</div> | ||
</nav> | ||
<?php } ?> | ||
<div class="container"> |