Created
June 29, 2018 03:55
-
-
Save parzibyte/2c178921d2f963da341e9bc2b2a69ff8 to your computer and use it in GitHub Desktop.
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
<?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 ManejadorDeSesion implements \SessionHandlerInterface | |
{ | |
private $base_de_datos; #Aquí vamos a guardar nuestra referencia a la base de datos | |
public function open($ruta_de_guardado, $nombre_de_sesion) | |
{ | |
$pass = ""; // Contraseña del servidor MySQL | |
$usuario = "root"; // Usuario de MySQL | |
$nombre_base_de_datos = "test"; // El nombre de la base de datos (no el de la tabla) | |
$host = "localhost"; // El host, normalmente localhost pero puede ser una IP en algunos casos | |
try { | |
$this->base_de_datos = new PDO('mysql:host=' . $host | |
. ';dbname=' . $nombre_base_de_datos, | |
$usuario, $pass); | |
return true; | |
} catch (Exception $e) { | |
//TODO: Imprimir o guardar el mensaje en un log | |
return false; | |
} | |
} | |
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]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Manejador de sesiones en PHP con MySQL. Para una explicación detallada y ejemplos de uso, visita: https://parzibyte.me/blog/2018/06/28/manejador-sesiones-php-mysql-pdo/