PHP set Cookies code example

Example 1: php cookies

//Cookies
//Cookies are stored on the client side. cookies are not as secure as sessions
//and it is recommended that you use sessions as much as possible.
====================
Version 1 for cookies
====================

<?php
if(isset($_COOKIE['nameofcookie'])){ 
    echo 'User ' . $_COOKIE['nameofcookie'] . ' is set<br>';
}else{
    echo'User is not set';
}


====================
Version 2 for cookies
====================
<?php
    //to change cookie
    setcookie('nameofcookie','Frank', time() + (86400 *30));//set for a day

if(isset($_COOKIE['nameofcookie'])){ 
    echo 'User ' . $_COOKIE['nameofcookie'] . ' is set<br>';
}else{
    echo'User is not set';
}

=======================
Version 3 for cookies
=======================

<?php
    //to change cookie
    setcookie('nameofcookie','Frank', time() + (86400 *30));//set for a day
    //to unset a cookie just set the time that is already past
    //delete cookie
    setcookie('nameofcookie','Frank', time() -3600);

if(isset($_COOKIE['nameofcookie'])){ 
    echo 'User ' . $_COOKIE['nameofcookie'] . ' is set<br>';
}else{
    echo'User is not set';
}

=========================
Version 4 check for cookies
=========================
<?php
    //to change cookie
    setcookie('nameofcookie','Frank', time() + (86400 *30));//set for a day

    if(count($_COOKIE) > 0){
        echo 'There are ' . count($_COOKIE)  .  ' cookies saved<br>';
        }else{
            echo 'There are no cookies saved<br>';
        }

if(isset($_COOKIE['nameofcookie'])){ 
    echo 'User ' . $_COOKIE['nameofcookie'] . ' is set<br>';
}else{
    echo'User is not set';
}

Example 2: setcookie php

setcookie($cookiename, $cookievalue, time() + (86400 * 30), "/"); // 86400 = 1 day

Example 3: php set cookie

<?php
  $name = 'COOKIE_NAME';
  $value = 'VALUE';
  $expireTime = strtotime('+1 years');
  $path = '/';
  setcookie($name,$value,$expireTime,$path);

Example 4: make cookies pph

setcookie("cookiename", "cookievalue", time(), ".mydomain.tld", "/")

// coookiename: The name of your cookie
// cookievalue: The value of your cookie
// Time: The expiration date of your cookie. If you plan to make a product for the EU, it's 13 months max.
// .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.
// "/": This is the folder where your cookie will apply. If you want a specific cookie for the /mySpecialSuperSecretPages folder, you have to set /mySpecialSuperSecretPages
// Check the source for more options.

Example 5: php cookies

//Cookies
//Cookies are stored on the client side. cookies are not as secure as sessions
//and it is recommended that you use sessions as much as possible.
//save addional information as an array in a cookie
<?php
    $user = ['name' => 'Brad', 'email' => '[email protected]', 'age' = 35];

    $user = serialize($user);

    setcookie('user', $user, time() + (86400 *30));
    
    $user = unserialize($_COOKIE['user']);

    echo $user['name'];

Example 6: cookies php syntax

setcookie("cookie_name", "type_on_cookie", expiry_time(), "/");

Tags:

Php Example