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}
641setcookie("cookiename", "cookievalue", time(), ".mydomain.tld", "/")
2
3// coookiename: The name of your cookie
4// cookievalue: The value of your cookie
5// Time: The expiration date of your cookie. If you plan to make a product for the EU, it's 13 months max.
6// .mydomain.tld: The domain that your webpage is using. You can only use the domain that the PHP file is on. Adding a dot before your domain will cover all subdomains.
7// "/": This is the folder where your cookie will apply. If you want a specific cookie for the /mySpecialSuperSecretPages folder, you have to set /mySpecialSuperSecretPages
8// Check the source for more options.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
5<?php
6if(isset($_POST['submit'])){
7    $username = htmlentities($_POST['username']);
8
9    setcookie('nameofcookie', $username, time()+3600); 
10    //1hour time limit
11
12    header('Location: page2.php');
13}
14?>
15
16<!DOCTYPE html>
17<html>
18<head>
19    <title>PHP Cookies</title>
20</head>
21<body>
22        <form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>">
23                <input type="text" name="username" placeholder="Enter Username">
24                <br>
25                <input type="submit" name="submit" value="Submit">
26            </form>
27        </div>
28</body>
29</html>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'];