GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Home
  • Contact Us
  • About Us
  • PHP
  • HTML
  • JavaScript
  • Git
  • Node.js
  • React.js
  • SQL Server
  • Real Dev Tips to Build Apps and Fix Issues

This view

  • Original page
  • New address

GeezerVison is showing flatcoding.com — PHP $_COOKIE: Securely Store and Manage Data - FlatCoding

Home PHP $_COOKIE: Securely Store and Manage Data
Montasser Mossallem
onMarch 30, 2025

PHP $_COOKIE: Securely Store and Manage Data

  • PHP
3 min read

Table of Content

  • What is $_COOKIE in PHP?
  • Setting Cookies in PHP
  • Checking if a PHP Cookie is Still There
  • Updating and Deleting Cookies
  • Securing $_COOKIE in PHP
  • Wrapping Up

Ever notice how some websites just seem to “know” you? That’s thanks to cookies! When you’re working with PHP, $_COOKIE becomes a handy way to make your site feel a bit more personal. Let’s dive into how it works, how you can set it up, and why it makes such a difference.

What is $_COOKIE in PHP?

Think of $_COOKIE as your way of helping PHP remember things about users between visits. Whether it’s holding onto their login info or saving their preferences, cookies are like little notes stored in their browser, ready for the next visit. This makes it easier for users to pick up right where they left off, like a welcome back from a friend.

In the following section, you will learn how to set and retrieve cookies using PHP, so that you can apply your knowledge.

Setting Cookies in PHP

Setting up a cookie in PHP is quick—just one line, and you’re set. Here’s an example that creates a cookie named user_name:  

setcookie("username", "JohnDoe", time() + (86400 * 30), "/");

This code snippet sets a cookie with user_name as “JohnDoe,” which will hang around for an hour. When the user comes back within that time, PHP can read it and greet them like an old friend.  

The next section shows you how to retrieve and use set cookies, enabling your application to utilize stored data.

Checking if a PHP Cookie is Still There

When a user returns, here’s how you can check if that user_name cookie is still around: 

if (isset($_COOKIE["user_name"])) {
    echo "Welcome back, " . $_COOKIE["user_name"];
} else {
    echo "Hello, new visitor!";
}

Using isset() here is key. It makes sure PHP only tries to read the cookie if it’s actually there. That way, you avoid any errors if the cookie has expired or been cleared.  

In the following section, we will continue with ways of updating and deleting cookies to widen our horizons for managing sessions.

Updating and Deleting Cookies

Changing a cookie is simple because you just set it with the same name but with the new value, kind of like replacing one cookie with another with some different information:

setcookie("username", "JaneDoe", time() + (86400 * 30), "/");

There will be times when you want to clear out a cookie—like when a user logs out. To delete a cookie, set its expiration time in the past:

setcookie("user_name", "", time() - 3600); // Expired an hour ago

Once the user reloads, that cookie will disappear. It’s an easy way to tidy up and keep only what’s necessary.

The following section will give you an overview of the best practices for security that you should follow when working with cookies in PHP to keep your users safe.

Securing $_COOKIE in PHP

While cookies are useful, they’re not always private. Here are a few tips to make sure they’re as safe as possible:

  • Use HTTPS: This encrypts cookies when they’re sent to your server, keeping them safe from snooping.
  • Set HttpOnly: Adding this flag keeps JavaScript from accessing the cookie, which helps prevent certain types of attacks.
  • Use the secure flag: This makes sure cookies are only sent over HTTPS, adding another layer of security.

Here’s an example with security settings:

setcookie("user_name", "JohnDoe", time() + 3600, "/", "", true, true);

This line sets up a secure cookie that’s only accessible over HTTPS and not available to JavaScript.

It’s good to remember that cookies are meant for small bits of data. Browsers generally limit them to about 4KB, so save only the essentials. If you need to store a lot, consider using other storage methods like sessions or databases.

Wrapping Up

PHP $_COOKIE lets you create a more personalized experience, whether that means keeping users logged in, saving preferences, or making content recommendations. It’s one of those small details that adds up, making your site a place people want to return to.  

    Similar Reads

    PHP fwrite: How to Write Data to Files in PHP

    actions, store settings, or even create log files for debugging. That’s where PHP fwrite comes in. It writes data to…

    PHP strpos Function: How it Works with Examples

    Sometimes the string you expect is there, but strpos function gives a surprise in PHP. It may show the result…

    PHP strlen Function: How It Works with Strings

    Developers needed a way to check the length of a string, so the strlen function was introduced to handle this…

    PHP Class Destructor: How It Works with Examples

    A destructor in a PHP class runs when an object is no longer needed. It frees resources and cleans up…

    PHP OOP Constructor: How It Works in a Class with Examples

    The OOP constructor initializes an object when it is created in PHP. Understand What a Constructor Is in PHP A…

    File Handling in PHP

    File Handling in PHP is an important tool especially when you need to manage files such as reading, writing, or…

    How to Connect a MySQL Database to PHP?

    Connecting PHP to MySQL is not just a technical step—it is the basic part of how data-driven websites work. From…

    Constants in PHP: How They Work & Examples

    Constants in PHP store fixed values that don’t change during execution. We will cover the following topics in this article:…

    PHP substr Function: How to Extract and Manipulate Strings

    The substr() function in PHP returns part of a string. You give it a string, a starting point, and optionally…

    PHP trim(): How to remove Whitespace from String

    Early PHP scripts often failed because of extra spaces or line breaks in strings. These issues came from user input…

    • PHP
    Montasser Mossallem
    onMarch 30, 2025
    • PHP
    Share
    Previous Article

    PHP $_FILES: How to Upload Files in PHP

    Next Article

    PHP mail() Function: A Complete Guide

    Write a Comment

    Leave a Comment Cancel

    Your email address will not be published. Required fields are marked *


    Recent Posts

    • PHP array_key_exists: How it Works with Examples
    • Git add Command with Step by Step with Example
    • JavaScript valueOf Function: How it Works with Examples
    • Node JS Buffers Tutorial for Beginners and Pros
    • Component-driven HTML Guide with Examples

    Recent Comments

    No comments to show.
    You are visitor 01402520 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.