PHP : How to use Cookies

in #utopian-io7 years ago (edited)


codeigniter-logo.png

What Will I Learn?

This tutorial is all about how to set and get PHP cookies for remote browsers.

  • What is a cookie?
  • How to set a cookie?
  • Difference between setcookie and setrawcookie.
  • How to get a cookie?
  • How to remove a cookie?

Requirements

  • PHP Server
  • Basic knowledge about PHP

Difficulty

  • Basic

Tutorial Contents

What is a Cookie?

Cookies are messages that contains information about the user's browsers. This is use to track and identify users. On some websites, these cookies are used to store personal information of the user.

How to set a cookie?

There are two ways to set a cookie. You can use setcookie() or setrawcookie().

setcookie() function defines a cookie that will be sent along with the HTTP headers. So this function should be use before the HTML tag. And also this function will automatically encode the data using URLEncoding when sending the cookie to the user's browser and decoded it when received from the server. The syntax for this function is this:

setcookie ( string $name [, string $value = "" [, int $expire = 0 [, string $path = "" [, string $domain = "" [, bool $secure = FALSE [, bool $httponly = FALSE ]]]]]] )

Example:

<?php 
     setcookie('Name', 'Gentlemanoi');
?>

cookie.PNG

So here, we can see that there is a cookie on the request and response header after we set the cookie.

The setrawcookie() function is just like setcookie() but without URLEncoding. So the data that will be sent to the browser and receive from the server are raw data. It means that the data that you will set will be the actual data that will be sent or received. The syntax for this function is this:

setrawcookie ( string $name [, string $value [, int $expire = 0 [, string $path [, string $domain [, bool $secure = FALSE [, bool $httponly = FALSE ]]]]]] )

Example:

<?php
     setcookie('Name', 'Gentlemanoi');
     setrawcookie('Nickname', 'gentleman');
?>

cookie.PNG

And here you can see that we already have two cookies (Name, Nickname).

The main difference between the two functions is the URLEncoding. So if you have an already encoded cookie you can use setrawcookie() to avoid double encoding.

How to get a cookie?

To get a cookie, you can use the Cookie global variable of PHP. The syntax will be like this:

<?php
     $_COOKIE['Name'];
?>

Please note that the cookie global variable cannot be used to set a cookie. The example below is INVALID:

<?php
     $_COOKIE['Name'] = 'Gentlemanoi'; //This is invalid!
?>

You can also use isset() function to check if the cookie is available.

<?php
    if(isset($_COOKIE['Name'])){
          echo $_COOKIE['Name'];
    }
?>
How to remove a cookie?

Deleting a cookie is just like setting a cookie. To remove a cookie, you also have to use the setcookie function with a third parameter(the expiration date).

Example:

<?php
    setcookie('Name', '', time() - 3600); //set to one hour ago
?>

You only have to set the expiration date to a value that is before the current time.

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @gentlemanoi I am @utopian-io. I have just upvoted you!

Achievements

  • You have less than 500 followers. Just gave you a gift to help you succeed!
  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Thanks for the detailed answer. I got to know him in detail.

This is very useful information. But I also read that you can use a global associative array $_COOKIE, for example, $_COOKIE["name"]. I recently found out how to enable cookies on iphone, found https://setapp.com/how-to/enable-cookies-on-iphone information here. I didn't think this would be so useful for my phone. Now I constantly read something new for myself.

Coin Marketplace

STEEM 0.15
TRX 0.16
JST 0.028
BTC 67011.69
ETH 2366.74
USDT 1.00
SBD 2.32