oscarricardosan / session
处理会话变量的小包。
1.0.2
2019-07-05 16:30 UTC
This package is auto-updated.
Last update: 2024-09-06 04:14:32 UTC
README
无差别的PHP包。
处理会话变量和闪存变量的小包。
它是一个抽象类,因此您必须先通过自己的类实现它,才能实现 verificateUserSession 方法。
示例
use Oscarricardosan\_Session_\_Session_; class _MSession_ extends _Session_ { /** * @param bool $if_fails_close_session * @return bool */ public static function verificateUserSession($if_fails_close_session= true) { $token_user = self::getAttr('user_id'); $user_data= // Code-sql that the user obtains from the session data. if (password_verify($_POST['password'], $hash)) { self::setUserData($user_data); }else{ self::logout('/'); } } }
写入示例
//In normal php session_start(); $_SESSION["newsession"]=$value; $_SESSION["name"]= 'Juan'; //With _Session_ _MSession_::setAttr('newsession', $value); _MSession_::setAttr('name', 'Juan');
读取示例
//Normally session_start(); if(isset($_SESSION["name"])) echo $_SESSION["name"]; else echo 'Sin nombre'; $products= isset($_SESSION["products"]) && is_array($_SESSION["products"])?$_SESSION["products"]:[]; //With _Session_ echo _MSession_::getAttr('name', 'Sin nombre'); $products= _MSession_::getAttr('products', []);
访问用户数据
_MSession_::verificateUserSession(); print_r(_MSession_::userData());//Al user data echo "Hola "._MSession_::userData()['name']; // Attribute in user data
控制闪存消息和闪存错误,当您调用 getFlashErrors 或 getFlashMessages 时,会话变量会自动删除。
index_product.php <form action="store_product.php"> <input type="submit" value="Send"> </form> <div> Errores:<div> <ul style="color:darkgreen;"> <?php foreach(_MSession_::getFlashErrors as $error){ echo "<li>$error</li>"; } ?> </ul> <div> Mensajes:<div> <ul style="color:darkgreen;"> <?php foreach(_MSession_::getFlashMessages as $message){ echo "<li>$message</li>"; } ?> </ul> store_product.php <?php _MSession_::setFlashError('ID no encontrado'); _MSession_::setFlashMessage('Vuelve la proxima ;)'); //Go back, index_product.php header("Location: {$_SERVER['HTTP_REFERER']}"); ?>
清理会话
//Close session and redirect to specific page. _MSession_::logout('page_to_redirect.php'); //Clears the session but does not delete the flash variables _MSession_::destroy(false); //Destroy the session and delete the flash variables _MSession_::destroy(true);