Skip to content

Commit

Permalink
Use Cookies to Store the Login Session Last Longer
Browse files Browse the repository at this point in the history
  • Loading branch information
taufik-nurrohman committed Sep 13, 2014
1 parent b7b0f9f commit 0868b0d
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 7 deletions.
10 changes: 5 additions & 5 deletions system/kernel/guardian.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ protected function __clone() {}
*/

public static function get($key = null, $fallback = "") {
$log = Session::get(self::$login);
$log = Session::get('cookie:' . self::$login);
if( ! is_null($key)) {
return isset($log[$key]) ? $log[$key] : $fallback;
}
Expand Down Expand Up @@ -292,14 +292,14 @@ public static function authorize() {
if(isset($_POST['username']) && isset($_POST['password']) && ! empty($_POST['username']) && ! empty($_POST['password'])) {
if(isset($authors[$_POST['username']]) && $_POST['password'] === $authors[$_POST['username']]['password']) {
$token = self::token();
Session::set(self::$login, array(
Session::set('cookie:' . self::$login, array(
'token' => $token,
'username' => $_POST['username'],
// 'password' => $authors[$_POST['username']]['password'],
'author' => $authors[$_POST['username']]['author'],
'status' => $authors[$_POST['username']]['status'],
'email' => $authors[$_POST['username']]['email']
));
), 30, '/', "", false, true);
File::write($token)->saveTo(SYSTEM . DS . 'log' . DS . 'token.' . Text::parse($_POST['username'])->to_slug_moderate . '.txt', 0600);
File::open(SYSTEM . DS . 'log' . DS . 'users.txt')->setPermission(0600);
} else {
Expand Down Expand Up @@ -330,7 +330,7 @@ public static function authorize() {

public static function reject() {
self::deleteToken();
Session::kill(self::$login);
Session::kill('cookie:' . self::$login);
return new static;
}

Expand All @@ -351,7 +351,7 @@ public static function reject() {

public static function happy() {
$file = SYSTEM . DS . 'log' . DS . 'token.' . Text::parse(self::get('username'))->to_slug_moderate . '.txt';
$auth = Session::get(self::$login);
$auth = Session::get('cookie:' . self::$login);
return isset($auth['token']) && File::exist($file) && $auth['token'] === File::open($file)->read() ? true : false;
}

Expand Down
4 changes: 2 additions & 2 deletions system/kernel/session.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@

class Session {

public static function set($session, $value = "", $expire = 86400, $domain = "", $path = '/', $secure = false, $http_only = false) {
public static function set($session, $value = "", $expire = 1, $path = '/', $domain = "", $secure = false, $http_only = false) {
if(strpos($session, 'cookie:') === 0) {
$name = substr($session, 7);
$expire = time() + (int) $expire;
$expire = time() + 60 * 60 * 24 * ((int) $expire);
if(strpos($name, '.') !== false) {
$parts = explode('.', $name);
$name = array_shift($parts);
Expand Down

0 comments on commit 0868b0d

Please sign in to comment.