<?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>Quinterox &#187; Code</title>
	<atom:link href="http://www.quinterox.com/content/category/code/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.quinterox.com/content</link>
	<description>Cesar Quinteros</description>
	<lastBuildDate>Sun, 20 Jun 2010 04:51:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>AJAX In A Nutshell- Learning GET</title>
		<link>http://www.quinterox.com/content/blog/ajax-in-a-nutshell/</link>
		<comments>http://www.quinterox.com/content/blog/ajax-in-a-nutshell/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 04:13:11 +0000</pubDate>
		<dc:creator>Cesar Quinteros</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[ajax]]></category>
		<category><![CDATA[get]]></category>
		<category><![CDATA[simple ajax]]></category>

		<guid isPermaLink="false">http://www.quinterox.com/content/?p=685</guid>
		<description><![CDATA[A simple explanation and demo of how to execute the GET method using AJAX.]]></description>
			<content:encoded><![CDATA[<p>So I&#8217;ve been trying to drill the super simple concept of AJAX into my head for months (sadly) now. Anyways I think I finally got it down enough to explain it. I know there are libraries out there that can do it for you without any thinking at all. But it is my belief that a developer MUST understand how to build an application&#8217;s functionality from scratch if he/she has to. In this post I will explain how to use an XMLHttpRequest object in conjunction with the form&#8217;s &#8216;GET&#8217; method to retrieve data from the server. In our example application we will push a button passing the message &#8216;Marco&#8217; to the server which will then return the message &#8216;Polo&#8217; back to us. So let&#8217;s begin!</p>
<h2>1. Make XMLHttpRequest Object</h2>
<p>This is the most important thing. It is what AJAX is all about, it is the object that fetches stuff for you to display on your web pages. Without it (and well along with the user having JavaScript enabled) basic AJAX is not possible. I know there are &#8220;workarounds&#8221; that might give the impression of AJAX but I am just talking about straight-up AJAX. Anyways, the XMLHttpRequest object is important, so it is understandable it must work on every browser. I hope I didn&#8217;t lose you. This is how we do it.</p>
<pre>
var req = null; // XMLHttpRequest Object 

var init = function() {
   try { // firefox, safari, ie7
   req = new XMLHttpRequest(); }catch(e) {
   try { // later ie
   req = new ActiveXObject('MSXML2.XMLHTTP');}catch(e) {
   try { //early ie
   req = new ActiveXObject('Microsoft.XMLHTTP');} catch(e) {
   return false;}}}
   return req;
}
</pre>
<p>
So what did we do here? If you look past all the &#8216;try&#8217; you notice that we try to declare the &#8216;req&#8217; object as whatever form of XMLHttpRequest is available depending on your browser. In older versions of Internet Explorer AJAX occurs in the form of ActiveXObjects. This explains most of the clutter in the preceding code.</p>
<h2>2. Open The XMLHttpRequest</h2>
<p>
So you made the object now you need to tell it what to fetch. For this we use the open method of the XMLHttpRequest object which takes three parameters: method (get or post); url, and whether our request is asynchronous or not. What asynchronous means is allowing to happen at different times. </p>
<p>
Additionally you have to execute the &#8216;send&#8217; method of the request object  passing a null value. The &#8216;send&#8217; method ships our request to the server, the value it takes is used for POST requests which is why we pass null this time. This is what it looks like.</p>
<pre>
req.open('GET', 'index.php?call=Marco', true);
req.send(null);
</pre>
<p>
For simplicity&#8217;s sake in our example we are going to use the form&#8217;s &#8216;GET&#8217; method to request text from the server as opposed to XML. Using the form&#8217;s &#8216;POST&#8217; method is a little more difficult and requires additional code. I will later explain how that is done in another post.</p>
<p>
Another thing you might want to know is that some browsers might try to cache the response you received from the server. So no matter how different the response you are expecting is, it returns the same one always. To avoid this you might want to try append the time the request was made to your &#8216;URL&#8217; string as so.</p>
<pre>
var time = new Date();
req.open('GET', 'index.php?call=Marco&#038;time=' + time.getTime(), true);
req.send(null);
</pre>
<h4>Note</h4>
<p>
Also please note that AJAX will only work within the server that made the request so make sure the &#8216;url&#8217; value you pass is a local one. Cross server AJAX requests are not readily possible for security reasons but can be allowed through additional server configuration.</p>
<h2>3. Check For The Request&#8217;s State Change</h2>
<p>
Now the next few steps might get a tiny bit complicated. I will try extra hard to simplify them. <br />
First we want to try to monitor the &#8216;onreadystatechange&#8217; event of the XMLHttpRequest. The &#8216;onreadystatechange&#8217; event tells us when there is a change of state in our request as the name already suggests. So we are going to listen in by making an event handler for it.</p>
<pre>
req.onreadystatechange = function() {
     ...
}
</pre>
<p>
However the &#8216;onreadystatechange&#8217; event handler itself won&#8217;t really tell us anything. The reason being that there are four states the request can move around from which won&#8217;t necessarily hold the data we want.  They are listed below. For our purposes we only care about state #4 which means our data is ready for our viewing.</p>
<ul>
<li>Uninitialized (0)</li>
<li>Loading (1)</li>
<li>Loaded (2)</li>
<li>Interactive (3)</li>
<li>Completed (4)</li>
</ul>
<p>So in order to check when our state is at 4, we create a simple conditional statement within our event listener. </p>
<pre>
req.onreadystatechange = function() {
    if (req.readyState == 4) {
        ...
    }
}
</pre>
<p>
But again we need more since it is not enough for us to know when AJAX has something to show us. The reason is that it might have encountered an error and we wouldn&#8217;t want to show that to the users right? So now we must check to see if everything worked out right and that there were no server errors or anything of the sort. There are two ways to do this. One is to make sure our request&#8217;s status (req.status) is greater than or equal to 200 and that it is also less than or equal to 299. Together with the rest of our state change code it would look like this.
</p>
<pre>
req.onreadystatechange = function() {
    if (req.readyState == 4) {
        if (req.status >= 200 &#038;&#038; req.status <= 299) {
            ...
        }
    }
}
</pre>
<p>
A more simple alternative to the last step we did is to check if the 'statusText' property of our request simply returned 'OK'. For the sake of our Ajax In A Nutshell post name we will stick with the alternative. This is what that looks like.</p>
<pre>
req.onreadystatechange = function() {
    if (req.readyState == 4) {
        if(req.statusText == 'OK') {
            ...
        }
    }
}
</pre>
<p>Now wasn't that a lot easier?</p>
<h2>4. Retrieve Your Data</h2>
<p>Finally we grab our much awaited data which will be stored in our request's 'responseText' property. I suggest you structure your main function to accept a handler function for the data. This handler function would then either further process the data and or place it on the HTML document for viewing. In our example we are going to be passing our data to another function (our handler function) which will display it on a div in our HTML document. Below is what the passing of our data to the 'handler' function will look like along with our state change code from before.</p>
<pre>
req.onreadystatechange = function() {
    if (req.readyState == 4) {
        if(req.statusText == 'OK') {
            dataHandler(req.responseText);
        }
    }
}
</pre>
<p>
So there it is. Getting your data from the server wasn't that hard. Hopefully you are not like me and actually get it on the first few reads. </p>
<h2>Sample Please?</h2>
<p>
I have built a sample page and script that uses this code to submit a form to the server. The form will just submit the hidden value "Marco". The server will then respond "Polo" which will be displayed on a DIV below the button. </p>
<p>
You can see the sample demo here: <a href="http://www.quinterox.com/public/ajax_sample/">http://www.quinterox.com/public/ajax_sample/</a><br />
You can download the sample files here:  <a class="downloadlink" href="http://www.quinterox.com/content/wp-content/plugins/download-monitor/download.php?id=ajax_sample.zip" title="Version1.0 downloaded 25 times" >ajax_sample (25)</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.quinterox.com/content/blog/ajax-in-a-nutshell/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Enable And Disable Directory Browsing</title>
		<link>http://www.quinterox.com/content/code/enable-and-disable-directory-browsing/</link>
		<comments>http://www.quinterox.com/content/code/enable-and-disable-directory-browsing/#comments</comments>
		<pubDate>Sat, 25 Jul 2009 07:32:15 +0000</pubDate>
		<dc:creator>Cesar Quinteros</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[add index]]></category>
		<category><![CDATA[htaccess]]></category>
		<category><![CDATA[remove index]]></category>

		<guid isPermaLink="false">http://www.quinterox.com/content/?p=680</guid>
		<description><![CDATA[Adding and removing Linux server file indexes.]]></description>
			<content:encoded><![CDATA[<p>When setting up the structure to my website that includes private and public files I often found a need for a way for certain folders NOT to display a list of its containing files. After searching the web I found it well as a nice link with some .htaccess know how.</p>
<h3>Removing Indexes</h3>
<pre>
Options -Indexes
</pre>
<p></p>
<h3>Adding Indexes</h3>
<pre>
Options +Indexes
</pre>
<p>
As simple as that! Here is the link I found that might come in pretty handy.<br />
<a href="http://www.hostingmanual.net/other/htfun.shtml">http://www.hostingmanual.net/other/htfun.shtml</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.quinterox.com/content/code/enable-and-disable-directory-browsing/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>JavaScript Submitted Form</title>
		<link>http://www.quinterox.com/content/code/javascript-submitted-form/</link>
		<comments>http://www.quinterox.com/content/code/javascript-submitted-form/#comments</comments>
		<pubDate>Fri, 24 Jul 2009 03:30:26 +0000</pubDate>
		<dc:creator>Cesar Quinteros</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[form]]></category>
		<category><![CDATA[submit]]></category>

		<guid isPermaLink="false">http://www.quinterox.com/content/?p=677</guid>
		<description><![CDATA[How to submit a form using JavaScript.]]></description>
			<content:encoded><![CDATA[<p>Simply this is the code for calling JavaScript to submit a form. One good use is in case you want your submit button to be an anchored image and not a regular form button. You can attach this function to the link/ image and let it do its thing. I am currently learning Ajax so I am using the script to submit my form but not redirect or refresh the page.</p>
<pre>
document.forms['myForm'].onsubmit = function() {
       // Your code
}
</pre>
<p>Or</p>
<pre>
document.forms[0].onsubmit = function() {
       // Your code
}
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.quinterox.com/content/code/javascript-submitted-form/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding Expire Headers to Htaccess File</title>
		<link>http://www.quinterox.com/content/code/adding-expire-headers-to-htaccess-file/</link>
		<comments>http://www.quinterox.com/content/code/adding-expire-headers-to-htaccess-file/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 04:29:48 +0000</pubDate>
		<dc:creator>Cesar Quinteros</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[expire headers]]></category>
		<category><![CDATA[htaccess]]></category>

		<guid isPermaLink="false">http://www.quinterox.com/content/?p=654</guid>
		<description><![CDATA[How to add expire headers to your .htaccess file in order to save you from unnecessary server requests.]]></description>
			<content:encoded><![CDATA[<p>Adding expire headers is a good way to keep client requests to a minimum. Without them every time the client&#8217;s browser requests a file the server has to serve it. Adding expire headers, in this case to the .htaccess file, keeps a cached version of the requested file on the client&#8217;s computer in case it is requested again. For example, you have an image gallery. The client clicks on a picture, the picture is served to them, fine. Now when the client clicks the link at the top of your page to return to the main gallery page, ALL those thumbnails of the large images are requested from the server. With this code the client merely retrieves the same images he originally saw again, without the extra server requests. Nice isn&#8217;t it?<br />
<code><br />
&lt;FilesMatch "(?i)^.*\.(ico|flv|jpg|jpeg|png|gif|js|css)$"&gt;<br />
Header unset Last-Modified<br />
Header set Expires "Fri, 10 Jun 2050 00:00:00 GMT"<br />
Header set Cache-Control "public, no-transform"<br />
&lt;/FilesMatch&gt;</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.quinterox.com/content/code/adding-expire-headers-to-htaccess-file/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Turning on Gzip Compression</title>
		<link>http://www.quinterox.com/content/code/turning-on-gzip-compression/</link>
		<comments>http://www.quinterox.com/content/code/turning-on-gzip-compression/#comments</comments>
		<pubDate>Thu, 11 Jun 2009 04:14:47 +0000</pubDate>
		<dc:creator>Cesar Quinteros</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[gzip]]></category>
		<category><![CDATA[htaccess]]></category>

		<guid isPermaLink="false">http://www.quinterox.com/content/?p=650</guid>
		<description><![CDATA[How to Gzip through the .htaccess file.]]></description>
			<content:encoded><![CDATA[<p>Gzipping files could be very useful when trying to speed up your website.</p>
<p>Though I do not have the whole .htaccess thing down yet. I have found some code that simply works. As always I post it for myself as well as anyone who needs it.</p>
<p><code><br />
&lt;IfModule mod_deflate.c&gt;<br />
&lt;FilesMatch "\.(js|css)$"&gt;<br />
SetOutputFilter DEFLATE<br />
&lt;/FilesMatch&gt;<br />
&lt;/IfModule&gt;<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://www.quinterox.com/content/code/turning-on-gzip-compression/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Connecting to a MySQL database</title>
		<link>http://www.quinterox.com/content/code/connecting-to-a-mysql-database/</link>
		<comments>http://www.quinterox.com/content/code/connecting-to-a-mysql-database/#comments</comments>
		<pubDate>Fri, 02 Jan 2009 04:59:19 +0000</pubDate>
		<dc:creator>Cesar Quinteros</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[SQL]]></category>
		<category><![CDATA[connecting]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.quinterox.com/content/?p=477</guid>
		<description><![CDATA[Simple steps to connect to and extract data from a MySQL database.]]></description>
			<content:encoded><![CDATA[<p>Below are the steps to connect to and extract data from a MySQL database.</p>
<h3>Connecting to the database</h3>
<p>Step 1. Create and configure some connection variables.</p>
<pre>
$mysql_server = "mysql.yourserver.com";
$mysql_user = "admin";
$mysql_pwd= "password";
$mysql_db= "mydatabase";
</pre>
<p><br class="clear" /><br />
Step 2. Create connection.</p>
<pre>
$mysql_connection =
mysql_connect($mysql_server, $mysql_user, $mysql_pwd);
</pre>
<p><br class="clear" /><br />
Step 3. Select your database.</p>
<pre>
mysql_select_db($mysql_db) or die("Unable to select a database.");
</pre>
<p><br class="clear" /><br />
Step 4. Create your SQL string.</p>
<pre>
$mysql_sql = "SELECT * FROM mytable";
</pre>
<p><br class="clear" /><br />
Step 5. Combine your connection string and your SQL to return data.</p>
<pre>
$mysql_result = mysql_query($mysql_sql, $mysql_connection);
</pre>
<p><br class="clear" /></p>
<h3>Printing the data</h3>
<p>Step 6. Loop through the results and write it to the page. This output&#8217;s format is not pretty since it is meant to be simple.</p>
<pre>
while($data_row = mysql_fetch_row($mysql_result)) {
    foreach($data_row as $data_field) {
        print $data_field;
    }
    print "&lt;br /&gt;";
}
</pre>
<p><br class="clear" /><br />
Step 7. Close the connection string.</p>
<pre>
mysql_close($mysql_connection);
</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.quinterox.com/content/code/connecting-to-a-mysql-database/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Adding expire tags to your CSS and JavaScript files</title>
		<link>http://www.quinterox.com/content/blog/expire-tags-adding-them-to-your-css-javascript/</link>
		<comments>http://www.quinterox.com/content/blog/expire-tags-adding-them-to-your-css-javascript/#comments</comments>
		<pubDate>Thu, 01 Jan 2009 08:02:33 +0000</pubDate>
		<dc:creator>Cesar Quinteros</dc:creator>
				<category><![CDATA[ASP.NET]]></category>
		<category><![CDATA[Blog]]></category>
		<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[set expire]]></category>

		<guid isPermaLink="false">http://www.quinterox.com/content/?p=451</guid>
		<description><![CDATA[Adding expiration headers to PHP/ASP files.]]></description>
			<content:encoded><![CDATA[<p>Though we live in an age where a lot of people have fast internet connections. The focus (at least for developers) has shifted to technologies that relieve the server of heavy workloads and increase processing speeds such as AJAX, using server-side and client-side caching, as well as gzipping. In this post I will lightly touch upon client-side caching of dynamically generated CSS and JavaScript files.</p>
<p>I recently installed YSlow, a handy web development tool for Firefox. In it there is an option that tells you whether your CSS or JavaScript files are cached on the client&#8217;s computer allowing for a quicker page load. I noticed my files were not cached so I looked a little into how to go about caching them so the browser won&#8217;t download the same file over and over when going from page to page. So I went about conducting my own tests to see how to make this happen. The only way of making it happen other than setting an expire date/time on the WHOLE page (or on the .htaccess file in Linux servers) was to create a server-side file that would output the expiration, content type headers as well as the actual styles/code in your web application. Below is a clearer picture of how I made it happen.</p>
<h3>PHP- Step 1.</h3>
<p>Make a dynamically generated CSS file that includes expiration and content headers (code below). Save it as &#8216;dynamic_css.php&#8217;. The actual CSS styling can be typed in the same way you would do in a regular stylesheet, we are only after getting those expiration and content type headers sent out.</p>
<pre>&lt;?php
header("Expires: Thu, 31 Dec 2020 20:00:00 GMT");
header('Content-type: text/css');
?&gt;
h1 { color: Red; }</pre>
<p><br class="clear" /></p>
<h3>PHP- Step 2.</h3>
<p>Add &#8216;dynamic_css.php&#8217; as the CSS href of you document. This should make YSlow happy and increase the speed at which the user navigates through pages that use the same CSS file. Use the same method to add expiration tags to JavaScript.</p>
<pre>&lt;link rel="stylesheet" type="text/css" <strong><span style="color: #ff0000;">href="dynamic_css.php"</span></strong>/&gt;</pre>
<p><br class="clear" /></p>
<h3>ASP- Step 1.</h3>
<p>The way I added expire tags to my CSS code in ASP is very similar to PHP&#8217;s. First you make a server-side CSS file and save it as &#8216;dynamic_css.aspx&#8217;. Since it is important the headers hit the server first we use server-side code  to send those out first. We type in the actual CSS code like we would in a regular CSS file after that. Besides would you really want to Response.Write() all of your CSS code? I think not.</p>
<pre>&lt;%
    Response.Expires = 3600
    Response.ContentType = "text/css"
%&gt;
    h1 { color: Blue; }</pre>
<p><br class="clear" /></p>
<h3>ASP- Step 2.</h3>
<p>Add &#8216;dynamic_css.aspx&#8217; as the CSS source of you document. This should make YSlow happy since the expiration headers are now being sent increasing the speed at which the user navigates through the pages that share the same CSS file. Use this same header method to add expiration tags to JavaScript files.</p>
<pre>&lt;link rel="stylesheet" type="text/css" <strong><span style="color: #ff0000;">href="dynamic_css.aspx"</span></strong>/&gt;</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.quinterox.com/content/blog/expire-tags-adding-them-to-your-css-javascript/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>ETag Removal</title>
		<link>http://www.quinterox.com/content/code/etag-removal/</link>
		<comments>http://www.quinterox.com/content/code/etag-removal/#comments</comments>
		<pubDate>Fri, 21 Nov 2008 05:24:29 +0000</pubDate>
		<dc:creator>Cesar Quinteros</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[ETag]]></category>
		<category><![CDATA[Remove]]></category>

		<guid isPermaLink="false">http://www.quinterox.com/content/?p=429</guid>
		<description><![CDATA[Simple ETag removal.]]></description>
			<content:encoded><![CDATA[<p>The main purpose of ETags is for servers to validate whether there is a new version of the file they are requesting. The problem is that sites served on multiple servers are likely to have ETags that do not match. This problem exists for both IIS and Apache served sites. This mismatch is not important in a small site served by a single server. However it is suggested that ETags be removed on larger websites with multiple servers, this decreases header data and thus allows for a faster load. Leaving the mismatching ETag however is said to bog down busier sites. Below is a simple piece of code that shows you how to remove ETags, just add it to your .htaccess file.</p>
<pre>FileETag None</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.quinterox.com/content/code/etag-removal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Dynamically Getting File and Website Address</title>
		<link>http://www.quinterox.com/content/code/dynamically-getting-file-and-website-address/</link>
		<comments>http://www.quinterox.com/content/code/dynamically-getting-file-and-website-address/#comments</comments>
		<pubDate>Thu, 13 Nov 2008 06:49:07 +0000</pubDate>
		<dc:creator>Cesar Quinteros</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[dynamic file address]]></category>

		<guid isPermaLink="false">http://www.quinterox.com/content/?p=393</guid>
		<description><![CDATA[Getting the current web site's address or the current file location is simple in PHP. Here are a few examples.]]></description>
			<content:encoded><![CDATA[<p>Getting the current web site&#8217;s address is pretty simple and straight forward in PHP, no crazy functions nor regular expressions are needed. Below I demonstrate how to achieve this one line of code. Also I shall show you how to retrieve the page&#8217;s path from the root.</p>
<p>1. Get the current website address.</p>
<pre>$website = $_SERVER['HTTP_HOST'];</pre>
<p>Woah that was simple! Now the current folder path.</p>
<p>2. Get the current folder path.</p>
<pre>$filePath= $_SERVER['REQUEST_URI'];</pre>
<p>Did you catch that? It was so fast.</p>
<p>Put the first part and the second together (and of course add an &#8220;http://&#8221;) and you have the current file&#8217;s complete url!</p>
<pre>$fileWebAddress= "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];</pre>
]]></content:encoded>
			<wfw:commentRss>http://www.quinterox.com/content/code/dynamically-getting-file-and-website-address/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>PHP &#8211; Where to place your code</title>
		<link>http://www.quinterox.com/content/code/php-where-to-place-your-code/</link>
		<comments>http://www.quinterox.com/content/code/php-where-to-place-your-code/#comments</comments>
		<pubDate>Thu, 30 Oct 2008 06:11:15 +0000</pubDate>
		<dc:creator>Cesar Quinteros</dc:creator>
				<category><![CDATA[Code]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[script tag]]></category>

		<guid isPermaLink="false">http://www.quinterox.com/content/?p=394</guid>
		<description><![CDATA[There are a few places where to place your PHP code on your dynamic web page. This all depends on your needs and preference. Of course there is a slight difference to whether you put it on a script tag, before any HTML, within your HTML, or at the bottom of the page. Here I will explain what I have found some of the differences to be.]]></description>
			<content:encoded><![CDATA[<p>There are a few places where to place your PHP code on your dynamic web page. This all depends on your needs and preference. Of course there is a slight difference to whether you put it on a script tag, before any HTML, within your HTML, or at the bottom of the page. Here I will explain what I have found some of the differences to be.</p>
<h3>Before HTML</h3>
<p>A few of the reasons you would want to place your code before the HTML are because you want to redirect the user to another page on your website, or getting data out of a database and placing it on your page as content.</p>
<pre><span style="color: #ff6600;"><span>&lt;?<span>php</span> $<span>pageTitle</span> = "Cesar's page"; ?&gt;</span></span>
<span>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.<span>dtd</span>"&gt;</span>
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
<span>&lt;meta http-equiv="Content-Type" content="text/html; <span>charset</span>=<span>utf</span>-8" /&gt;</span>
&lt;title&gt;<span style="color: #ff6600;"><strong><span>&lt;?<span>php</span> echo $<span>pageTitle</span>; ?&gt;</span></strong></span>&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt; ...</pre>
<p><br class="break" /></p>
<h3>In the script tag</h3>
<p>Yes! Just like Javascript you can place your PHP code within a script tag, but also like Javascript gets executed after your page loads. Though I have not seen many people using this method I think it really helps you keep your code clean and organized. It is similar to the .NET code placement.</p>
<pre><span>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.<span>dtd</span>"&gt;</span>
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
<span>&lt;meta http-equiv="Content-Type" content="text/html; <span>charset</span>=<span>utf</span>-8" /&gt;</span>
&lt;title&gt;My Web Page&lt;/title&gt;
<span style="color: #ff6600;"><strong><span>&lt;script language="<span>php</span>"&gt;</span>
$pageContent = "Cesar's page content.";
&lt;/script&gt;</strong>
</span>&lt;/head&gt;
&lt;body&gt;
<span>&lt;?<span>php</span> echo $<span>pageContent</span>; ?&gt;</span>
&lt;/body&gt;
&lt;/html&gt;</pre>
<p><br class="break" /></p>
<h3>Within HTML</h3>
<p>Mixing HTML and PHP is probably the most common way PHP applications are made. As useful and conventional as this type of coding can be, it can also become confusing. Below is an example of how that looks.</p>
<pre><span>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.<span>dtd</span>"&gt;</span>
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
<span>&lt;meta http-equiv="Content-Type" content="text/html; <span>charset</span>=<span>utf</span>-8" /&gt;</span>
&lt;title&gt;My Web Page&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
<span style="color: #ff6600;"><strong><span>&lt;?<span>php</span></span>
<span>$<span>HeadDiv</span> = "&lt;div&gt;&lt;h1&gt;Welcome to my website!&lt;/h1&gt;&lt;/div&gt;";</span>
$BodyDiv = "&lt;div&gt;&lt;p&gt;Are you ready to hire me?! Click on my port...&lt;/p&gt;&lt;/div&gt;";
<span>$<span>FooterDiv</span> = "&lt;div&gt;copyright ...&lt;/div&gt;";</span>
?&gt;
<span>&lt;?<span>php</span></span>
<span>echo $<span>HeadDiv</span>;</span>
<span>echo $<span>BodyDiv</span>;</span>
<span>echo $<span>FooterDiv</span>;</span>
?&gt;</strong>
</span>&lt;/body&gt;
&lt;/html&gt;</pre>
<p><br class="break" /></p>
<h3>After HTML</h3>
<p>This might be an unorthodox way of code execution but nonetheless it is a possibility and depending on your application needs, it might be a useful one. Your code will execute after the HTML so anything that would be rendered above your HTML will render below it (afterwards) now. Here is how that looks. Please comment if you have an idea of how it would be useful.</p>
<pre><span>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.<span>dtd</span>"&gt;</span>
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;head&gt;
<span>&lt;meta http-equiv="Content-Type" content="text/html; <span>charset</span>=<span>utf</span>-8" /&gt;</span>
&lt;title&gt;My WebApp&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
<span>&lt;form action="/code/untitled.<span>php</span>" method="post"&gt;</span>
&lt;input type="text" name="txtMessage" /&gt;
&lt;input type="submit" value="submit" /&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;
<strong><span style="color: #ff6600;"><span>&lt;?<span>php</span> echo $_POST['txtMessage'];?&gt;</span></span></strong></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.quinterox.com/content/code/php-where-to-place-your-code/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
