Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
[PHP] OOP Cookie Wrapper
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="Markshall" data-source="post: 240144" data-attributes="member: 1872"><p>A lot of people have trouble with cookies in PHP, they're rather tricky to manipulate and can be a pain, this I've coded this cookie wrapper class today to make it easier for myself when I'm coding projects and thought I'd be generous and share it.</p><p></p><p>I've tested it and it works fine without any problems as far as I am aware, if you come across a problem, let me know and I'll see to it if I'm not busy.</p><p></p><p>It is a static class so you do not need to instantiate it, you can just include the file and use it whenever you like.</p><p></p><p>The code is commented and I will show a few usage demonstrations after the code to show you how to use it.</p><p></p><p><a href="http://pastebin.com/FKLk24PH" target="_blank">[URL]http://pastebin.com/qAtbPv3d</a>[/URL]</p><p>[PHP]<?php</p><p>/**</p><p>* @desc Cookie wrapper coded in OOP PHP that reduces the hassle of PHP cookies.</p><p>* @author Mark Eriksson</p><p>* @webpage http://mark-eriksson.com</p><p>*/</p><p></p><p>date_default_timezone_set('Europe/London');</p><p>class Cookie {</p><p> const Session = 0;</p><p> const ADay = 86400;</p><p> const AWeek = 604800;</p><p> const AMonth = 2592000; //technically this is only 30 days.</p><p> const AYear = 31536000;</p><p></p><p> static public function Day($amount=1) {</p><p> if (!is_numeric($amount) || !is_int($amount) || $amount>200) $amount=1;</p><p> </p><p> return (self::ADay*$amount);</p><p> }</p><p> </p><p> static public function Week($amount=1) {</p><p> if (!is_numeric($amount) || !is_int($amount) || $amount>200) $amount=1;</p><p> </p><p> return (self::AWeek*$amount);</p><p> }</p><p> </p><p> static public function Month($amount=1) {</p><p> if (!is_numeric($amount) || !is_int($amount) || $amount>200) $amount=1;</p><p> </p><p> return (self::AMonth*$amount);</p><p> }</p><p> static public function Year($amount=1) {</p><p> if (!is_numeric($amount) || !is_int($amount) || $amount>200) $amount=1;</p><p> </p><p> return (self::AYear*$amount);</p><p> }</p><p> </p><p> static public function Exists($cookie) {</p><p> return (isset($_COOKIE[$cookie]));</p><p> }</p><p> </p><p> static public function IsEmpty($cookie) {</p><p> return (empty($_COOKIE[$cookie]));</p><p> }</p><p> </p><p> static public function Get($cookie, $def_value='') {</p><p> return ((self::Exists($cookie)) ? $_COOKIE[$cookie] : $def_value);</p><p> }</p><p> </p><p> static public function Set($cookie, $value, $options=array()) {</p><p> $default_options = array('expiry' => self::Year(),</p><p> 'path' => '/',</p><p> 'domain' => (bool)true,</p><p> 'secure' => (bool)false,</p><p> 'httponly' => (bool)false</p><p> );</p><p> $cookie_set = false;</p><p> </p><p> if (!headers_sent()) {</p><p> foreach ($default_options as $option_key=>$option_value) {</p><p> if (!array_key_exists($option_key, $options)) $options[$option_key] = $default_options[$option_value];</p><p> }</p><p> </p><p> $options['domain'] = (($options['domain'] === true) ? ('.' . $_SERVER['HTTP_HOST']) : '');</p><p> </p><p> $options['expiry'] = (int)((is_numeric($options['expiry']) ? ($options['expiry']+=time()) : strtotime($options['expiry']));</p><p> </p><p> $cookie_set = @setcookie($cookie, $value, $options['expiry'], $options['path'], $options['domain'], $options['secure'], $options['httponly']);</p><p> if ($cookie_set) $_COOKIE[$cookie] = $value;</p><p> }</p><p> </p><p> return $cookie_set;</p><p> }</p><p> </p><p> static public function Remove($cookie, $options=array()) {</p><p> $default_options = array('path' => '/',</p><p> 'domain' => (bool)true,</p><p> 'secure' => (bool)false,</p><p> 'httponly' => (bool)false,</p><p> 'globalremove' => (bool)true</p><p> );</p><p> $return = false;</p><p> </p><p> if (!headers_sent()) {</p><p> foreach ($default_options as $option_key=>$option_value) {</p><p> if (!array_key_exists($option_key, $options)) $options[$option_key] = $default_options[$option_value];</p><p> }</p><p> </p><p> $options['domain'] = (($options['domain'] === true) ? ('.' . $_SERVER['HTTP_HOST']) : '');</p><p> </p><p> if ($options['globalremove']) unset($_COOKIE[$cookie]);</p><p> </p><p> $return = @setcookie($cookie, '', (time()-3600), $options['path'], $options['domain'], $options['secure'], $options['httponly']);</p><p> }</p><p> </p><p> return $return;</p><p> }</p><p>}</p><p>?>[/PHP]</p><p></p><p><strong><u>Usage demonstrations</u></strong></p><p><strong><u></u></strong></p><p>Sets a cookie called 'rememberme' with value 'yes' that will expire after 2 months.</p><p>[php]<?php</p><p>include('class.cookie.php');</p><p>Cookie::Set('rememberme', 'yes', array('expiry' => Cookie::Month(2)));</p><p>?>[/php]</p><p></p><p>Checks if a cookie exists.</p><p>[php]<?php</p><p>include('class.cookie.php');</p><p>Cookie::Set('rememberme', 'yes');</p><p>var_dump(Cookie::Exists('rememberme')); //true</p><p>var_dump(Cookie::Exists('trolol')); //false</p><p>?>[/php]</p><p></p><p>Checks if a cookie is empty (means the cookie exists, but has an empty value).</p><p>[php]<?php</p><p>include('class.cookie.php');</p><p>Cookie::Set('test', 'valuehere');</p><p>var_dump(Cookie::IsEmpty('rememberme')); //false</p><p></p><p>Cookie::Set('test', '');</p><p>var_dump(Cookie::IsEmpty('test')); //true</p><p>?></p><p>[/php]</p><p></p><p>Gets the value of a cookie called 'rememberme'.</p><p>[php]<?php</p><p>include('class.cookie.php');</p><p>var_dump(Cookie::Get('rememberme')); //'yes'</p><p>?>[/php]</p><p></p><p>Removes a cookie called 'rememberme'.</p><p>[php]<?php</p><p>include('class.cookie.php');</p><p>var_dump(Cookie::Remove('rememberme')); //true</p><p>?>[/php]</p><p></p><p>I hope it helps you with any future projects if you decide to use it.</p><p>- Mark.</p></blockquote><p></p>
[QUOTE="Markshall, post: 240144, member: 1872"] A lot of people have trouble with cookies in PHP, they're rather tricky to manipulate and can be a pain, this I've coded this cookie wrapper class today to make it easier for myself when I'm coding projects and thought I'd be generous and share it. I've tested it and it works fine without any problems as far as I am aware, if you come across a problem, let me know and I'll see to it if I'm not busy. It is a static class so you do not need to instantiate it, you can just include the file and use it whenever you like. The code is commented and I will show a few usage demonstrations after the code to show you how to use it. [URL='http://pastebin.com/FKLk24PH'][URL]http://pastebin.com/qAtbPv3d[/URL][/URL] [PHP]<?php /** * @desc Cookie wrapper coded in OOP PHP that reduces the hassle of PHP cookies. * @author Mark Eriksson * @webpage http://mark-eriksson.com */ date_default_timezone_set('Europe/London'); class Cookie { const Session = 0; const ADay = 86400; const AWeek = 604800; const AMonth = 2592000; //technically this is only 30 days. const AYear = 31536000; static public function Day($amount=1) { if (!is_numeric($amount) || !is_int($amount) || $amount>200) $amount=1; return (self::ADay*$amount); } static public function Week($amount=1) { if (!is_numeric($amount) || !is_int($amount) || $amount>200) $amount=1; return (self::AWeek*$amount); } static public function Month($amount=1) { if (!is_numeric($amount) || !is_int($amount) || $amount>200) $amount=1; return (self::AMonth*$amount); } static public function Year($amount=1) { if (!is_numeric($amount) || !is_int($amount) || $amount>200) $amount=1; return (self::AYear*$amount); } static public function Exists($cookie) { return (isset($_COOKIE[$cookie])); } static public function IsEmpty($cookie) { return (empty($_COOKIE[$cookie])); } static public function Get($cookie, $def_value='') { return ((self::Exists($cookie)) ? $_COOKIE[$cookie] : $def_value); } static public function Set($cookie, $value, $options=array()) { $default_options = array('expiry' => self::Year(), 'path' => '/', 'domain' => (bool)true, 'secure' => (bool)false, 'httponly' => (bool)false ); $cookie_set = false; if (!headers_sent()) { foreach ($default_options as $option_key=>$option_value) { if (!array_key_exists($option_key, $options)) $options[$option_key] = $default_options[$option_value]; } $options['domain'] = (($options['domain'] === true) ? ('.' . $_SERVER['HTTP_HOST']) : ''); $options['expiry'] = (int)((is_numeric($options['expiry']) ? ($options['expiry']+=time()) : strtotime($options['expiry'])); $cookie_set = @setcookie($cookie, $value, $options['expiry'], $options['path'], $options['domain'], $options['secure'], $options['httponly']); if ($cookie_set) $_COOKIE[$cookie] = $value; } return $cookie_set; } static public function Remove($cookie, $options=array()) { $default_options = array('path' => '/', 'domain' => (bool)true, 'secure' => (bool)false, 'httponly' => (bool)false, 'globalremove' => (bool)true ); $return = false; if (!headers_sent()) { foreach ($default_options as $option_key=>$option_value) { if (!array_key_exists($option_key, $options)) $options[$option_key] = $default_options[$option_value]; } $options['domain'] = (($options['domain'] === true) ? ('.' . $_SERVER['HTTP_HOST']) : ''); if ($options['globalremove']) unset($_COOKIE[$cookie]); $return = @setcookie($cookie, '', (time()-3600), $options['path'], $options['domain'], $options['secure'], $options['httponly']); } return $return; } } ?>[/PHP] [B][U]Usage demonstrations [/U][/B] Sets a cookie called 'rememberme' with value 'yes' that will expire after 2 months. [php]<?php include('class.cookie.php'); Cookie::Set('rememberme', 'yes', array('expiry' => Cookie::Month(2))); ?>[/php] Checks if a cookie exists. [php]<?php include('class.cookie.php'); Cookie::Set('rememberme', 'yes'); var_dump(Cookie::Exists('rememberme')); //true var_dump(Cookie::Exists('trolol')); //false ?>[/php] Checks if a cookie is empty (means the cookie exists, but has an empty value). [php]<?php include('class.cookie.php'); Cookie::Set('test', 'valuehere'); var_dump(Cookie::IsEmpty('rememberme')); //false Cookie::Set('test', ''); var_dump(Cookie::IsEmpty('test')); //true ?> [/php] Gets the value of a cookie called 'rememberme'. [php]<?php include('class.cookie.php'); var_dump(Cookie::Get('rememberme')); //'yes' ?>[/php] Removes a cookie called 'rememberme'. [php]<?php include('class.cookie.php'); var_dump(Cookie::Remove('rememberme')); //true ?>[/php] I hope it helps you with any future projects if you decide to use it. - Mark. [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
[PHP] OOP Cookie Wrapper
Top