최초 작성일 : 2021-09-25 | 수정일 : 2023-09-10 | 조회수 : |
세션의 최대 라이프 타임을 설정할 수 있다.
이 기능을 이용하여 로그인 하고 자동 로그아웃이 되도록 설정 가능
The best solution is to implement a session timeout of your own. Use a simple time stamp that denotes the time of the last activity (i.e. request) and update it with every request:
매 페이지 요청시 아래 소스를 추가하여 세션의 동작시간을 체크하고 1800초, 30분이 지나면 세션을 없에도록(로그아웃 처리 되도록) 하면 된다.
if (isset($_SESSION['LAST_ACTIVITY']) && (time() - $_SESSION['LAST_ACTIVITY'] > 1800)) { // last request was more than 30 minutes ago session_unset(); // unset $_SESSION variable for the run-time session_destroy(); // destroy session data in storage } $_SESSION['LAST_ACTIVITY'] = time(); // update last activity time stamp
그냥 위만 쓸 경우 페이지가 요청될때 세션이 갱신되는 경우 세션파일의 날짜가 수정되어 제거가 안될 수 있다.
이를 위해 time() 을 이용하여 주기적으로 타임스템프 ID를 변경하게 되면 로그아웃이 안되는 경우를 피할 수 있다.
if (!isset($_SESSION['CREATED'])) { $_SESSION['CREATED'] = time(); } else if (time() - $_SESSION['CREATED'] > 1800) { // session started more than 30 minutes ago session_regenerate_id(true); // change session ID for the current session and invalidate old session ID $_SESSION['CREATED'] = time(); // update creation time }