<?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>Work that Web &#187; http</title>
	<atom:link href="http://davidvanvickle.com/blog/tag/http/feed/" rel="self" type="application/rss+xml" />
	<link>http://davidvanvickle.com/blog</link>
	<description>My independent creative efforts.</description>
	<lastBuildDate>Sun, 29 Aug 2010 05:38:25 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Passing Data with AJAX, Sessions, Cookies, and GET and POST Requests</title>
		<link>http://davidvanvickle.com/blog/2008/06/15/podcast-episode-8-passing-data-with-ajax-sessions-cookies-and-get-and-post-requests/</link>
		<comments>http://davidvanvickle.com/blog/2008/06/15/podcast-episode-8-passing-data-with-ajax-sessions-cookies-and-get-and-post-requests/#comments</comments>
		<pubDate>Sun, 15 Jun 2008 23:55:51 +0000</pubDate>
		<dc:creator>David</dc:creator>
				<category><![CDATA[Javascript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Study]]></category>
		<category><![CDATA[clientside]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[cookies]]></category>
		<category><![CDATA[data]]></category>
		<category><![CDATA[http]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[requests]]></category>
		<category><![CDATA[sample]]></category>
		<category><![CDATA[serverside]]></category>
		<category><![CDATA[session]]></category>
		<category><![CDATA[sessions]]></category>
		<category><![CDATA[tutorial]]></category>
		<category><![CDATA[vbscript]]></category>

		<guid isPermaLink="false">http://vanturtle.wordpress.com/?p=100</guid>
		<description><![CDATA[Demo http://www.davidvanvickle.com/examples/ep8/index.htm Download http://www.davidvanvickle.com/examples/ep8/ep8.zip Web pages can be highly dynamic. We can log in to a site and get relevant data like a bank balance or see our social network friends &#8211; information specific to us. That means our page requests to the server are identified as being ours and the server can compile something ]]></description>
			<content:encoded><![CDATA[<p><strong>Demo</strong><br />
<a href="http://www.davidvanvickle.com/examples/ep8/index.htm"> http://www.davidvanvickle.com/examples/ep8/index.htm</a></p>
<p><strong>Download</strong><br />
<a href="http://www.davidvanvickle.com/examples/ep8/ep8.zip"> http://www.davidvanvickle.com/examples/ep8/ep8.zip</a></p>
<p>Web pages can be highly dynamic.  We can log in to a site and get relevant data like a bank balance or see our social network friends &#8211; information specific to us.  That means our page requests to the server are identified as being ours and the server can compile something with our data in it.</p>
<p>The internet’s way of communicating (<a href="http://en.wikipedia.org/wiki/Hypertext_Transfer_Protocol">“hypertext transfer protocol” or HTTP</a>) is stateless.  That means, on it’s own, it doesn’t remember the visitor from one page to the next.  It just sends anonymous requests to the server, and the server sends back that static page.  Fortunately there are a few mechanisms for distinguishing one request from another, which allows us to make web pages more personalized.</p>
<p>One mechanism is the <a href="http://en.wikipedia.org/wiki/HTTP_cookie">cookie</a>.  Cookies are strings of text stored by the browser.  A cookie allows the browser to remember things between pages.  A common use of cookies is with sessions.  When you visit a web site, you are in a session with that web site.  Cookies are sent from the site and contain data specific to that session, such as a session ID.  The browser receives and stores the cookie, then sends it back to the web site every time a new page is requested.  So now the server, on some level, knows who is asking for what.  The server can respond differently depending on the cookie value sent from the browser.</p>
<p><a href="http://www.amazon.com/gp/product/0596101996?ie=UTF8&amp;tag=davidvanvicklecom-20&amp;linkCode=as2&amp;camp=1789&amp;creative=9325&amp;creativeASIN=0596101996"><img src="http://www.davidvanvickle.com/examples/ep8/51dIXs2B1LL._SL160_.jpg" border="0" alt="" /></a><img style="border:none !important;margin:0 !important;" src="http://www.assoc-amazon.com/e/ir?t=davidvanvicklecom-20&amp;l=as2&amp;o=1&amp;a=0596101996" border="0" alt="" width="1" height="1" /></p>
<p>Use Javascript to access cookies on the client-side.</p>
<pre>alert(document.cookie);</pre>
<p>Here is a Javascript object for reading, writing and deleting cookies.</p>
<p><a href="http://www.davidvanvickle.com/examples/ep8/index.htm"> http://www.davidvanvickle.com/examples/ep8/index.htm</a><br />
<a href="http://www.davidvanvickle.com/examples/ep8/ep8.zip"> http://www.davidvanvickle.com/examples/ep8/ep8.zip</a></p>
<p><strong>CookieHandler Example</strong></p>
<pre>var C = new CookieHandler();
C.set(‘mykey’, ‘myvalue’);
alert(C.get(‘mykey’));
C.del(‘mykey’);</pre>
<p>The problem with using cookies is that the user can always disable them, so you have to make sure your client side app doesn’t break when that happens.  Either that or you tell users up front that javascript and cookies must be enabled to use your application.</p>
<p>From the server side perspective, when cookies are not available, the server has to come up with another way to track session id’s.  Without cookies, the session ID must be embedded in a different part of the page request.  There are a couple methods to request information from a server.  One is called a GET and the other is called a POST.</p>
<p><strong>GET</strong></p>
<p>Using a GET request means that data is attached to the URL after the question mark.  This looks like “page.php?session=mysessionid”.  Multiple bits of data can get strung together after the question mark with ampersands like “page.php?key1=value1&amp;key2=value2”.</p>
<p>Usually this data is read on the server side, but Javascript on the client side can parse these GET query strings as well.</p>
<p>The following Javascript code can read the data sent with the GET method.</p>
<p><a href="http://www.davidvanvickle.com/examples/ep8/index.htm"> http://www.davidvanvickle.com/examples/ep8/index.htm</a><br />
<a href="http://www.davidvanvickle.com/examples/ep8/ep8.zip"> http://www.davidvanvickle.com/examples/ep8/ep8.zip</a></p>
<p><strong>QueryStringReader Example</strong></p>
<pre>var qsr = new QueryStringReader();
var key1 = qsr.get("key1");
var key2 = qsr.get("key2");
if (key1) alert(key1);
if (key2) alert(key2);</pre>
<p><strong>POST</strong></p>
<p>Using a POST request puts data in another part of the request packet and is generally preferred when more than a few pieces of data need to go back to the server.  When a user uses a search field, often a GET is used as there are only a couple fields of data to transfer.  But when a larger order form is used, it will tend to POST since there is more data to pass.  When files such as images need to be sent from the client to the server, the POST method is always used.  Javascript does not have access to POST data, only to GET data.</p>
<p><strong>AJAX</strong></p>
<p>Requests aren’t restricted to when the user goes between pages.  Javascript can send it’s own requests behind the scenes within a single page.  This is how AJAX works.  Javascript does a GET or POST request from inside the page and gets an XML response from the server.  The Javascript receives the XML, parses it, and changes the page layout somehow, all without the page having to reload.  That’s AJAX.  There is <a href="http://developer.mozilla.org/en/docs/AJAX:Getting_Started">a great AJAX quick start tutorial on Mozilla’s developer wiki</a>.  A link to it is available in the show transcript at Podturtle.com.</p>
<p><a href="http://www.davidvanvickle.com/examples/ep8/index.htm"> http://www.davidvanvickle.com/examples/ep8/index.htm</a><br />
<a href="http://www.davidvanvickle.com/examples/ep8/ep8.zip"> http://www.davidvanvickle.com/examples/ep8/ep8.zip</a></p>
<pre>var httpRequest;
if (window.XMLHttpRequest) { // Mozilla, Safari, ...
httpRequest = new XMLHttpRequest();
} else if (window.ActiveXObject) { // IE
httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
}
httpRequest.onreadystatechange = function() { alert(httpRequest.responseText); };
httpRequest.open('GET', ‘data.xml’, true);
httpRequest.send('');</pre>
<p><strong>Sessions</strong></p>
<p>The most reliable and easy way to deal with sessions is to use the built in session handling of modern server side scripting languages such as PHP and ASP.  There will be an object dedicated to keeping track of the session for us.  And we get to store whatever we want in that temporary session data.  A server side programmer can trust the server to maintain that session for the user however it needs to, via cookies or via GET requests.  It’s automatic.</p>
<p>PHP</p>
<pre>session_start();
$_SESSION[‘mykey’] = ‘myvalue’;
echo $_SESSION[‘mykey’];</pre>
<p>ASP</p>
<pre>Session(“mykey”) = “myvalue”
Response.write(Session(“mykey”))</pre>
<p>Similarly, methods for getting GET and POST data from requests are also handled very easily with server side scripting languages.</p>
<p>PHP</p>
<pre>echo $_GET[‘mykey’]; // GET
echo $_POST[‘mykey’]; // POST</pre>
<p>ASP</p>
<pre>Response.write(request.querystring(“mykey”)) ‘ GET
Response.write(request.form(“mykey”)) ‘ POST</pre>
<p>Whether you plan to program on the client side or the server side, learning how to pass data between pages is the first step to making your web apps interactive and personalized.</p>
]]></content:encoded>
			<wfw:commentRss>http://davidvanvickle.com/blog/2008/06/15/podcast-episode-8-passing-data-with-ajax-sessions-cookies-and-get-and-post-requests/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
