DB 세션 핸들링
다들 아시는 내용이겠지만,
혹시 모르는분들을 위하여 공유합니다.
1. 환경
- mysql 또는 mariadb
- php 5.xx 이상
2. 구현
mysql에서 테이블을 생성합니다.
[code]
CREATE TABLE `Session` (
`Session_Id` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
`Session_Expires` datetime NOT NULL,
`Session_Data` text COLLATE utf8_unicode_ci,
PRIMARY KEY (`Session_Id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
[/code]
inc.session.php 파일을 만들어 줍니다.
[code]
<?php
//inc.session.php
class SysSession implements SessionHandlerInterface
{
private $link;
public function open($savePath, $sessionName)
{
$link = mysqli_connect("server","user","pwd","mydatabase"); // 자신의 db정보를 기입합니다.
if($link){
$this->link = $link;
return true;
}else{
return false;
}
}
public function close()
{
mysqli_close($this->link);
return true;
}
public function read($id)
{
$result = mysqli_query($this->link,"SELECT Session_Data FROM Session WHERE Session_Id = '".$id."' AND Session_Expires > '".date('Y-m-d H:i:s')."'");
if($row = mysqli_fetch_assoc($result)){
return $row['Session_Data'];
}else{
return "";
}
}
public function write($id, $data)
{
$DateTime = date('Y-m-d H:i:s');
$NewDateTime = date('Y-m-d H:i:s',strtotime($DateTime.' + 1 hour'));
$result = mysqli_query($this->link,"REPLACE INTO Session SET Session_Id = '".$id."', Session_Expires = '".$NewDateTime."', Session_Data = '".$data."'");
if($result){
return true;
}else{
return false;
}
}
public function destroy($id)
{
$result = mysqli_query($this->link,"DELETE FROM Session WHERE Session_Id ='".$id."'");
if($result){
return true;
}else{
return false;
}
}
public function gc($maxlifetime)
{
$result = mysqli_query($this->link,"DELETE FROM Session WHERE ((UNIX_TIMESTAMP(Session_Expires) + ".$maxlifetime.") < ".$maxlifetime.")");
if($result){
return true;
}else{
return false;
}
}
}
$handler = new SysSession();
session_set_save_handler($handler, true);
?>
[/code]
db 세션핸들링이 잘되는지 세션 스타트 테스트를 해봅니다.
db에 잘들어가져 있으면 정상적으로 핸들링이 되는겁니다.
이코드를 잘 이해를 하셨다면 redis나 memcached같은 메모리db에서도 활용이 가능합니다.
[code]
<?php
//page 1
require_once('inc.session.php');
session_start();
$_SESSION['var1'] = "My Portuguese text: SOU Gaucho!";
?>
[/code]
댓글 2개
감사합니다.
게시판 목록
개발자팁
질문은 QA에서 해주시기 바랍니다.
| 번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 |
|---|---|---|---|---|---|
| 4826 | jQuery | 8년 전 | 1672 | ||
| 4825 | JavaScript | 8년 전 | 2739 | ||
| 4824 | jQuery | 8년 전 | 1463 | ||
| 4823 | jQuery | 8년 전 | 1726 | ||
| 4822 | jQuery | 8년 전 | 1810 | ||
| 4821 | jQuery | 8년 전 | 1637 | ||
| 4820 | jQuery | 8년 전 | 1647 | ||
| 4819 | jQuery | 8년 전 | 1325 | ||
| 4818 | jQuery | 8년 전 | 1969 | ||
| 4817 | jQuery | 8년 전 | 2407 | ||
| 4816 | jQuery | 8년 전 | 1703 | ||
| 4815 | jQuery | 8년 전 | 1420 | ||
| 4814 | jQuery | 8년 전 | 1839 | ||
| 4813 | jQuery | 8년 전 | 5411 | ||
| 4812 | 기타 | 8년 전 | 3163 | ||
| 4811 | jQuery | 8년 전 | 1542 | ||
| 4810 | jQuery | 8년 전 | 1714 | ||
| 4809 | jQuery | 8년 전 | 1802 | ||
| 4808 | PHP |
|
8년 전 | 5084 | |
| 4807 | node.js |
|
8년 전 | 4117 | |
| 4806 | jQuery | 8년 전 | 2215 | ||
| 4805 | jQuery | 8년 전 | 1652 | ||
| 4804 | jQuery | 8년 전 | 1201 | ||
| 4803 | jQuery | 8년 전 | 1911 | ||
| 4802 | jQuery | 8년 전 | 1417 | ||
| 4801 | jQuery | 8년 전 | 1522 | ||
| 4800 | jQuery | 8년 전 | 1742 | ||
| 4799 | jQuery | 8년 전 | 1984 | ||
| 4798 | jQuery | 8년 전 | 1521 | ||
| 4797 | jQuery | 8년 전 | 1456 |
댓글 작성
댓글을 작성하시려면 로그인이 필요합니다.
로그인하기