1setcookie($cookiename, $cookievalue, time() + (86400 * 30), "/"); // 86400 = 1 day
1//Cookies
2//Cookies are stored on the client side. cookies are not as secure as sessions
3//and it is recommended that you use sessions as much as possible.
4====================
5Version 1 for cookies
6====================
7
8<?php
9if(isset($_COOKIE['nameofcookie'])){
10 echo 'User ' . $_COOKIE['nameofcookie'] . ' is set<br>';
11}else{
12 echo'User is not set';
13}
14
15
16====================
17Version 2 for cookies
18====================
19<?php
20 //to change cookie
21 setcookie('nameofcookie','Frank', time() + (86400 *30));//set for a day
22
23if(isset($_COOKIE['nameofcookie'])){
24 echo 'User ' . $_COOKIE['nameofcookie'] . ' is set<br>';
25}else{
26 echo'User is not set';
27}
28
29=======================
30Version 3 for cookies
31=======================
32
33<?php
34 //to change cookie
35 setcookie('nameofcookie','Frank', time() + (86400 *30));//set for a day
36 //to unset a cookie just set the time that is already past
37 //delete cookie
38 setcookie('nameofcookie','Frank', time() -3600);
39
40if(isset($_COOKIE['nameofcookie'])){
41 echo 'User ' . $_COOKIE['nameofcookie'] . ' is set<br>';
42}else{
43 echo'User is not set';
44}
45
46=========================
47Version 4 check for cookies
48=========================
49<?php
50 //to change cookie
51 setcookie('nameofcookie','Frank', time() + (86400 *30));//set for a day
52
53 if(count($_COOKIE) > 0){
54 echo 'There are ' . count($_COOKIE) . ' cookies saved<br>';
55 }else{
56 echo 'There are no cookies saved<br>';
57 }
58
59if(isset($_COOKIE['nameofcookie'])){
60 echo 'User ' . $_COOKIE['nameofcookie'] . ' is set<br>';
61}else{
62 echo'User is not set';
63}
64
1//Parameter of Cookie
2//only first line is usable other lines is for descrption
3setcookie($cookiename ,$cookievalue , time() + (86400 * 10) , "/" , domain.com ,True , False);
4 //Explanation
5setcookie(name , value, time, path, domain , secure, httponly)
6 1.name is the name of cookie
7 2.value is the value that you want to save in cookie
8 3.time is expire time of cookie and it is set in sec so 86400 sec is
9 equal to 1 day time() function get the current time and 86400 * 10 means
10 after 10 days cookie will be expire
11 4.path is path of website to access coookie if we use "/" it means we can
12 access cookie from every page
13 5.domain is the domain from which you want to access the cookie if we use
14 domain then we only access cookie from that specific domain
15 6.secure means HTTPs protocol if its True it means cookie only set if its
16 HTTPs otherwise cookie cannot set
17 7.HTTPonly means if its false we can access cookie from localsite(javascript)
18 and serversite but if its Ture other wise from only serversite (php)
1<?php
2 $name = 'COOKIE_NAME';
3 $value = 'VALUE';
4 $expireTime = strtotime('+1 years');
5 $path = '/';
6 setcookie($name,$value,$expireTime,$path);
7
1//Cookies
2//Cookies are stored on the client side. cookies are not as secure as sessions
3//and it is recommended that you use sessions as much as possible.
4//save addional information as an array in a cookie
5<?php
6 $user = ['name' => 'Brad', 'email' => 'test@test.com', 'age' = 35];
7
8 $user = serialize($user);
9
10 setcookie('user', $user, time() + (86400 *30));
11
12 $user = unserialize($_COOKIE['user']);
13
14 echo $user['name'];