<?xml version="1.0" encoding="UTF-8"?> <rss
version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:atom="http://www.w3.org/2005/Atom"
xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
><channel><title>rvdavid: A Web Developer&#039;s Blog &#187; Classes &amp; Examples</title> <atom:link href="http://www.rvdavid.net/category/web-development/php-programming/classes-examples/feed/" rel="self" type="application/rss+xml" /><link>http://www.rvdavid.net</link> <description>A periodical blog of experiences from the angle of an autodidactic, paranoid and narcissistic web developer...</description> <lastBuildDate>Wed, 01 Sep 2010 12:47:15 +0000</lastBuildDate> <language>en</language> <sy:updatePeriod>hourly</sy:updatePeriod> <sy:updateFrequency>1</sy:updateFrequency> <generator>http://wordpress.org/?v=3.0.1</generator><meta
xmlns="http://www.w3.org/1999/xhtml" name="robots" content="noindex,follow" /> <item><title>PHP HTTPRequest Class</title><link>http://www.rvdavid.net/php-httprequest-class/</link> <comments>http://www.rvdavid.net/php-httprequest-class/#comments</comments> <pubDate>Thu, 10 Jan 2008 00:49:03 +0000</pubDate> <dc:creator>rvdavid</dc:creator> <category><![CDATA[Classes & Examples]]></category><guid
isPermaLink="false">http://blog.rvdavid.net/php-httprequest-class/</guid> <description><![CDATA[Note: This is is a quick example of a very basic HTTP Request Class in PHP. This would be a nice, practical introduction to classes for someone who has been looking for a place to start. Problem: By default, request data in PHP is handled by using Super Globals ($_POST, $_GET, $_COOKIES). Globals are bad [...]]]></description> <content:encoded><![CDATA[<div
class="adsense adsense-leadin" style="float:right;margin: 12px;"><script type="text/javascript">google_ad_client = "pub-3968550303568935";
/* 250x250, created 07/06/10 */
google_ad_slot = "3782770990";
google_ad_width = 250;
google_ad_height = 250;</script> <script type="text/javascript"
src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script></div><p><strong>Note:</strong> This is is a quick example of a very basic HTTP Request Class in PHP. This would be a nice, practical introduction to classes for someone who has been looking for a place to start.</p><p><strong>Problem: </strong>By default, request data in PHP is handled by using Super Globals ($_POST, $_GET, $_COOKIES). Globals are bad for several reasons (outside the scope of this example google <a
href="http://www.google.com.au/search?hl=en&amp;q=globals+are+bad+php&amp;btnG=Google+Search&amp;meta=" title="google: globals are bad php" target="_blank">here</a>). In an effort to remedy this, we need a way to encapsulate the request data into an object. <em>By doing this, we are centralising access to request data through the one channel &#8211; the HTTP Request object.</em></p><p><strong>Class Name:</strong>HTTPRequest</p><p><strong>Responsibilities: </strong></p><ul><li>Store &#8220;request data&#8221; in GPC order.</li><li>Store additional &#8220;request data&#8221; outside of the super globals referenced by a key.</li><li>Read stored &#8220;request data&#8221; by referencing a key.</li><li>Internally clean request data by handling magic_quotes_gpc and then adding slashes.</li><li>Allow access to data stored in GET, POST and COOKIE super globals.</li></ul><p><strong> Collaborators:</strong> Any &#8211; the request object is used by any other class or function that requires access to the request data.</p><p><span
id="more-80"></span><br
/> <strong>Class Code:</strong></p><pre lang="php"># Filename: httprequest.php
&lt;?php
/**
 * HTTPRequest Class
 * @author R. Villar David &lt;my@email.com&gt;
 *
 * Encapsulate request data into the one object providing a
 * single channel for request data access and manipulation.
 *
 */
class HTTPRequest
{
	/**
	 * Holds collective request data
	 *
	 * @var array
	 */
	protected $_data = array();
	/**
	 * Holds data from the $_POST super global
	 *
	 * @var array
	 */
	protected $_post = array();
	/**
	 * Holds data from the $_GET super global
	 *
	 * @var array
	 */
	protected $_get = array();
	/**
	 * Holds data from the $_COOKIE super global
	 *
	 * @var array
	 */
	protected $_cookie = array();
	/**
	 * Constructor
	 * Stores "request data" in GPC order.
	 */
	public function __construct()
	{
		$this-&gt;_data = array_merge($this-&gt;_data, $_REQUEST);
		$this-&gt;_get = array_merge($this-&gt;_get, $_GET);
		$this-&gt;_post = array_merge($this-&gt;_post, $_POST);
		$this-&gt;_cookie = array_merge($this-&gt;_cookie, $_COOKIE);
		$this-&gt;_clean();
	}
	/**
	 * Store "request data" in GPC order.
	 *
	 * @param string $key
	 * @param mixed $value
	 */
	public function set($key, $value)
	{
		$this-&gt;_data[$key] = $value;
	}
	/**
	 * Read stored "request data" by referencing a key.
	 *
	 * @param string $key
	 * @return mixed
	 */
	public function get($key)
	{
		return isset($this-&gt;_data[$key]) ? $this-&gt;_data[$key] : null;
	}
	/**
	 * Allow access to data stored in GET, POST and COOKIE super globals.
	 *
	 * @param string $var
	 * @param string $key
	 * @return mixed
	 */
	public function getRawData($var, $key)
	{
		switch(strtolower($var)) {
			case 'get':
			$array = $this-&gt;_get;
			break;
			case 'post':
			$array = $this-&gt;_post;
			break;
			case 'cookie':
			$array = $this-&gt;_cookie;
			break;
			default:
			$array = array();
			break;
		}
		if(isset($array[$key])) {
			return $array[$key];
		}
		return null;
	}
	/**
	 * Internally clean request data by handling magic_quotes_gpc and then adding slashes.
	 *
	 */
	protected function _clean()
	{
		if(get_magic_quotes_gpc()) {
			$this-&gt;_data = $this-&gt;_stripSlashes($this-&gt;_data);
			$this-&gt;_post = $this-&gt;_stripSlashes($this-&gt;_post);
			$this-&gt;_get = $this-&gt;_stripSlashes($this-&gt;_get);
		}
	}
	/**
	 * Strip slashes code from php.net website.
	 *
	 * @param mixed $value
	 * @return array
	 */
	protected function _stripSlashes($value)
	{
	    if(is_array($value)) {
			return array_map(array($this,'_stripSlashes'), $value);
		} else {
			return stripslashes($value);
		}
	}
}</pre><p><strong>Example Usage</strong></p><pre lang="php"># Example Usage
require 'httprequest.php';
$request = new HTTPRequest();
$request-&gt;set('testvar','123');
$message = 'Username and Password submitted';
echo '&lt;h1&gt;Simulated Form: &lt;/h1&gt;';
echo '&lt;form action="?submit_message='.urlencode($message).'" method="post" style="width: 40em; font-size: 0.9em;"&gt;';
echo '&lt;fieldset&gt;';
echo '&lt;legend style="font-weight: bold"&gt;Demo login&lt;/legend&gt;';
echo '&lt;label for="username" style="display: block; float: left; width: 110px;"&gt;username:&lt;/label&gt;&lt;input type="text" id="username" name="username" value="'.$request-&gt;get('username').'" /&gt;&lt;br /&gt;';
echo '&lt;label for="password" style="display: block; float: left; width: 110px;"&gt;password:&lt;/label&gt;&lt;input type="password"  id="password" name="password" value="'.$request-&gt;get('password').'" /&gt;&lt;br /&gt;';
echo '&lt;input value="submit" type="submit"/&gt;&lt;br /&gt;';
echo '&lt;input type="hidden" value="submitted" name="submitted"/&gt;';
echo '&lt;/fieldset&gt;';
if($request-&gt;get('submitted')) {
	echo '&lt;div style="border: 1px solid #000; padding: 5px; margin-top: 20px;"&gt;';
	echo '&lt;span style="font-weight: bold;"&gt;Form submitted with the following values:&lt;/span&gt;';
	echo '&lt;ul style="color: #f00;"&gt;';
	echo '&lt;li&gt;submit Message (query string):'.$request-&gt;get('submit_message').'&lt;/li&gt;';
	echo '&lt;li&gt;testvar (manually set variable):'.$request-&gt;get('testvar').'&lt;/li&gt;';
	echo '&lt;li&gt;username (text field):'.$request-&gt;get('username').'&lt;/li&gt;';
	echo '&lt;li&gt;password (text field):'.$request-&gt;get('password').'&lt;/li&gt;';
	echo '&lt;/ul&gt;';
	echo '&lt;/div&gt;';
}
echo '&lt;/form&gt;';</pre><p><strong>Download</strong><br
/> <a
href="http://blog.rvdavid.net/wp-content/uploads/2008/01/httprequest.txt" target="_blank" title="HTTPRequest Class">HTTPRequest Class</a></p><p>As demonstrated in the code provided in the Example Usage section, the HTTPRequest class provides a centralised solution for handling request data.</p><p>This is a just base which you could build on top, or around of if you need more complex request data handling.</p><p>All in all a very simple class. Super globals are copied and &#8220;cleaned&#8221; in the constructor, getters and a setter to give you a channel where you can store your custom request data.</p><p>One of the things I take for granted with this class is the fact that I don&#8217;t need to manaully check for existing variables (eg if(isset($_POST['var'])) {) as this operation is abstracted in the get() method. If you try to access a variable that is not there, you will simply get a null value &#8211; notices need not be suppressed anymore.</p><p>Another feature that I did not set up an example for is the getRaw() method which allows you to access a variable from a specific super global, instead of relying on the data acquired from the $_REQUEST super global. While I did have a use for this, it was a special case scenario, but I thought I&#8217;d add it in to exemplify how you could add your own methods.</p><p>As you advance, you may find that you will need to add further support &#8211; such as providing the remote IP address, reading the URI string etc.</p> ]]></content:encoded> <wfw:commentRss>http://www.rvdavid.net/php-httprequest-class/feed/</wfw:commentRss> <slash:comments>5</slash:comments> </item> </channel> </rss>
<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced) (user agent is rejected)
Database Caching 4/21 queries in 0.020 seconds using disk

Served from: www.rvdavid.net @ 2010-09-10 20:26:19 -->