PHP5 Session Wraper Class


/ Published in: PHP
Save to your folder(s)

Easy and lazy way to use session in PHP5


Copy this code and paste it in your HTML
  1. class Session
  2. {
  3. var $__setting;
  4.  
  5. function __construct($name = 'FuseLogic')
  6. {
  7. $this->__setting = new open();
  8. $this->__setting->name = $name;
  9. $this->__setting->_session = array();
  10. $this->__setting->space = '___';
  11. if(!isset($_SESSION)) session_start();
  12. if(count($_SESSION)>0)
  13. {
  14. foreach($_SESSION as $name => $value)
  15. {
  16. $temp = explode($this->__setting->space,$name);
  17. if($temp[0] == $this->__setting->name)
  18. {
  19. $name = str_replace($this->__setting->name.$this->__setting->space,'',$name);
  20. $this->__setting->_session[$name] = $value;
  21. }
  22. }
  23. }
  24. }
  25.  
  26. private function name($name = null)
  27. {
  28. return $this->__setting->name.$this->__setting->space.$name;
  29. }
  30.  
  31. public function __get($name)
  32. {
  33. return @$this->__setting->_session[$name];
  34. }
  35.  
  36. public function __set($name,$value)
  37. {
  38. $this->__setting->_session[$name] = $value;
  39. $name = $this->name($name);
  40. $_SESSION[$name] = $value;
  41. }
  42.  
  43. public function __isset($name)
  44. {
  45. return isset($this->__setting->_session[$name]);
  46. }
  47.  
  48. function clean()
  49. {
  50. if(count($this->__setting->_session)>0)
  51. {
  52. foreach($this->__setting->_session as $name => $value)
  53. {
  54. $name = $this->name($name);
  55. unset($_SESSION[$name]);
  56. }
  57. $this->__setting->_session = array();
  58. }
  59. }
  60.  
  61. function destroy()
  62. {
  63. $this->__setting->_session = array();
  64. }
  65.  
  66. function fromArray($a=null)
  67. {
  68. if(is_array($a))
  69. {
  70. foreach($a as $k => $v) $this->$k = $v;
  71. }
  72. }
  73.  
  74. function toArray()
  75. {
  76. return $this->__setting->_session;
  77. }
  78. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.