Massless' PHP Request, Server, and Session Wrappers

Examples:

	$bookId = getRequestVariable("bookid");
	
	$username = getSessionVariable("username");
	
	$redirectURL = "http://" . getServerVariable("HTTP_HOST") . "/somepage.php";
	
	if (issetSessionVariable("color")) {
		unsetSessionVariable("color");
	}

Source code:

1<?
2
3/* VARIABLE FUNCTIONS */
4
5	/**
6	* Returns the value of a variable from the globals collection.
7	 *
8	 * Returns the value of a variable from PHP global variables.
9	 * Accesses the value in a portable manner by testing for the presence
10	 * of the $_REQUEST object.
11	 *
12	 *@access public
13	 *@param string $strParam The name of the variable
14	 *@return string The value of the variable
15	 */
16	function getRequestVariable($strParam) 
17	{
18		$value="";
19		if (!empty($_REQUEST)) {
20			if (!empty($_REQUEST[$strParam])) {$value=$_REQUEST[$strParam];}
21		} else {
22			eval("global \$".$strParam."; \$value=\$".$strParam.";");
23		}
24		return $value;
25	}
26	
27	/**
28	 * Returns the value of a server variable from the globals collection.
29	 *
30	 * Returns the value of a server variable from PHP global variables.
31	 * Accesses the value in a portable manner by testing for the presence
32	 * of the $_SERVER object.
33	 *
34	 *@access public
35	 *@param string $strParam The name of the variable
36	 *@return string The value of the variable
37	 */
38	function getServerVariable($strParam) 
39	{
40		$value="";
41		if (!empty($_SERVER)) {
42			if (!empty($_SERVER[$strParam])) {$value=$_SERVER[$strParam];}
43		} else {
44			eval("global \$".$strParam."; \$value=\$".$strParam.";");
45		}
46		return $value;
47	}	
48	
49	
50	/**
51	 * Returns the value of a session variable from the globals collection.
52	 *
53	 * Returns the value of a session variable from PHP global variables.
54	 * Accesses the value in a portable manner by testing for the presence
55	 * of the $_SESSION object.
56	 *
57	 *@access public
58	 *@param string $strParam The name of the variable
59	 *@return string The value of the variable
60	 */
61	function getSessionVariable($strParam) {	
62		global $HTTP_SESSION_VARS;
63		if (isset($_SESSION)) {
64			if (isset($_SESSION[$strParam])) {
65				return $_SESSION[$strParam];
66			} else {return false;}
67		} else if (isset($HTTP_SESSION_VARS)) {
68			if (isset($HTTP_SESSION_VARS[$strParam])) {
69				return $HTTP_SESSION_VARS[$strParam];
70			} else {return false;}
71		}
72	}
73	
74	
75	/**
76	 * Set the value of a session variable from the globals collection.
77	 *
78	 *@access public
79	 *@param string $strParam The name of the variable
80	 *@param string $value The value
81	 *@return void 
82	 */
83	function setSessionVariable($strParam,$value) {
84		global $HTTP_SESSION_VARS;
85		if (isset($_SESSION)) {
86			$_SESSION[$strParam]=$value;
87		} else if (isset($HTTP_SESSION_VARS)) {
88			eval("global \$".$strParam.";");
89			eval("\$".$strParam."=\"\";");
90			eval("\$".$strParam."= \"".$value."\";");
91			eval("session_register('".$strParam."');"); 
92		}	
93	
94	}
95	
96	
97	/**
98	 * Set the value of a session variable from the globals collection to some object.
99	 *
100	 *@access public
101	 *@param string $strParam The name of the variable
102	 *@param string $obj The object
103	 *@return void 
104	 */
105	function setObjectInSession($strParam,$obj) {
106		global $HTTP_SESSION_VARS;
107		if (isset($_SESSION)) {
108			$_SESSION[$strParam]=$obj;
109		} else if (isset($HTTP_SESSION_VARS)) {
110			$thisObj = $obj;
111			$stringObj = serialize( $thisObj );
112			eval("global \$".$strParam.";");
113			eval("\$".$strParam."=\"\";");
114			eval("\$".$strParam."= \"\$stringObj\";");
115			eval("session_register('".$strParam."');"); 
116		}	
117	
118	}
119	
120	
121	/**
122	 * Get the value of a object in a session variable from the globals collection.
123	 *
124	 *@access public
125	 *@param string $strParam The name of the variable
126	 *@return object An object stored in session
127	 */
128	function getObjectFromSession($strParam) {	
129		global $HTTP_SESSION_VARS;
130		if (isset($_SESSION)) {
131			if (isset($_SESSION[$strParam])) {
132				return $_SESSION[$strParam];
133			} else {return false;}
134		} else if (isset($HTTP_SESSION_VARS)) {
135			if (isset($HTTP_SESSION_VARS[$strParam])) {
136				return unserialize($HTTP_SESSION_VARS[$strParam]);
137			} else {return false;}
138		}
139	}
140	
141	
142	/**
143	 * Unset the value of a session variable from the globals collection.
144	 *
145	 *@access public
146	 *@param string $strParam The name of the variable
147	 *@return void 
148	 */
149	function unsetSessionVariable($strParam) {
150		global $HTTP_SESSION_VARS;
151		if (isset($_SESSION)) {
152			if (isset($_SESSION[$strParam])) {
153				unset($_SESSION[$strParam]);
154			} else {return false;}
155		} else if (isset($HTTP_SESSION_VARS)) {
156			if (isset($HTTP_SESSION_VARS[$strParam])) {
157				unset($HTTP_SESSION_VARS[$strParam]);
158			} else {return false;}
159		}
160	}
161	
162	/**
163	 * Test for the existence of a session variable from the globals collection.
164	 *
165	 *@access public
166	 *@param string $strParam The name of the variable
167	 *@return boolean Whether the session variable exists 
168	 */
169	function issetSessionVariable($strParam) {
170		global $HTTP_SESSION_VARS;
171		if (isset($_SESSION)) {
172			if (isset($_SESSION[$strParam])) {
173				return true;
174			} else {return false;}
175		} else if (isset($HTTP_SESSION_VARS)) {
176			if (isset($HTTP_SESSION_VARS[$strParam])) {
177				return true;
178			} else {return false;}
179		}
180	}
181	
182	
183	
184	/**
185	 * Print all variables contained in the globals collection.	 
186	 */
187	function PrintAllServerVariables() {
188		foreach($GLOBALS as $key=>$value) {
189			print $key . " = " . $value . "<br />";
190		}	
191	}
192	
193?>