Free cookie consent management tool by TermsFeed Generator PHP cookies | Amir Kamizi
AMIR KAMIZI
Home Blog Courses Books Newsletter Store Membership Buy me a coffee
PHP cookies

PHP cookies

Last Updated on Mar 21, 2023

What are cookies?

Have you seen those messages “We use cookies on our website” and then you click on accept? Cookies are like files or better say content that the website/server would embed on your computer through your browser.

You can see all the cookies that all the websites have embedded on your computer.

on chorme: privacy & policy > cookies and other site data > see all

on firefox: setting > privacy policy > cookies and site data

Did you see them?

Today we are going to learn to put those data on a user’s computer. So let’s get started.

Working with Cookies

There are 4 aspects of working with cookies

  1. create
  2. update
  3. delete
  4. read

1. Create

To create a cookie, you can use the setcookie function

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

Name and Value: Do you remember associative array? And we said we have key and we have value. Here is exactly like that. The name is like key/index. if the name is the key of our array, then the value is well, value!

Expires: how long to keep the cookie on the user’s computer? If you set it to 0 or leave it empty when the user closes the browser the cookie will be expired. we can use time functionto get the current time and add the number of seconds we want.

Path: where this cookie will be available. If you set it to / the whole domain can access this cookie but if you set it to /example only /example and all the subdirectories like /example/second can access the cookie and /another cannot access it

Domain: to which subdomains will your cookie be available. If you want it to be available to all the subdomains and the domain itself you can set it to domain name like example .com

Secure: true or false. If set true the cookie can be accessed only on https from the client-side.

Httponly: it will only be available when sending the request through http protocol and not any other protocol

 

setcookie function must appear BEFORE the <html> tag.

after setcookie your cookies will be available in the next request. so refresh the page and you can see the cookie

<?php
// set the cookie
setcookie('user', 'John Doe', time() + (86400 * 30), "/");
// it will be available in the next request

2. Update

If you want to update the cookie, just run the setcookie function again, and set the name of the cookie that exists there. It will overwrite it and update the cookie.

<?php
// set the cookie
setcookie('user', 'John Doe', time() + (86400 * 30), "/");
// later update it
setcookie('user', 'John Doe Updated', time() + (86400 * 30), "/");

3. Delete

If you want to delete the cookie use setcookie but set a time in the past for example time() - 60. It will expire/delete.

setcookie('user', '', time() - 3600, "/");

4. Read

You can get all the cookies from $_COOKIE variable.

$_COOKIE is one of the 'superglobal' variables in php. This means that it is available in all scopes throughout a script.

If you want to get the cookie first make sure it exists with the isset function

// set the cookie
setcookie('user', 'John Doe', time() + (86400 * 30), "/");
// this will be available on the next request

if(isset($_COOKIE['user'])){
    // your code
}

Security

Cookies can be good to provide a very smooth and good user experience. But on the other hand insecure cookies can cause a lot of security issues.

  1. NEVER put any sensitive information as a cookie
  2. Almost always try to encrypt the cookies
  3. Don’t log the user in by checking their id that you set inside the cookie. This sentence might sound so obvious to you but it’s one of the weirdest vulnerabilities. Don’t do it. NEVER.

https://youtu.be/aeseGKEaCDU

Conclusion

Now you know about cookies in PHP.

I recommend you to open a PHP files and try to define cookies. refresh the page and see if you can access them. see them in the browser's setting. update them and delete them. make sure to be aware of the security issues.

If you have any suggestions, questions, or opinions, please contact me. I’m looking forward to hearing from you!

Key takeaways

  • cookies in PHP
  • Create and Update and Delete and Read cookies
  • Cookie security

Category: programming

Tags: #php

Join the Newsletter

Subscribe to get my latest content by email.

I won't send you spam. Unsubscribe at any time.

Related Posts

Introduction to PHP and how to build your first web application in less than 10 minutes
Mar 21, 2023 programming

Introduction to PHP and how to build your first web application in less than 10 minutes

PHP is an open source scripting language for backend development. and we are going to build our very first PHP application together. ...

7 Min Read Read More
PHP string functions
Mar 21, 2023 programming

PHP string functions

Today we are going to learn about some of the useful string functions in PHP. we are going to send a text through different functions and learn what those functions do. ...

9 Min Read Read More
PHP Array Intersect
Feb 14, 2023 programming

PHP Array Intersect

Today we are going to talk about array intersect functions in PHP .If you want to find the items in your array that are present in the other arrays, array intersect is your friend. ...

5 Min Read Read More
Vue.js Beginner to Advanced 2025: Complete Guide
Jul 21, 2025 programming

Vue.js Beginner to Advanced 2025: Complete Guide

Vue.js has evolved into one of the most popular JavaScript frameworks for building dynamic web applications. it continues to strike a balance between simplicity and capability, offering a progressive approach to front-end development. ...

33 Min Read Read More

Recommended Courses

Introduction to Machine Learning in PHP

Introduction to Machine Learning in PHP

Learn to Build Different Machine Learning Models Easily ...

PHP Tutorial Beginner to Advanced

PHP Tutorial Beginner to Advanced

Learn everything you need to start a successful career as a PHP developer ...