월루를 꿈꾸는 대학생

PHP기초 - 쿠키 , 세션 본문

Programing/PHP

PHP기초 - 쿠키 , 세션

하즈시 2022. 2. 14. 11:38
728x90

쿠키

- 쿠키의 이름 , 쿠키의 값 , 만기 날짜가 중요 

- 구분을 쉼표로 함 

- 클라이언트에 저장됨 

 

 

 

https://www.php.net/manual/en/function.setcookie.php

 

PHP: setcookie - Manual

The server my php code is running on has sessions disabled so I am forced to store a fair bit of arbitrary data in cookies.  Using array names was impractical and problematic, so I implemented a splitting routine.  I do not serialize any class instances,

www.php.net

 

setcookie(
    string $name,  // 쿠키 이름 
    string $value = "", // 쿠키값
    int $expires_or_options = 0, //언제까지 유효한가!
    string $path = "",
    string $domain = "",
    bool $secure = false,
    bool $httponly = false
): bool

 

<?php
    $cookieName = "city";
    $cookieValue="Seoul";
    //time() 현재 시간을 반환 
    //경로 '/' 루트 
    //setcookie가 실행되면 쿠기가 만들어짐 
    setcookie($cookieName,$cookieValue,time()+60,'/');
?>

<!DOCTYPE html>
<html lang="en">
<body>
    <h1> 쿠키 체크하기 </h1>
    <?php
        //$_GET방식 $_POST방식 $COOKIE[] 방식 
        // $COOKIE[]등록된 쿠키 값 반환 
        // $_COOKIE[] 슈퍼 글로벌 변수  어느 사이트에 있건 불러서 쓸 수 있음 

        if(!isset($_COOKIE[$cookieName])){
            echo $cookieName."의 쿠키는 생성되지 않았습니다.","<br>";
        }
        else{
            echo $cookieName."의 쿠키는 생성되었습니다.","<br>";
            //$_COOKIE[$cookieName] : cookieName의 값이 cookieValue로 되는건가
            echo "생성된 값은 ". $_COOKIE[$cookieName],"<br>";
        }
    ?>
</body>
</html>

 

 

 

- 처음 접속시 쿠키 생성은 되지 않음 

- 접속후 60초간 유지되는 쿠키가 생성됨 

 

쿠키를 php의 setcookie라는 함수로 생성을 하고

생성되어 있는 쿠키를 확인 함 

 

 

 


쿠키 삭제하는 파일 생성 

 

 

- 쿠키가 삭제되고 쿠키가 없으니 $_COOKIE[$cookieName] 호출 불가라 에러 뜸 

 


 

세션 

- 서버 측에 저장됨

- 브라우저가 해당 사이트에서 벗어나면 기본적으로 끝남 = 세션 종료 

- 사이트 접속 후 아무것도 안 하는 시간이 일정 시간 넘어나면 세선이 자동으로 종료되기도 함 EX_ 은행 

- session 코드는 코드의 최상단에 작성하는 것이 좋음 

 

https://www.php.net/manual/en/function.session-start.php

 

PHP: session_start - Manual

A note about session_start(), custom handlers and database foreign key constraints, which I think may be of some use...We know that if we want our sessions into a database table (rather than the default storage), we can refer to session_set_save_handler(..

www.php.net

 

 

 

- $_SESSION['city']="Seoul"; 이런 정보가 키 : 값 형태로 되어있고 사용자가 브라우저를 계속 유지하고 있으면 이 정보가 브라우저 닫기 전까지 계속 유지됨 

- 세션 정보를 가지고 여러 활용이 가능 

- print_r 로 SESSION의 배열의 정보 확인 

 

 

- 세션은 서버에 저장되어 있음 

- 세션은 서버 어디에 저장되어 있는가 -> php.ini에 세션경로가 있음

 

 

해당 경로의 session 파일과 브라우저 상에서 보이는 session 파일명이 같음 

 

파일 내용도 확인 가능 

 


 

세션 삭제하기 

 

session_unset

- 세션의 변수를 풀어줌 
- 세션 변수가 지워짐    /// echo $_SESSION['city'];

https://www.php.net/manual/en/function.session-unset

 

PHP: session_unset - Manual

The difference between both session_unset and session_destroy is as follows:session_unset just clears out the sesison for usage. The session is still on the users computer. Note that by using session_unset, the variable still exists.Using session_unset in

www.php.net

 

 

 

session_distroy 

- 모든 세션을 파괴

 

https://www.php.net/manual/en/function.session-destroy

 

PHP: session_destroy - Manual

 

www.php.net

 

 

사실상 distory와 unset은 차이가 없는걸로 보임 

 

728x90