In the previous PHP tutorial on how to send mails using PHP, you learnt how to send emails using PHP script. In this tutorial, you will learn how to set cookies in PHP.
We come across a drop down menu every time we login a new website that asks whether we want to remember password for that website or not. Here comes the use of cookies.
Cookie, also called browser cookie, is data sent from a website when it is being accessed by a user and is stored to a user’s browser. When the user visits the website again, the saved information is used. When we click on “Remember Password” for a particular website, the login information gets saved in the cookie and thus we need not to fill password again the next time we login.
Cookies may include the information about the websites being visited by the user, entering sensitive information like username and password etc. Cookies can be set for a particular time period, for example, for a month or for a year. When expiry date is defined in the cookie, it is automatically erased.
Create Cookies
Cookies can be created using PHP as follows:
Setcookie (name, value, expire, path, domain, secure);
Setcookie() is the function used to create cookie. Among the arguments mentioned above, name is mandatory and others are optional. The argument “name” is the name of the cookie, “value” defines the value of cookie, “expire” will define the time the cookie will expire. Mostly Unix time is used, which is the number of seconds elapsed since 1 January 1970. “path” is the path on the server where the cookie will be available. A single slash will tell the cookie is valid for all directories. Similarly, “domain” is the domain where the cookie is available which must contain atleast two periods for it to be valid and lastly, if “secure” is set TRUE (1), it means the cookie should be transmitted on secure connection (HTTPS). Otherwise, it set to FALSE (0) as default value. Setcookie() function should be called before printing out any HTML output.
For example,
<?php Setcookie(“cookie1”,”value1”,time()+3600, “/”,””, 0); ?>
Thus a cookie has been set with the name “cookie1” with its value as “value1” and this cookie will expire in 3600 sec i.e, 1 hour. “/” shows the cookie is valid for all directories and secure set to “0” shows the cookie is not limited to secure connection. If we don’t specify the expiry time as below:
<?php Setcookie(“cookie1”,”value1”); ?>
In this case, cookie will expire when the browser is closed.
Retrieve Cookies
To retrieve the cookies, use $_COOKIE or $_HTTP_COOKIE_VARS arrays as follows:
<?php Echo $_COOKIE[“cookie1”]; ?>
This will retrieve the cookie with the name “cookie1”.
To check whether a cookie is set or not, use isset() function as follows:
<?php If (isset ( $_COOKIE[“cookie1”])) { Echo $_COOKIE[“cookie”]; } Else Echo “cookie does not exit”; ?>
Delete Cookies
To delete a cookie, just set the expiry time as a time in the past. For example,
<?php Setcookie(“cookie1”,”value1”,time()-3600); ?>
The negative time set will not let the cookie to run.
If you have any queries, please leave a comment below.
1 Comment
I’ve been browsing online more than 3 hours today, yet I never found any interesting article like yours.