[PHP] session의 지속시간

안녕하세요. 개발자 드리머즈입니다.


웹에서 일반적으로 사용자의 구분을 위해서 쿠키(cookie)와 세션(session) 개념을 사용합니다.

php에서도 당연히 이를 지원합니다.


쿠키와 세션에 대해서는 나중에 따로 정리할 예정입니다.


session의 지속시간은 기본으로 180분이라고 합니다.

이를 확인하기 위해서는 아래의 코드를 사용하면 됩니다.


php코드

<?php

    session_start(); 

    echo "session_cache_expire = ".session_cache_expire();

?>


실행결과(단위는 분)

session_cache_expire = 180 



A.php에서 세션 만료 시간을 정했어도..

B.php에서 세션 만료 시간을 설정하지 않고 동일한 세션에 접근하면 세션만료 시간은 default 값을 가지는 것 같습니다.


그런데 말입니다..

지정한 세션 만료 시간이 지나도.. 서버의 세션 파일이 삭제 되지도 않고, php에서 해당 세션에 문제 없이 접근이 가능하여.. php 공식 사이트를 참고하니 어느 익명의 분이 아래와 같은 글을 남겼습니다.



Anonymous 

9 years ago
The manual probably doesn't stress this enough:

** This has nothing to do with lifetime of a session **

Whatever you set this setting to, it won't change how long sessions live on your server.

This only changes HTTP cache expiration time (Expires: and Cache-Control: max-age headers), which advise browser for how long it can keep pages cached in user's cache without having to reload them from the server.


해석을 간단하게 해보면,
이 함수는 서버에 존재하는 세션파일의 지속시간과 관련이 없다는 것입니다.

단지 HTTP cache 만료 시간만 변경한다고 하는데, 이는 브라우저가 서버로부터 다시 값을 읽어오지 않고, 페이지를 얼마나 오랫동안 유저의 cache에 보관하는지와 관련되어 있다고합니다.


기본적으로 서버의 sess_xxx 이름의 세션 파일은 정확히 얼마의 시간 뒤에 삭제된다는 개념은 없는 것 같습니다. 다만.. 아래의 스택오버플로우에 나온 설명처럼,


https://stackoverflow.com/questions/2064296/session-gc-maxlifetime-not-working-for-me


The session will live as long as the file is left on the server's file system. They are cleaned out by a garbage collector. The garbage collector is run approximately every hundred page loads on the server (this is rather random, the "every hundred" page loads is just an average).

Also, the age of the session is inactive age, not total age. The timer will be reset for that session every time the user does a request. 


garbage collector에 의해 제거되는데, 이는 다소 랜덤적이라고 합니다. 다른 답변을 보면 더 좋습니다.


Three variables are used to define the garbage collection behavior of PHP session variables:

  1. session.gc_maxlifetime is the lifetime in seconds for the session files (default value: 1440 = 24 minutes)
  2. session.gc_probability is the nominator for the probability to execute the garbage collector (default = 1)
  3. session.gc_divisor is the denominator for the probability to execute the garbage collector (default = 100 or 1000)

The nominator and denominator are used together to determine the probability (nominator / denominator). So when session.gc_probability is 1 and session.gc_divisor 100 this is 1 / 100 = 1 %. So 1 % of every page visit (= every session_start call) the garbage collector is executed.

If you want to test how your session expires, you need to set session.gc_probability and session.gc_divisor to 1, so each page visit will cause the garbage collector to run. Furthermore you need to use two different browsers for the test. The session of the first browser becomes cleaned when you visit your page with the second browser (and the session of the first browser is timed out). In my tests, when you use only one browser, the session becomes automatically extended although it is outdated.


기본적으로 session.gc_maxlifetime 값에 해당하는 시간이 지난 서버의 세션 파일이

'garbage'로 취급되며 잠재적으로 clean up 대상으로 여겨집니다. 그리고 session_start()가 실행될 때 session.gc_probability/session.gc_divisor 에 해당하는 확률로 세션 파일의 운명이 결정됩니다.


간단하게 말씀드리면, 서버의 사용되지 않는 세션 파일은.. 낮은 확률로 알아서 제거되는 것 같습니다.

(직접 확인은 못 함)


로그아웃 등으로 서버 내의 session 파일이 더 이상 필요하지 않은 시점을 명확히 안다면, 다른 함수를 호출하여 직접 제거해야합니다. 이는 다음 포스트에서 다루겠습니다.



참고하시길 바랍니다.



*참고

http://php.net/manual/en/function.session-cache-expire.php

http://php.net/manual/en/session.configuration.php#ini.session.gc-maxlifetime

https://stackoverflow.com/questions/520237/how-do-i-expire-a-php-session-after-30-minutes

작성자

Posted by 드리머즈

관련 글

댓글 영역