내가 직면하고 문제는 두 배입니다 : 나는 여러 페이지에 걸쳐 세션을 생성하는 방법으로 내 세션 래퍼 클래스를 사용하고자하는세션 데이터는 다른 페이지
- .
- 설정 시간 초과 후 위의 세션을 만료시키는 방법이 좋겠습니까?
예제 사용 사례로서, 내 사이트가 페이지에 액세스하기 전에 인증을 요구하면 사용자의 자격 증명이 유효하면 계정 페이지로 리디렉션 할 것이므로 내 세션 래퍼의 인스턴스를 만들 것입니다.
// session.php
class Session {
/**
* Default Constructor.
*
* @param (String) $name the name of the session, as well as the session cookie name
* @param (String) $timeout the amount of time to permit the existence of
* this session.
* -1, indicates that the session should live on indefinetely.
*/
function __construct($name, $timeout = -1) {
session_name($name);
session_start();
$_SESSION["timeout"] = $timeout;
$_SESSION["created"] = time();
}
/**
* Determines if the session is still considered "alive" based on its
* timeout + creation time.
* If the session has expired we remove the session effectively "Timing out".
*/
public static function isExpired() {
// Default infinite timeout case
if ($_SESSION["created"] == -1) {
return false;
}
// Evaluate time left on session
if(($_SESSION["timeout"] + $_SESSION["created"]) <= time()) {
// Remove Session
return true;
} else {
// Session has not expired yet
return false;
}
}
}
내가 $_SESSION
내에서 데이터를 기대 : 여기
// utils.php
ob_start(); // Start output buffer
/**
* Redirects the HTTP header to another location.
*
* @param (String) $address the new location to send the browser.
*/
public static function redirect($address) {
header("Location: $address");
exit();
}
이 세션 래퍼 클래스의 구현입니다 : 여기
// index.php
if (invalidUser) {
// Show error
} else if(userIsValid($user_email, $user_pass)) {
$sess = new Session("MySite", 10);
Utils::redirect("accountPage.php");
}
는 계정 페이지로 리디렉션 유틸리티 방법
global
이 페이지의 배열은
NULL
입니다. 비슷한 게시물을 읽었지만 특정 구현을 통해 뭔가를 놓치고있는 것 같습니다. 나는
$_SESSION
global
배열을 인쇄 한 후 리디렉션하지 않으면 그 안에 데이터가 있기 때문에 나는 그것을 알고
// accountsPage.php
<?php
include_once("session.php");
Session::isExpired(); => false
print_r($_SESSION); => NULL
부분적으로 작동합니다. 각 페이지의 시작 부분에 session_start()
을 추가하는 것에 대해 알고 있지만 추가 세션 및 쿠키를 작성하는 것을 완화하고 싶습니다.
도움이 될 것입니다 사전에 감사드립니다!
아주 잘 했어! – Romelus