A php5 class for dealing with cookies. Technically it requires php 5.2 because of the added ‘httponly’ option but removing any reference to it will allow it to run under any 5.x build.
Example usage:
require 'cookies.class.php';
$cookie = new cookies('360000'); // Pass expiration value
if ($cookie->set('example', 'example') === TRUE) { // Pass cookie name and value
echo 'Cookie is set!';
}
echo $cookie->value('example');
Here’s the full code:
<?php
/**
* ------------------------------------------------------------------------------------------------
* php class for cookie manipulation.
* - Sets, deletes, checkes existance of, and returns values from cookies.
* ------------------------------------------------------------------------------------------------
* This program is free software: you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with this program. If
* not, see <http://www.gnu.org/licenses/lgpl.html>
* ------------------------------------------------------------------------------------------------
* @author Chris Sprague (chris@chaoscontrol.org)
* @license LGPL http://www.gnu.org/licenses/lgpl.html
* @link http://chaoscontrol.org
* @version 1.0.0
* @requires php 5.2.0
* ------------------------------------------------------------------------------------------------
*/
class cookies {
/* Class Variables ------------------------------------------------------------------------- */
/**
* Name of the cookie
* @var $mixed
*/
public $name;
/**
* Class constructor
*
* @param int $expires Number of seconds until cookie expires
* @param mixed $path
* @param bool $secure
* @param bool $httponly
*/
public function __construct($expires, $path = '/', $secure = FALSE, $httponly = FALSE) {
$this->expiration = $expires;
$this->path = $path;
$this->domain = '.' . $_SERVER["SERVER_NAME"];
$this->secure = $secure;
$this->httponly = $httponly;
}
/**
* Deletes a cookie
*
* @param mixed $name
* @return bool
*/
public function delete($name) {
if (headers_sent() === FALSE) {
setcookie (
$name,
"",
time() - 3600 * 25,
$this->path,
$this->domain,
$this->secure,
$this->httponly);
// Confirm Deletion
if ($this->check($name) === FALSE) {
return TRUE;
}
}
return FALSE;
}
/**
* Creates a cookie
*
* @param mixed $name
* @param mixed $data
* @return bool
*/
public function set($name, $data = '') {
if (headers_sent() === FALSE) {
setcookie (
$name,
serialize($data),
time() + $this->expiration,
$this->path,
$this->domain,
$this->secure,
$this->httponly);
// Set $_COOKIE value for same page load
$_COOKIE[$name] = serialize($data);
// Confirm Set
if ($this->check($name) === TRUE) {
return TRUE;
}
}
return FALSE;
}
/**
* Returns the value of a cookie
*
* @param mixed $name
* @return mixed
*/
public function value($name) {
if ($this->check($name) === TRUE) {
return unserialize($_COOKIE[$name]);
}
return FALSE;
}
/**
* Check the existance of a cookie
*
* @param mixed $name
* @return bool
*/
public function check($name) {
return isset($_COOKIE[$name]);
}
}
There are no comments to display.