1 <?php 2 /* Copyright (c) 2012, Geert Bergman (geert@scrivo.nl) 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. Neither the name of "Scrivo" nor the names of its contributors may be 14 * used to endorse or promote products derived from this software without 15 * specific prior written permission. 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 27 * POSSIBILITY OF SUCH DAMAGE. 28 * 29 * $Id: Session.php 708 2013-07-02 11:59:37Z geert $ 30 */ 31 32 /** 33 * Implementation of the \Scrivo\Session class. 34 */ 35 36 namespace Scrivo; 37 38 /** 39 * The Scrivo Session class is a lightweight container for session variables. 40 * It should enable you to create a section in to session variable space to 41 * store a set of session variables and to enable you to destroy the complete 42 * set at once. 43 * 44 * <?php 45 * // script1.php 46 * $session = new \Scrivo\Session("My_session"); 47 * $session->userId = 32; 48 * header("Location: script2.php"); 49 * ?> 50 * 51 * <?php 52 * // script2.php 53 * $session = new \Scrivo\Session("My_session"); 54 * echo $session->userId; 55 * ?> 56 */ 57 class Session { 58 59 /** 60 * The name of the section. 61 * @var string 62 */ 63 private $section = "Scrivo"; 64 65 /** 66 * Construct a session, either using the default session name: "Scrivo" or 67 * using your own name. 68 * 69 * @param string $section 70 */ 71 public function __construct($section=null) { 72 if ($section) { 73 $this->section = $section; 74 } 75 if (!isset($_SESSION[$this->section])) { 76 $_SESSION[$this->section] = array(); 77 } 78 } 79 80 /** 81 * Implementation of the writable properties using the PHP magic 82 * method __set(). 83 * 84 * @param string $name The name of the property to set. 85 * @param mixed $value The value of the property to set. 86 */ 87 public function __set($name, $value) { 88 $_SESSION[$this->section][$name] = $value; 89 } 90 91 /** 92 * Property access to a session variable. 93 * 94 * @param string $name The session variable name. 95 * @return mixed The value of the session variable. 96 * @throws \Scrivo\SystemException if the variable does not exists. 97 */ 98 public function __get($name) { 99 if ($this->__isset($name)) { 100 return $_SESSION[$this->section][$name]; 101 } 102 throw new \Scrivo\SystemException("Invalid session variable $name"); 103 } 104 105 /** 106 * Check if a session variable was set. 107 * 108 * @param string $name The session variable name. 109 */ 110 public function __isset($name) { 111 return isset($_SESSION[$this->section][$name]); 112 } 113 114 /** 115 * Unset/delete a session variable. 116 * 117 * @param string $name The session variable name. 118 */ 119 public function __unset($name) { 120 unset($_SESSION[$this->section][$name]); 121 } 122 123 /** 124 * Destroy the session and all its variables. 125 */ 126 public function destroy() { 127 $_SESSION[$this->section] = array(); 128 } 129 } 130 131 ?>
Documentation generated by phpDocumentor 2.0.0a12 and ScrivoDocumentor on August 29, 2013