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//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>