Make your own free website on Tripod.com

Posted by on July 31, 2019

types of cookies in PHP

There are 2 types of cookies:

(1) session-based which ends at the end of the session.

(2) Persistent cookies which are written on harddisk.

Session cookies expire toward the end of the session. This implies, when you close your browser window, the session cookie is deleted. This site just uses session cookies.

Persistent cookies don’t terminate toward the end of the session.

A session cookie might be made when you visit a site or part of a site. The cookie exists for the duration of your visit.  For instance, a session cookie is created when you utilize the Personal Login to access verified pages. Depending on the settings in your browser, you may have the option to reject session cookies; However, if you reject cookies, you may have trouble using the site that depends on that cookie.

Read also – PHP Constant

PHP provided setcookie() function to set a cookie. This function requires up to six arguments and ought to be called before <html> tag. For each cookie, this function must be called separately.

setcookie(name, value, expire, path, domain, security);

<?php

setcookie(“name”, “John Watkin”, time()+3600, “/”,””, 0);

setcookie(“age”, “36”, time()+3600, “/”, “”,  0);

?>

Read also – what is a full-stack developer 

Here is the detail of all the arguments:

Name – It sets the name of the cookie and it is stored in an environment variable called HTTP_COOKIE_VARS. This variable is used

  • During accessing cookies.

Value- It sets the value of the named variable and is the content that you really want to store.

Expiry – It specifies the future time in seconds since 1 January 1970 in 00:00 GMT. After this time the cookie will be inaccessible. If this parameter is not set, then the cookie will be

  • Automatically terminates when the web browser is closed.

Path- Specifies the directory for which the cookie is valid. Allows a single forward slash character to be a cookie Valid for all directories.

Domain – It can be used to specify domain names in very large domains and should be valid for at least two periods to be valid.

  • All cookies are only valid for the host and domain that have created them.

Security – This can be set to specify that the cookie should be sent through secure transmission using HTTPS otherwise

  • Set to 0, which means that the cookie can be sent via regular HTTP.

Published by PHP training in Chandigarh

Posted in: PHP

Comments

Be the first to comment.

Leave a Reply


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*