From e4d2565e05a517641de921c4c19a2c9d1beea2e7 Mon Sep 17 00:00:00 2001 From: nicosomb Date: Thu, 18 Apr 2013 15:39:34 +0200 Subject: =?UTF-8?q?#4=20-=20ajout=20syst=C3=A8me=20de=20connexion=20(login?= =?UTF-8?q?=20poche=20mot=20de=20passe=20poche=20pour=20l'instant)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- inc/Session.class.php | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 inc/Session.class.php (limited to 'inc/Session.class.php') diff --git a/inc/Session.class.php b/inc/Session.class.php new file mode 100644 index 00000000..06fa6a8e --- /dev/null +++ b/inc/Session.class.php @@ -0,0 +1,136 @@ + $value) { + $_SESSION[$key] = $value; + } + if ($login==$login_test && $password==$password_test){ + // generate unique random number to sign forms (HMAC) + $_SESSION['uid'] = sha1(uniqid('',true).'_'.mt_rand()); + $_SESSION['info']=Session::_allInfos(); + $_SESSION['username']=$login; + // Set session expiration. + $_SESSION['expires_on']=time()+Session::$inactivity_timeout; + return true; + } + return false; + } + + // Force logout + public static function logout() + { + unset($_SESSION['uid'],$_SESSION['info'],$_SESSION['expires_on']); + } + + // Make sure user is logged in. + public static function isLogged() + { + if (!isset ($_SESSION['uid']) + || $_SESSION['info']!=Session::_allInfos() + || time()>=$_SESSION['expires_on']){ + Session::logout(); + return false; + } + // User accessed a page : Update his/her session expiration date. + $_SESSION['expires_on']=time()+Session::$inactivity_timeout; + return true; + } + + // Returns a token. + public static function getToken() + { + if (!isset($_SESSION['tokens'])){ + $_SESSION['tokens']=array(); + } + // We generate a random string and store it on the server side. + $rnd = sha1(uniqid('',true).'_'.mt_rand()); + $_SESSION['tokens'][$rnd]=1; + return $rnd; + } + + // Tells if a token is ok. Using this function will destroy the token. + // return true if token is ok. + public static function isToken($token) + { + if (isset($_SESSION['tokens'][$token])) + { + unset($_SESSION['tokens'][$token]); // Token is used: destroy it. + return true; // Token is ok. + } + return false; // Wrong token, or already used. + } +} \ No newline at end of file -- cgit v1.2.3