User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 331,473 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,077 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Feb 29th, 2008
Views: 850
This class allows for the handling of normal and serialized cookies as well as switching between these types of cookies.

Cookie Functions:
Write Cookies
Read Cookies
Delete Cookies

Use of this class is fairly simple.

Step 1: Include the class source in your php project using the require or include statement.
Step 2: Instance the cookie class by using the new keyword. The constructor uses these parameters
cookieName[string]{required} as the base name of the cookie
cookieTimeout[int]{required} as the time added the Unix timestamp to determine when a cookie expires
cookieSerialize[bool]{optional} default is false. If set to true then the cookies will be stored as a single serialized cookie.
cookiePath[string]{optional} default is "/" for root path you can set for sub-directory paths here if you wish.

The constructor also handles the switch over from individual cookies to serialzied cookies.

Step 3: Write a cookie

NOTE: It is important to remember that writing cookies must be done before any other output is sent to the browser.

The WriteCookie function takes as an array as an argument. This array should be built so that the name of the value name
is stored as the array element index and the value to be set is the element value
array[name] = value

Create one element for each value you want stored as a cookie.

Step 4: Read a cookie

NOTE: Of course you can read a cookie from the $_COOKIE array however you will lose out on the in built handling of serialized cookies.

You read a cookie's value by calling the ReadCookie function which takes as its argument the value name and you will receive the
cookie value in return. If the value name is not present (this single cookie is not set or not in the serialized array) then a NULL
is returned.

It is best to store the value to a variable and then test for NULL by the is_null function.

Step 5: KillCookie

The KillCookie function allows you to delete a single cookie item or array element. It takes as its argument the value name to be deleted.

Step 6: DestroyAllCookies

Will go through all the cookies with the base name as set in the constructor and delete them.
The delete process for this and the KillCookie function is handled by setting the cookie's expire date as the current Unix timestamp
- a large integer (several years).

Step 7: To change cookies from individual cookies to a serialized cookie
This is handled by changing the constructor's cookieSerialize flag from false to true. It must be done when the cookieClass is
instanced or it will not function correctly.

If you have any comments or questions concerning this php class you may contact me at jrsofty@gmail.com

This class is released under the GNU/GPL.
Copyright (C) 2005 JRSofty Programming. All rights reserved.
php Syntax
  1. <?php
  2. // cookieClass
  3. // Copyright (C) 2005 JRSofty Programming.
  4. // http://jrsofty1.stinkbugonline.com
  5. // Licensed under GNU/GPL
  6.  
  7. class cookieClass{
  8.  
  9. var $cName = '';
  10. var $cTime = '';
  11. var $cSerialize = false;
  12. var $cPath = '';
  13.  
  14. function cookieClass($cookieName, $cookieTimeout, $cookieSerialize = false, $cookiePath = "/"){
  15. $this->cName = $cookieName;
  16. $this->cTime = $cookieTimeout;
  17. $this->cSerialize = $cookieSerialize;
  18. $this->cPath = $cookiePath;
  19. // This should fix the issue if you have cookies set and THEN turn on the serialization.
  20. $iname = $this->cName . "_S";
  21. if($this->cSerialize && !isset($_COOKIE[$iname])){
  22. $cookArr = array();
  23. foreach($_COOKIE as $name=>$val){
  24. if(strpos($name,$this->cName) !== false ){ // make sure it is a cookie set by this application
  25. $subname = substr($name,strlen($this->cName) + 1);
  26. $cookArr[$subname] = $val;
  27. $this->KillCookie($name);
  28. }
  29. }
  30. $this->WriteCookie($cookArr);
  31. }
  32. // This is the opposite from above. changes a serialized cookie to multiple cookies without loss of data
  33. if(!$this->cSerialize && isset($_COOKIE[$iname])){
  34. $cookArr = unserialize($_COOKIE[$iname]);
  35. $this->KillCookie($iname);
  36. $this->WriteCookie($cookArr);
  37. }
  38.  
  39.  
  40. }
  41.  
  42. function DestroyAllCookies(){
  43. foreach($_COOKIE as $name=>$val){
  44. if(strpos($name,$this->cName) !== false){
  45. $_COOKIE[$name] = NULL;
  46. $this->KillCookie($name);
  47. }
  48. }
  49. }
  50.  
  51. function ReadCookie($item){
  52. if($this->cSerialize){
  53. $name = $this->cName . "_S";
  54. if(isset($_COOKIE[$name])){
  55. // handle the cookie as a serialzied variable
  56. $sCookie = unserialize($_COOKIE[$name]);
  57. if(isset($sCookie[$item])){
  58. return $sCookie[$item];
  59. }else{
  60. return NULL;
  61. }
  62. }else{
  63. return NULL;
  64. }
  65. }else{
  66. $name = $this->cName . "_" . $item;
  67. if(isset($_COOKIE[$name])){
  68. // handle the item as separate cookies
  69. return $_COOKIE[$name];
  70. }else{
  71. return NULL;
  72. }
  73. }
  74. }
  75.  
  76. function KillCookie($cName){
  77. $tStamp = time() - 432000;
  78. setcookie($cName,"",$tStamp,$this->cPath);
  79. }
  80.  
  81. function WriteCookie($itemArr){
  82. if($this->cSerialize){
  83. $sItems = serialize($itemArr);
  84. $name = $this->cName . "_S";
  85. $_COOKIE[$name] = $sItems;
  86. $tStamp = time() + $this->cTime;
  87. setcookie($name,$sItems,$tStamp,$this->cPath);
  88. }else{
  89. $tStamp = time() + $this->cTime;
  90. foreach($itemArr as $nam=>$val){
  91. $name = $this->cName . "_" . $nam;
  92. $_COOKIE[$name] = $val;
  93. setcookie($name,$val,$tStamp,$this->cPath);
  94. }e
  95. }
  96. }
  97.  
  98.  
  99. }
  100. ?>
Post Comment

Only community members can submit or comment on code snippets. You must register or log in to contribute.

DaniWeb Marketplace (Sponsored Links)
All times are GMT -4. The time now is 8:45 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC