Which PHP Function Is Used To Create A Cookie In PHP

admin_img Posted By Bajarangi soft , Posted On 22-09-2020

A cookie is used to identify a user. A cookie is a small file that the server embeds on the user's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With PHP so we can create and retrieve cookie values.

Which function is used to create a cookie in PHP

Cookies With PHP:
cookie is created with the setcookie() function.

Syntax

setcookie(name, value, expire, path, domain, secure, httponly);

Parameter   Description

Name           set the name of the cookie.
Value          set the value of the cookie.
Expire         set the expiry timestamp of the cookie after which the cookie can’t be accessed.
Path           specify the path on the server for which the cookie will be available.
Domain         specify the domain for which the cookie is available.
httponly       indicate that the cookie should be sent only if a secure HTTPS connection exists.
 

1.Create Cookie
Exampe(1)

<?php
$cookie_name = "user";
$cookie_value = "Kiran";
setcookie($cookie_name, $cookie_value, time() + (172800 * 30), "/"); 
?>
<!DOCTYPE html>
<html>
<head>
    <title>Cookie in PHP</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <div class="well">
        <?php
        if(!isset($_COOKIE[$cookie_name])) {
            echo "Cookie named '" . $cookie_name . "' is not set!";
        } else {
            echo "Cookie '" . $cookie_name . "' is set!<br>";
            echo "Value is: " . $_COOKIE[$cookie_name];
        }
        ?>
    </div>
    <br>
</div>
</body>
</html>
 

In above example creates a cookie named "user" with the value "Kiran". The cookie will expire after 30 days (172800 * 30). The "/" means that the cookie is available in entire website after that retrieve the value of the cookie "user" using the global variable $_COOKIE.

2.We can also Modify a Cookie Value.
Exampe(2)

<?php
$cookie_name = "user";
$cookie_value = "shiva";
setcookie($cookie_name, $cookie_value, time() + (172800 * 30), "/"); // 86400 = 1 day
?>
<!DOCTYPE html>
<html>
<head>
    <title>Cookie in PHP</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <div class="well">
        <?php
        if(!isset($_COOKIE[$cookie_name])) {
            echo "Cookie named '" . $cookie_name . "' is not set!";
        } else {
            echo "Cookie '" . $cookie_name . "' is set!<br>";
            echo "Value is: " . $_COOKIE[$cookie_name];
        }
        ?>
    </div>
    <br>
</div>
</body>
</html>

In above example we can modify set the value execute file and setcookie() function restarts.

3.Delete  Cookie
Exampe(3)

<?php
// set the expiration date to one hour ago
setcookie("user", "", time() - 3600);
?>
<!DOCTYPE html>
<html>
<head>
    <title>Cookie in PHP</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
    <div class="well">
        <?php
        echo "Cookie 'user' is deleted.";
        ?>
    </div>
    <br>
</div>
</body>
</html>

In above example delete a cookie, use the setcookie() function with an expiration date.

Related Post