PHP Cookie

Cookies are similar to sessions, the main difference is cookie will be maintained in visitor’s browser.

How to set a cookie in PHP?

In php you can use setcookie function to set a cookie. Here is the syntax to set cookie in php.

setcookie(cookiname,cookievalue,expirytime,path);

If you want to set a login user cookie for 2 hours, you can set like:

setcookie(’user’,'value’,time()+2*60*60);

Normally you can store upto 4KB of cookie data in visitor’s browser.

How to destroy a cookie?

you can destroy or clear a cookie by setting a past time as expiry time to that cookie. To destroy the above set cookie, we can use like:

setcookie(’user’,”,time()-100);

Comments are closed.