Download the PHP package refkinscallv/cookie without Composer
On this page you can find all versions of the php package refkinscallv/cookie. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package cookie
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
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
- 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
-
Install the Package via Composer
To install the
refkinscallv/cookie
package, run the following command:This command will download and install the library along with its dependencies.
-
Include the Composer Autoloader
Make sure to include the Composer autoloader at the top of your PHP scripts to automatically load the classes:
-
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: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 tofalse
by default.
-
Using the Library
After installing the package and configuring your environment, you can now begin using the
Cookie
class in your project.
Usage
Basic Setup
Without Encryption
With Encryption
Configuration
Constructor Parameters
$db
(mixed): Pass aCrypto
instance to enable encryption. Usenull
for plain cookie management.
Environment Variables
Variable | Description | Default 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
all()
Retrieve all cookie data as an associative array.
get(string $key)
Retrieve a specific cookie value.
has(string $key)
Check if a cookie key exists.
set(string|array $key, mixed $value = null)
Set a cookie value. Supports setting multiple key-value pairs.
unset(string|array $key)
Remove a specific cookie or multiple cookies.
destroy()
Remove all cookies.
Notes
- Encryption Dependency: To enable encryption, ensure the
Crypto
class is correctly configured and passed to theCookie
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
To use database-based encryption with cookies, you can leverage the Crypto Library. This library supports multiple storage methods, including databases.
Example
For a full tutorial and detailed documentation on configuring the Crypto library, visit the Crypto Library GitHub Repository.