BLOG ESPERANCA

php 쿠키 사용방법 본문

Program/PHP

php 쿠키 사용방법

yobuce 2013. 1. 15. 21:06

* 쿠키 생성
<?
//bool setcookie(string name [, string balue [, int expire [, string path [,string domain[,bool secure]]]]])
$cookie_r1=setcookie("userid", "hkd",time()+8);
$cookie_r2=setcookie("username","홍길동",time()+8);

if($cookie_r1 and $cookie_r2)
echo "쿠키 생성이 완료되었습니다.(8초 이후 만료)"."<br>";
?>

* 쿠키 사용
<?
echo "생성된 'userid' 쿠키 : ".$_COOKIE[userid]."<br>";
echo "생성된 'username' 쿠키 : ".$_COOKIE[username]."<br>";
?>

* 쿠키 삭제
<?
setcookie("userid");
setcookie("username");
?>
위와 같이 setcookie()를 사용할 때 쿠키 값을 지정하지 않으면 된다.


음.
$cookie_r1=setcookie("userid", "hkd",time()+8);
$cookie_r2=setcookie("username","홍길동",time()+8);

if($cookie_r1 and $cookie_r2)
echo "쿠키 생성이 완료되었습니다.(8초 이후 만료)"."<br>";

echo "생성된 'userid'쿠키 : ".$_COOKIE[userid]."<br>";
echo "생성된 'username'쿠키 : ".$_cookie[username]."<br>";

이렇게 생성 후 바로 확인 하려니 안된다. 쿠키 값이 안 뜸.

그리고

echo "생성된 'userid' 쿠키 : ".$_COOKIE[userid]."<br>";
echo "생성된 'username' 쿠키 : ".$_COOKIE[username]."<br>";

setcookie("userid");
setcookie("username");

echo "생성된 'userid' 쿠키 : ".$_COOKIE[userid]."<br>";
echo "생성된 'username' 쿠키 : ".$_COOKIE[username]."<br>";

이렇게 생성 값 확인 후, 삭제 하고 다시 확인하는 걸 동시에 하니까
값이 삭제가 안됨.