How to Make Cookies in PHP

How to make cookies in php - Like sessions, cookies are also a concept of storing user information. Only, if the session where the storage is on the server, the cookies are in the client. Therefore, the concept of cookies should not be used to store user login information such as username, password and so on. In addition to the user can see the information stored, the user can also disable the cookies itself. If cookies are disabled, then programs that use cookies will not work properly.
Cookies themselves are usually used in shopping cart applications. Usually used for temporary storage, products selected by visitors at the time of shopping.
In the handling of cookies there are also some processes that need attention:
  • The process of making cookies
  • Cookies checking process
  • The process of deleting cookies
Below is an example of a program to create cookies in php. Please be noticed and understood.
Program 1
File Name: cookie01.php
Description: The program creates cookies.
Type the following php code in notepad.
<?php
$value = 'johans';
$value2 = 'johans william';

setcookie("username", $value);
setcookie("fullname", $value2, time()+3600); /* expire in 1
hour */
echo "<h1>This is a cookie test page</h1>";
echo "<h2>Click <a href='cookie02.php'>here</a> For
cookies checks</h2>";
?>

How to Make Cookies in PHP

Save it in the htdocs folder named cookie01.php
Program 2
File Name: cookie02.php
Description: Cookie checking program.
Type the php code below into notepad.
<?php

if(isset($_COOKIE['username'])) {
 echo "<h1>Cookies 'username' there is. The contents : " .
$_COOKIE['username'];
} else {
 echo "<h1>Cookies 'username' There is no.</h1>";
}
if(isset($_COOKIE['fullname'])) {
 echo "<h1>Cookies 'fullname' there is. The contents : " .
$_COOKIE['fullname'];
} else {
 echo "<h1>Cookies 'fullname' There is no.</h1>"; 
}
echo "<h2>Click <a href='cookie01.php'>here</a> For the creation of cookies</h2>";
echo "<h2>Click <a href='cookie03.php'>here</a> For deletion of cookies</h2>";
?>

How to Make Cookies in PHP

Save the code with the name cookie02.php in the htdocs folder.
Program 3
File Name: cookie03.php
Description: Cookie removal program.
Type the php code below in notepad.
<?php
// set the expiration date to one hour ago
setcookie ("username", "", time() - 3600);
setcookie ("fullname", "", time() - 3600);
echo "<h1>Cookies Successfully deleted.</h1>";
echo "<h2>Click <a href='cookie01.php'>here</a> For
creation of cookies</h2>";
echo "<h2>Click <a href='cookie02.php'>here</a> For
check cookies</h2>";
?>

How to Make Cookies in PHP

Save it in the htdocs folder named cookie03.php
Run the program by opening the browser, then type http://localhost/cookie01.php on the addressbar then enter. The result is as follows.
How to Make Cookies in PHP
Program view

How to Make Cookies in PHP
Cookies exist

How to Make Cookies in PHP
Cookies deleted

How to Make Cookies in PHP
No cookies

That's my tutorial on how to make cookies in php. Hopefully these tutorials can add to our knowledge of php programming language.

Comments