PHPackages                             refkinscallv/cookie - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. refkinscallv/cookie

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

refkinscallv/cookie
===================

Cookies Library for PHP

1.0.5(1y ago)032MITPHP

Since Aug 18Pushed 1y ago1 watchersCompare

[ Source](https://github.com/refkinscallv/cookie)[ Packagist](https://packagist.org/packages/refkinscallv/cookie)[ RSS](/packages/refkinscallv-cookie/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)DependenciesVersions (4)Used By (0)

Here's how you can modify the documentation to include the installation guide and a sample `.env` file, while keeping the existing content intact.

---

Cookie Library with Encryption Support
======================================

[](#cookie-library-with-encryption-support)

The `Cookie` class provides an advanced solution for managing cookies, with optional encryption support using the `Crypto` class. This library allows you to securely store and retrieve cookie data in both encrypted and plain formats.

---

Features
--------

[](#features)

- **Encrypted Cookies**: Use the `Crypto` library for secure cookie encryption and decryption.
- **Key-Value Storage**: Manage cookies as associative arrays for ease of use.
- **Customizable**: Configure expiration time, domain, path, and secure options.
- **Easy Integration**: Works seamlessly with both encrypted and non-encrypted cookie management.

---

Installation
------------

[](#installation)

1. **Install the Package via Composer**

    To install the `refkinscallv/cookie` package, run the following command:

    ```
    composer require refkinscallv/cookie
    ```

    This command will download and install the library along with its dependencies.
2. **Include the Composer Autoloader**

    Make sure to include the Composer autoloader at the top of your PHP scripts to automatically load the classes:

    ```
    require_once 'vendor/autoload.php';
    ```
3. **Environment Configuration**

    Make sure that your environment variables are properly configured for cookie management. You can do this by creating a `.env` file in the root directory of your project (if not already present) with the following contents:

    **Sample `.env` File**:

    ```
    COOKIE_NAME=web_cookie
    COOKIE_EXPIRES=24
    COOKIE_PATH=/
    COOKIE_SECURE=false
    ```

    This file defines:

    - `COOKIE_NAME`: The name of the cookie.
    - `COOKIE_EXPIRES`: The cookie expiration time in hours (default is 24 hours).
    - `COOKIE_PATH`: The path on the server where the cookie is available.
    - `COOKIE_SECURE`: Whether the cookie should be marked as secure (use HTTPS only). Set to `false` by default.
4. **Using the Library**

    After installing the package and configuring your environment, you can now begin using the `Cookie` class in your project.

---

Usage
-----

[](#usage)

### Basic Setup

[](#basic-setup)

#### Without Encryption

[](#without-encryption)

```
use RF\Cookie\Cookie;

// Initialize Cookie class without encryption
$cookie = new Cookie();

// Set a cookie
$cookie->set('user', 'John Doe');

// Get a cookie value
echo $cookie->get('user'); // Outputs: John Doe

// Check if a cookie exists
if ($cookie->has('user')) {
    echo 'User cookie is set.';
}

// Unset a cookie
$cookie->unset('user');

// Destroy all cookies
$cookie->destroy();
```

#### With Encryption

[](#with-encryption)

```
use RF\Crypto\Crypto;
use RF\Cookie\Cookie;

// Instantiate the Crypto class
$crypto = new Crypto([
    "encryptKey" => "your-secret-key",
    "encryptCipher" => "AES-256-CBC",
    "encryptStoreMethod" => "local",
    "encryptFile" => "/path/to/encrypt.txt"
]);

// Initialize Cookie class with encryption
$cookie = new Cookie($crypto);

// Set an encrypted cookie
$cookie->set('user', 'John Doe');

// Get an encrypted cookie value
echo $cookie->get('user'); // Outputs: John Doe

// Check if a cookie exists
if ($cookie->has('user')) {
    echo 'User cookie is set.';
}

// Unset an encrypted cookie
$cookie->unset('user');

// Destroy all encrypted cookies
$cookie->destroy();
```

---

Configuration
-------------

[](#configuration)

### Constructor Parameters

[](#constructor-parameters)

```
public function __construct(mixed $db = null)
```

- **`$db`** *(mixed)*: Pass a `Crypto` instance to enable encryption. Use `null` for plain cookie management.

### Environment Variables

[](#environment-variables)

VariableDescriptionDefault Value`COOKIE_NAME`The name of the cookie.`web_cookie``COOKIE_EXPIRES`Expiration time in hours.`24` (1 day)`COOKIE_PATH`The path on the server where the cookie is available.`/``COOKIE_SECURE`Use secure cookies (HTTPS only).`false`---

Methods
-------

[](#methods)

### `all()`

[](#all)

Retrieve all cookie data as an associative array.

```
$cookies = $cookie->all();
```

### `get(string $key)`

[](#getstring-key)

Retrieve a specific cookie value.

```
$value = $cookie->get('key');
```

### `has(string $key)`

[](#hasstring-key)

Check if a cookie key exists.

```
$exists = $cookie->has('key');
```

### `set(string|array $key, mixed $value = null)`

[](#setstringarray-key-mixed-value--null)

Set a cookie value. Supports setting multiple key-value pairs.

```
$cookie->set('key', 'value');
$cookie->set(['key1' => 'value1', 'key2' => 'value2']);
```

### `unset(string|array $key)`

[](#unsetstringarray-key)

Remove a specific cookie or multiple cookies.

```
$cookie->unset('key');
$cookie->unset(['key1', 'key2']);
```

### `destroy()`

[](#destroy)

Remove all cookies.

```
$cookie->destroy();
```

---

Notes
-----

[](#notes)

- **Encryption Dependency**: To enable encryption, ensure the `Crypto` class is correctly configured and passed to the `Cookie` constructor.
- **Headers Sent**: Ensure that cookies are set before any output is sent to the browser to avoid `headers already sent` errors.

---

Example with Database Encryption
--------------------------------

[](#example-with-database-encryption)

To use database-based encryption with cookies, you can leverage the [Crypto Library](https://github.com/refkinscallv/crypto). This library supports multiple storage methods, including databases.

### Example

[](#example)

```
use RF\Crypto\Crypto;
use RF\Cookie\Cookie;

// Configure Crypto with database storage
$crypto = new Crypto([
    "encryptKey" => "your-secret-key",
    "encryptCipher" => "AES-256-CBC",
    "encryptStoreMethod" => "database",
    "encryptDBHandler" => function($data, $mode) {
        // Database logic for storing and retrieving encrypted data
        if ($mode === 'save') {
            // Save $data to the database
        } elseif ($mode === 'load') {
            // Retrieve and return encrypted data from the database
        }
    }
]);

// Initialize Cookie class with database encryption
$cookie = new Cookie($crypto);

// Set and retrieve an encrypted cookie
$cookie->set('user', 'Encrypted User');
echo $cookie->get('user'); // Outputs: Encrypted User
```

For a full tutorial and detailed documentation on configuring the Crypto library, visit the [Crypto Library GitHub Repository](https://github.com/refkinscallv/crypto).

###  Health Score

25

—

LowBetter than 37% of packages

Maintenance38

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity42

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~47 days

Total

3

Last Release

542d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/917507a2999e41550fc4cd87473549eccd1f3099a08c44c22dd82c263c16b282?d=identicon)[refkinscallv](/maintainers/refkinscallv)

---

Top Contributors

[![refkinscallv](https://avatars.githubusercontent.com/u/31750609?v=4)](https://github.com/refkinscallv "refkinscallv (4 commits)")

### Embed Badge

![Health badge](/badges/refkinscallv-cookie/health.svg)

```
[![Health](https://phpackages.com/badges/refkinscallv-cookie/health.svg)](https://phpackages.com/packages/refkinscallv-cookie)
```

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
