<?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>Learn PHP Online</title>
	<atom:link href="http://www.learnphponline.com/feed" rel="self" type="application/rss+xml" />
	<link>http://www.learnphponline.com</link>
	<description>Learn PHP Online</description>
	<lastBuildDate>Wed, 06 May 2009 01:02:54 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>How To Find The Current URL In PHP</title>
		<link>http://www.learnphponline.com/php-basics/how-to-find-the-current-url-in-php</link>
		<comments>http://www.learnphponline.com/php-basics/how-to-find-the-current-url-in-php#comments</comments>
		<pubDate>Sun, 05 Apr 2009 19:04:11 +0000</pubDate>
		<dc:creator>Zachary Schuessler</dc:creator>
				<category><![CDATA[PHP Basics]]></category>
		<category><![CDATA[find current url]]></category>
		<category><![CDATA[path to script]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php tutorial]]></category>
		<category><![CDATA[query string]]></category>
		<category><![CDATA[request uri]]></category>
		<category><![CDATA[server_name]]></category>

		<guid isPermaLink="false">http://www.learnphponline.com/?p=114</guid>
		<description><![CDATA[How to find different components of the current URL in PHP.]]></description>
			<content:encoded><![CDATA[<p>In your valiant conquest of the web development industry, you will notice that some scripts will require that you know the current URL the user is browsing to provide certain services. A prime example would be in user management- where we make use of query strings to keep track of users. More practical solutions may even demand that we find the current URL to display relevant ads and increase conversion rates.</p>
<p>PHP has set forth certain global variables that makes this process painfully easy. We will be experimenting with several methods of finding certain parts in the URL within this tutorial. Respectively, they are:</p>
<ol>
<li><a href="#domain">Finding the current domain</a></li>
<li><a href="#script">Finding the path to the script</a></li>
<li><a href="#query">Finding the query string (if any)</a></li>
<li><a href="#uri">Using a special short cut method to tie things together</a></li>
</ol>
<h3 id="domain">Finding The Current Domain In PHP</h3>
<p>If you need the current domain, you can use this neat little snipped below:</p>
<pre>&lt;?php
</span><span style="color: rgb(255, 128, 0);"># Using HTTP_HOST

</span><span style="color: rgb(0, 0, 187);">$domain </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">$_SERVER</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(221, 0, 0);">'HTTP_HOST'</span><span style="color: rgb(0, 119, 0);">];
echo </span><span style="color: rgb(0, 0, 187);">$domain</span><span style="color: rgb(0, 119, 0);">;
</span><span style="color: rgb(0, 0, 187);">?&gt;</span></pre>
<p>If we were to use this directly on this page, the output would be <strong>learnphponline.com</strong> – notice that it does not include the &#8216;http://&#8217; or &#8216;www.&#8217; prefixes. If you are trying to make a link, you could do so by concatenating these prefixes onto the HTTP_HOST server variable.</p>
<h3 id="script">Finding The Path To The Current Script</h3>
<p>If you need to link to the current page, we use the SCRIPT_NAME server variable. We see this in use a lot more than you would think. WordPress installations will link article titles to the same page for several reasons. First, it keeps things user friendly- but it is also great for search engine optimization. Don&#8217;t be afraid to follow their example such as the snippet below shows.</p>
<pre>&lt;?php
</span><span style="color: rgb(255, 128, 0);"># Using SCRIPT_NAME

</span><span style="color: rgb(0, 0, 187);">$path </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">$_SERVER</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(221, 0, 0);">'SCRIPT_NAME'</span><span style="color: rgb(0, 119, 0);">];

echo </span><span style="color: rgb(221, 0, 0);">"Path To Script Example: &lt;a href='$path'&gt;An Article Title&lt;/a&gt;"</span><span style="color: rgb(0, 119, 0);">;

</span><span style="color: rgb(0, 0, 187);">?&gt;</span></pre>
<p>You will notice that the domain section and query string is left out. Instead we get the script path that links nicely to the current page.</p>
<h3 id="query">Finding The Query String In a URL</h3>
<p>The query string is important in passing variables or authorization information across several different pages in your website. You have probably noticed this before when logging into your favorite website and seen something to this effect: &#8220;TheWebsite.com/users/index.php?name=YourName&#8221; </p>
<p>Making a query string is actually quite easy. Make a simple PHP file and create a link to the current file, yet concatenate a ternary symbol and assign a variable like this:</p>
<ul>
<li>&lt;a href=&#8217;www.yoururl.com/index.php?variable=value&#8217;>Test it!&lt;/a></li>
</ul>
<p>This won&#8217;t do anything since we haven&#8217;t coded anything to work with the variable. But it will allow us to test the server variable below.</p>
<pre>&lt;?php
</span><span style="color: rgb(255, 128, 0);"># Using QUERY_STRING

</span><span style="color: rgb(0, 0, 187);">$queryString </span><span style="color: rgb(0, 119, 0);">= </span><span style="color: rgb(0, 0, 187);">$_SERVER</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(221, 0, 0);">'QUERY_STRING'</span><span style="color: rgb(0, 119, 0);">];

echo </span><span style="color: rgb(221, 0, 0);">"Query: " </span><span style="color: rgb(0, 119, 0);">. </span><span style="color: rgb(0, 0, 187);">$queryString</span><span style="color: rgb(0, 119, 0);">;

</span><span style="color: rgb(0, 0, 187);">?&gt;</span></pre>
<h3 id="uri">Finding The Current URL With Request URI</h3>
<p>If you are using MOD REWRITE to make your URLs more user-friendly, there is still a way to get the original URL. By using the REQUEST_URI server variable, we can get the URL given to access the page. So be definition, we bypass any rewrite rules.</p>
<pre>&lt;?php
</span><span style="color: rgb(255, 128, 0);"># Using REQUEST_URI

</span><span style="color: rgb(0, 119, 0);">echo </span><span style="color: rgb(221, 0, 0);">"http://" </span><span style="color: rgb(0, 119, 0);">. </span><span style="color: rgb(0, 0, 187);">$_SERVER</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(221, 0, 0);">'HTTP_HOST'</span><span style="color: rgb(0, 119, 0);">]  . </span><span style="color: rgb(0, 0, 187);">$_SERVER</span><span style="color: rgb(0, 119, 0);">[</span><span style="color: rgb(221, 0, 0);">'REQUEST_URI'</span><span style="color: rgb(0, 119, 0);">];

</span><span style="color: rgb(0, 0, 187);">?&gt;</span></pre>
<p>This saves a little bit of space over the previous examples, since REQUEST_URI can replace the script path and query string server variables. This is best used when you don&#8217;t need these variables separated, which you commonly do.</p>
<h3>Security Issues To Consider</h3>
<p>There are many ways to get the current URL, but the ones mentioned here are the safest. The server variable PHP_SELF is an example of a method that can result in cross-scripting attacks (XSS). Instead make sure you use the SCRIPT_NAME variable as we did above in our examples.</p>
<p>Also make note that header information can be faked. Any variable that includes &#8220;HTTP&#8221; in it has a potential to be untrustworthy data. There are always verification methods and alternatives to these pitfalls, so you aren&#8217;t without options.</p>
<p>A humorous example of how HTTP headers can be faked is with certain security software packages that rewrite referrer information that websites use for analytics. By setting your referrer field to something such as &#8220;FBI&#8221; or &#8220;CIA&#8221; you can effectively give the statistics-conscious webmaster a nice scare.</p>
<h4>Closing Comments</h4>
<p>As a last note, be sure to encode any URL information you make use of if you are using these server variables for your own coding experiments. Otherwise malicious users may take advantage of your inept security tactics and cause havoc on your database.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learnphponline.com/php-basics/how-to-find-the-current-url-in-php/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>A Simple Yet Elegant Contact Form In PHP</title>
		<link>http://www.learnphponline.com/scripts/a-simple-yet-elegant-contact-form-in-php</link>
		<comments>http://www.learnphponline.com/scripts/a-simple-yet-elegant-contact-form-in-php#comments</comments>
		<pubDate>Sun, 05 Apr 2009 00:29:27 +0000</pubDate>
		<dc:creator>Zachary Schuessler</dc:creator>
				<category><![CDATA[PHP Scripts]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php captcha]]></category>
		<category><![CDATA[php contact form]]></category>
		<category><![CDATA[php email]]></category>
		<category><![CDATA[php simple contact form]]></category>
		<category><![CDATA[php tutorial]]></category>

		<guid isPermaLink="false">http://www.learnphponline.com/?p=97</guid>
		<description><![CDATA[A guide that teaches developers how to create a simple yet secure contact form.]]></description>
			<content:encoded><![CDATA[<p style="text-align: center;"><img class="size-full wp-image-96 aligncenter" title="php-simple-contact-form" src="http://www.learnphponline.com/wp-content/uploads/2009/04/php-simple-contact-form.jpg" alt="PHP Simple Contact Form" width="450" height="325" /></p>
<p style="text-align: center;"><span style="text-decoration: underline;">3 Files Used:<br />
</span></p>
<ol style="text-align: center;">
<li>contact.php</li>
<li>captcha.php</li>
<li>captcha.png</li>
</ol>
<p style="text-align: center;"><span style="text-decoration: underline;">Requirements:</span></p>
<ol style="text-align: center;">
<li>GD Library Installed (<em>For Captacha- it&#8217;s 99% likely it&#8217;s already installed</em>)<span style="text-decoration: underline;"><br />
</span></li>
</ol>
<hr />Simplicity is bliss. Unfortunately for webmasters, a simple and elegant contact form is hard to come by. Sure, it may be nice to include nifty AJAX validation techniques and a Captcha system that only a supercomputer could crack, but if you are anything like the LearnPHPOnline.com development team, you prefer a minimalistic approach to web development over a cluttered PHP script.</p>
<p>In this tutorial you will learn how to create a stylized form in HTML, code the backend in PHP to send an email to a specific address, and finally add a moderate Captcha system to thwart spammers from maliciously using your form.</p>
<h3>Getting Started – Building The HTML Form And Style</h3>
<p>We&#8217;ll start this off with a simple HTML template, that you are likely all too familiar with:</p>
<pre><span style="color: #0000ff;">&lt;!</span><span style="color: #ff00ff;">DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">html </span><span style="color: #ff0000;">xmlns</span><span style="color: #0000ff;">="http://www.w3.org/1999/xhtml"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">html</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">head</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">title</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">Contact Form - YourWebsite.com</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">title</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">head</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">body</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">body</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">html</span><span style="color: #0000ff;">&gt;</span></pre>
<p>We will be using a CSS layout to organize the information. To help keep things simple, we have used inline styling- but do feel free to make your own remote CSS file to further cut down on code count.</p>
<pre><span style="color: #000000;">
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;html&gt;
&lt;head&gt;

&lt;title&gt;Contact Form - YourWebsite.com&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;</span>
<span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="width:650px;border-left:1px solid black;border-right:1px solid gray;margin:0px auto;overflow:hidden;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">h1 </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="background-color:#1c5665;color:white;padding:5px;text-align:center;margin-top:0px;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">Contact Form - YourWebsite.com</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">h1</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

  </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="float:left;width:214px;border-right:1px solid gray;padding:5px;background-color:#f6f8f9;height:100%;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

    </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">p </span><span style="color: #ff0000;">align</span><span style="color: #0000ff;">="right"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="padding:5px;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">Name:</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">p </span><span style="color: #ff0000;">align</span><span style="color: #0000ff;">="right"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="padding:5px;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">Email Address:</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

    </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">p </span><span style="color: #ff0000;">align</span><span style="color: #0000ff;">="right"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="padding:5px;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">Comment/Suggestion:</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

  </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="float:left;width:415px;padding:5px;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

    </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="name"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="border:1px solid #1c5665;padding:3px;margin-top:5px;"</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
    </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="text"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="email"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="border:1px solid #1c5665;padding:3px;margin-top:5px;"</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

    </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;&lt;</span><span style="color: #800000;">textarea </span><span style="color: #ff0000;">cols</span><span style="color: #0000ff;">="40"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="comment"</span><span style="color: #ff0000;"> rows</span><span style="color: #0000ff;">="4"</span><span style="color: #ff0000;"> style</span><span style="color: #0000ff;">="border:1px solid #1c5665;padding:3px;margin-top:5px;"</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">textarea</span><span style="color: #0000ff;">&gt;&lt;/</span><span style="color: #800000;">p</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

  </span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

  </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="clear:both;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #ff0000;">&amp;nbsp;</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">hr </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="color:gray"</span><span style="color: #ff0000;"> </span><span style="color: #0000ff;">/&gt;</span><span style="color: #000000;">

  </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="submit"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="Submit Form"</span><span style="color: #ff0000;"> </span><span style="color: #0000ff;">/&gt;</span><span style="color: #000000;">
 </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">input </span><span style="color: #ff0000;">type</span><span style="color: #0000ff;">="hidden"</span><span style="color: #ff0000;"> name</span><span style="color: #0000ff;">="form_submitted"</span><span style="color: #ff0000;"> value</span><span style="color: #0000ff;">="1"</span><span style="color: #0000ff;">/&gt;</span><span style="color: #000000;">
      </span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">form</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="width:650px;height:10px;background-color:#1c5665;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #ff0000;">&amp;nbsp</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span>
<span style="color: #000000;">
&lt;/body&gt;
&lt;/html&gt;</span></pre>
<p>So far things should be fairly simple if you already have some knowledge about HTML and basic forms. One thing that seems to stump a lot of developers is the float property in CSS. To properly create a structured float, you will need four components: the wrapper, two or more sections of content that are using the float property, and finally a DIV that clears the float. You can see these, respectively, below:</p>
<pre><span style="color: #008000;">&lt;!--</span><span style="color: #008000;">WRAPPER </span><span style="color: #008000;">--&gt;</span><span style="color: #000000;">
</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="width:650px;border-left:1px solid black;border-right:1px solid gray;margin:0px auto;overflow:hidden;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

</span><span style="color: #008000;">&lt;!--</span><span style="color: #008000;">FLOATED CONTENT 1 </span><span style="color: #008000;">--&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="float:left;width:214px;border-right:1px solid gray;padding:5px;background-color:#f6f8f9;height:100%;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

(content)
  </span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

</span><span style="color: #008000;">&lt;!--</span><span style="color: #008000;">FLOATED CONTENT 2 </span><span style="color: #008000;">--&gt;</span><span style="color: #000000;">
</span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="float:left;width:415px;padding:5px;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
(content 2)
</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">

</span><span style="color: #008000;">&lt;!--</span><span style="color: #008000;">CLEAR FLOAT </span><span style="color: #008000;">--&gt;</span><span style="color: #000000;">
  </span><span style="color: #0000ff;">&lt;</span><span style="color: #800000;">div </span><span style="color: #ff0000;">style</span><span style="color: #0000ff;">="clear:both;"</span><span style="color: #0000ff;">&gt;</span><span style="color: #ff0000;">&amp;nbsp;</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span><span style="color: #000000;">
</span><span style="color: #0000ff;">&lt;/</span><span style="color: #800000;">div</span><span style="color: #0000ff;">&gt;</span></pre>
<p>And that&#8217;s it! The HTML form is done for now. Now let&#8217;s go on with sending the form contents to a specified email address.</p>
<h3>Sending Mail In PHP</h3>
<p>We like to keep file count as small as possible, so we will be recycling the same file we just created to act as both the form and the file that processes the data to email the contents to your email address. We can do this with a simple IF structure in PHP.</p>
<p>You may have noticed this hidden value in the form we created:</p>
<ul>
<li>&lt;input type=&#8221;hidden&#8221; name=&#8221;form_submitted&#8221; value=&#8221;1&#8243;/&gt;</li>
</ul>
<p>This is going to allow us to see whether or not the user is submitting the form, or if they are filling it out. Since this is only true once the &#8220;Submit Form&#8221; button is clicked, we can use the following IF structure in conjunction:</p>
<pre>&lt;?php
<span style="color: #007700;">if (</span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'form_submitted'</span><span style="color: #007700;">] != </span><span style="color: #dd0000;">'1'</span><span style="color: #007700;">) {
</span><span style="color: #ff8000;"># Form not submitted, show form

</span><span style="color: #007700;">} else {
</span><span style="color: #ff8000;"># Process script, form was submitted
</span><span style="color: #007700;">}
</span><span style="color: #0000bb;">?&gt;</span></pre>
<p>To save you hassle, you can easily implement this selection structure by nesting in around the HTML:</p>
<pre>&lt;?php
    <span style="color: #007700;">if (</span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'form_submitted'</span><span style="color: #007700;">] != </span><span style="color: #dd0000;">'1'</span><span style="color: #007700;">) {
</span><span style="color: #ff8000;"># Form was not submitted, show form

</span><span style="color: #0000bb;">?&gt;
</span>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;html&gt;
&lt;head&gt;

&lt;title&gt;Contact Form - YourWebsite.com&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div style="width:650px;border-left:1px solid black;border-right:1px solid gray;margin:0px auto;overflow:hidden;"&gt;

<em>...[form contents]...</em>

&lt;/div&gt;

<span style="color: #0000bb;">&lt;?php </span><span style="color: #007700;">} else if (</span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #0000bb;">form_submitted</span><span style="color: #007700;">] == </span><span style="color: #0000bb;">1</span><span style="color: #007700;">) { </span><span style="color: #0000bb;">?&gt;

</span>
<span style="color: #0000bb;">&lt;?php
</span><span style="color: #ff8000;"># Code to send email goes here, since form was submitted
</span><span style="color: #0000bb;">?&gt;
</span>
<span style="color: #0000bb;">&lt;?php </span><span style="color: #007700;">}  </span><span style="color: #0000bb;">?&gt;

</span>
&lt;/div&gt;
&lt;/body&gt;

&lt;/html&gt;</pre>
<p>Now, onto the email function! PHP developers can make use of the mail() function already included in the PHP language. This function takes four main parameters to function correctly. As you&#8217;ll see below, we can simply plug in the information we just got from the submitted form&#8211; and the function takes care of the rest.</p>
<pre>// Send

<span style="color: #0000bb;">$to      </span><span style="color: #007700;">= </span><span style="color: #dd0000;">'yourname@yourdomain.com'</span><span style="color: #007700;">;
</span><span style="color: #0000bb;">$subject </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"Message from " </span><span style="color: #007700;">. </span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'name'</span><span style="color: #007700;">];

</span><span style="color: #0000bb;">$message </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'comment'</span><span style="color: #007700;">];
</span><span style="color: #0000bb;">$headers </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"From: " </span><span style="color: #007700;">. </span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'email'</span><span style="color: #007700;">] . </span><span style="color: #dd0000;">"\r\n" </span><span style="color: #007700;">.

    </span><span style="color: #dd0000;">"Reply-To: "</span><span style="color: #007700;">.</span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'name'</span><span style="color: #007700;">] . </span><span style="color: #dd0000;">"\r\n" </span><span style="color: #007700;">.
    </span><span style="color: #dd0000;">'X-Mailer: PHP/' </span><span style="color: #007700;">. </span><span style="color: #0000bb;">phpversion</span><span style="color: #007700;">();

</span><span style="color: #0000bb;">mail</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$to</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$subject</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$message</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$headers</span><span style="color: #007700;">);

echo </span><span style="color: #dd0000;">"&lt;html&gt;&lt;body style='background-color:#ececec;'&gt;&lt;div style='width:300px;border:1px dashed black;text-align:center;margin:0px auto;margin-top:200px;padding:20px;font-size:20px;background-color:white;'&gt;Your comment has been sent! Thanks!&lt;/div&gt;&lt;/html&gt;"</span><span style="color: #007700;">;

</span></pre>
<p>It looks like we would be done. But, wait- what about those <em>dastardly </em>spammers?</p>
<h3>Creating A Simple Captcha For PHP Forms</h3>
<p>Some Captcha scripts are quite confusing; making use of many different technologies to provide the best possible protection against spam robots. Even though Captchas have been cracked, they are hard to do so- and certainly will cut out almost all of your spam, if not all of it completely.</p>
<p style="text-align: center;"><span style="color: #000000;">In this example we are using a fairly secure Captcha system. First we are going to need a background image for our Captcha, which you  can download via &#8220;Save As..&#8221; here:</span><br />
<img class="size-full wp-image-98 aligncenter" title="captcha background" src="http://www.learnphponline.com/wp-content/uploads/2009/04/captcha.png" alt="Captcha Background Image" width="87" height="35" /></p>
<p>Now we will have to create a <strong>captcha.php</strong> file to build the image we will be using via the GD Library. Follow the comments in the script to help get an understanding of how it works:</p>
<pre>&lt;?php
session_start<span style="color: #007700;">();

</span><span style="color: #ff8000;">// Generate a Random String, Based On Time
</span><span style="color: #0000bb;">$md5 </span><span style="color: #007700;">= </span><span style="color: #0000bb;">md5</span><span style="color: #007700;">(</span><span style="color: #0000bb;">microtime</span><span style="color: #007700;">() * </span><span style="color: #0000bb;">mktime</span><span style="color: #007700;">());

</span><span style="color: #ff8000;"> //We don't need a 32 character long string, let's trim it
 <span style="color: #0000ff;">$string = substr($md5,0,5);</span></span><span style="color: #007700;">

</span><span style="color: #ff8000;">// Use GD Library to make a PNG from a file
</span><span style="color: #0000bb;">$captcha </span><span style="color: #007700;">= </span><span style="color: #0000bb;">imagecreatefrompng</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"captcha.png"</span><span style="color: #007700;">);

</span><span style="color: #ff8000;">// Set colors of lines with RGB colors
</span><span style="color: #0000bb;">$black </span><span style="color: #007700;">= </span><span style="color: #0000bb;">imagecolorallocate</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$captcha</span><span style="color: #007700;">, </span><span style="color: #0000bb;">0</span><span style="color: #007700;">, </span><span style="color: #0000bb;">0</span><span style="color: #007700;">, </span><span style="color: #0000bb;">0</span><span style="color: #007700;">);

</span><span style="color: #0000bb;">$line </span><span style="color: #007700;">= </span><span style="color: #0000bb;">imagecolorallocate</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$captcha</span><span style="color: #007700;">,</span><span style="color: #0000bb;">233</span><span style="color: #007700;">,</span><span style="color: #0000bb;">239</span><span style="color: #007700;">,</span><span style="color: #0000bb;">239</span><span style="color: #007700;">);

</span><span style="color: #ff8000;">// The following creates random lines to help throw off a spam robot's ability to guess the string

</span><span style="color: #0000bb;">imageline</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$captcha</span><span style="color: #007700;">,</span><span style="color: #0000bb;">0</span><span style="color: #007700;">,</span><span style="color: #0000bb;">10</span><span style="color: #007700;">,</span><span style="color: #0000bb;">50</span><span style="color: #007700;">,</span><span style="color: #0000bb;">16</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$black</span><span style="color: #007700;">);
</span><span style="color: #0000bb;">imageline</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$captcha</span><span style="color: #007700;">,</span><span style="color: #0000bb;">40</span><span style="color: #007700;">,</span><span style="color: #0000bb;">11</span><span style="color: #007700;">,</span><span style="color: #0000bb;">64</span><span style="color: #007700;">,</span><span style="color: #0000bb;">29</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$black</span><span style="color: #007700;">);

</span><span style="color: #0000bb;">imageline</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$captcha</span><span style="color: #007700;">,</span><span style="color: #0000bb;">0</span><span style="color: #007700;">,</span><span style="color: #0000bb;">60</span><span style="color: #007700;">,</span><span style="color: #0000bb;">90</span><span style="color: #007700;">,</span><span style="color: #0000bb;">0</span><span style="color: #007700;">,</span><span style="color: #0000bb;">$black</span><span style="color: #007700;">);

</span><span style="color: #ff8000;">//Write the string to the image

</span><span style="color: #0000bb;">imagestring</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$captcha</span><span style="color: #007700;">, </span><span style="color: #0000bb;">5</span><span style="color: #007700;">, </span><span style="color: #0000bb;">20</span><span style="color: #007700;">, </span><span style="color: #0000bb;">10</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$string</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$black</span><span style="color: #007700;">);

</span><span style="color: #ff8000;">// Use MD5 encryption on the key, and store it for a comparison test later

</span><span style="color: #0000bb;">$_SESSION</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'key'</span><span style="color: #007700;">] = </span><span style="color: #0000bb;">md5</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$string</span><span style="color: #007700;">);

</span><span style="color: #ff8000;">// Print out the image
</span><span style="color: #0000bb;">header</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"Content-type: image/png"</span><span style="color: #007700;">);

</span><span style="color: #0000bb;">imagepng</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$captcha</span><span style="color: #007700;">);
</span><span style="color: #0000bb;">?&gt;</span></pre>
<p>This script is really neat, considering the .PHP file is treated as a .PNG image if successfully executed. This way, we can simply call to the image from the form and have a dynamic image to test our users with!</p>
<p>Now let&#8217;s go back to our form and make some necessary changes, as seen in red words below. This is the final version of the script- enjoy!</p>
<pre>&lt;?php
    <span style="color: #800000;"><strong>session_start();</strong></span>
    if (<span style="color: #000000;">$_POST['form_submitted'] != '1'</span><span style="color: #007700;"><span style="color: #000000;">) {</span>
</span><span style="color: #0000bb;">?&gt;

</span>
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;
&lt;html&gt;
&lt;head&gt;

&lt;title&gt;Contact Form - YourWebsite.com&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div style="width:650px;border-left:1px solid black;border-right:1px solid gray;margin:0px auto;overflow:hidden;"&gt;

  &lt;h1 style="background-color:#1c5665;color:white;padding:5px;text-align:center;margin-top:0px;"&gt;Contact Form - YourWebsite.com&lt;/h1&gt;

  &lt;div style="float:left;width:214px;border-right:1px solid gray;padding:5px;background-color:#f6f8f9;height:100%;"&gt;
    &lt;form method="post"&gt;

    &lt;p align="right" style="padding:5px;"&gt;Name:&lt;/p&gt;
    &lt;p align="right" style="padding:5px;"&gt;Email Address:&lt;/p&gt;
    &lt;p align="right" style="padding:5px;"&gt;Comment/Suggestion:&lt;/p&gt;

  &lt;/div&gt;

  &lt;div style="float:left;width:415px;padding:5px;"&gt;
    &lt;p&gt;&lt;input type="text" name="name" style="border:1px solid #1c5665;padding:3px;margin-top:5px;"&gt;&lt;/p&gt;
    &lt;p&gt;&lt;input type="text" name="email" style="border:1px solid #1c5665;padding:3px;margin-top:5px;"&gt;&lt;/p&gt;

    &lt;p&gt;&lt;textarea cols="40" name="comment" rows="4" style="border:1px solid #1c5665;padding:3px;margin-top:5px;"&gt;&lt;/textarea&gt;&lt;/p&gt;
  &lt;/div&gt;

  &lt;div style="clear:both;"&gt;&amp;nbsp;&lt;/div&gt;

  &lt;hr style="color:gray" /&gt;

 <span style="color: #800000;"> <strong>&lt;div style="width:325px; border:1px solid black;margin:0px auto;text-align:center;"&gt;
    &lt;p&gt;&lt;img src="captcha.php" /&gt;&lt;/p&gt;

      &lt;div style="margin-top:-15px;"&gt;
        Please enter the image text:
      &lt;/div&gt;
      &lt;div style="margin-top:-3px;margin-bottom: 4px;"&gt;
        &lt;input type="text" name="code" style="border:1px solid #1c5665;padding:3px;margin-top:5px;"&gt;

      &lt;/div&gt;
      &lt;input type="submit" value="Submit Form" /&gt;
      &lt;input type="hidden" name="form_submitted" value="1"/&gt;
      &lt;/form&gt;
  &lt;/div&gt;</strong>
</span>
  &lt;div style="width:650px;height:10px;background-color:#1c5665;"&gt;&amp;nbsp&lt;/div&gt;

<span style="color: #000000;">&lt;?php } else if ($_POST[form_submitted] == 1) { ?&gt;

&lt;?php
<span style="color: #800000;"><strong>//Encrypt the posted code field and then compare with the stored key

if(md5($_POST['code']) != $_SESSION['key'])

{
  echo "It seems you entered an invalid Captcha key. Please go back and try again.";

}else{
session_unset();
session_destroy();</strong></span>
// Send

$to      = 'yourname@yourwebsite.com';
$subject = "Message from " . $_POST['name'];

$message = $_POST['comment'];
$headers = "From: " . $_POST['email'] . "\r\n" .

    "Reply-To: ".$_POST['name'] . "\r\n" .
    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

echo "&lt;html&gt;&lt;body style='background-color:#ececec;'&gt;&lt;div style='width:300px;border:1px dashed black;text-align:center;margin:0px auto;margin-top:200px;padding:20px;font-size:20px;background-color:white;'&gt;Your comment has been sent! Thanks!&lt;/div&gt;";

}
?&gt;
&lt;?php }  </span>

<span style="color: #0000bb;"><span style="color: #000000;">?&gt;</span>
</span>
&lt;/div&gt;
&lt;/body&gt;

&lt;/html&gt;</pre>
<p>Note in particular that we are using the <strong>Session Unset</strong> and <strong>Session Destroy</strong> functions. Without them, you could refresh the page continually after you passed the Captcha test and send as much spam as you would like- &#8211; despite our efforts!</p>
<h4>Closing Comments</h4>
<p style="text-align: center;">Our form is still missing several things. You will likely need to add form validation, such as checking to see if all fields were filled out. You may also tweak the Captcha system to your liking- which is a great way to get experience with the GD Library. You may also want to consider that not everyone can see or hear- and that the Captcha in its current state will prevent such users from sending you mail.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learnphponline.com/scripts/a-simple-yet-elegant-contact-form-in-php/feed</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
		<item>
		<title>URL File-Access is Disabled in the Server Configuration</title>
		<link>http://www.learnphponline.com/errors/url-file-access-is-disabled-in-the-server-configuration</link>
		<comments>http://www.learnphponline.com/errors/url-file-access-is-disabled-in-the-server-configuration#comments</comments>
		<pubDate>Fri, 03 Apr 2009 23:59:46 +0000</pubDate>
		<dc:creator>Zachary Schuessler</dc:creator>
				<category><![CDATA[PHP Errors]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php debug]]></category>
		<category><![CDATA[php errors]]></category>
		<category><![CDATA[php help]]></category>
		<category><![CDATA[url file access]]></category>

		<guid isPermaLink="false">http://www.learnphponline.com/?p=86</guid>
		<description><![CDATA[How to fix the PHP error: URL File-Access is Disabled in the Server Configuration]]></description>
			<content:encoded><![CDATA[<p style="border-top: 1px solid blue; border-bottom: 1px solid blue; margin: 0px auto; padding: 10px; width: 95%; background-color: #e0eaef; text-indent: 0px;"><strong>Warning: include()  [function.include]: URL file-access is disabled in the server configuration</strong> is an error obtained by using the include command. Lucky for webmasters, this error is easily fixed via several different methods.</p>
<h3>Why This Error Occurs</h3>
<p>If you&#8217;re seeing this error, we are willing to bet that you are using the include statement as seen below:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Common Include File Usage</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

</span><span style="color: #007700;">include (</span><span style="color: #dd0000;">"http://www.YourDomain.com/includes/header.php"</span><span style="color: #007700;">);

</span><span style="color: #0000bb;">?&gt;</span> </span></pre>
<p>And you are more than likely getting an error similar to the following:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Include File Error Message</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

</span><span style="color: #ff8000;">/*Warning: include() [function.include]: URL file-access is disabled in the server configuration in /home/YourUsername/public_html/index.php on line xx */

/*Warning: include(http://www.YourDomain.com/index.php) [function.include]: failed to open stream: no suitable wrapper could be found in /home/YourUsername/public_html/index.php on line xx*/

/*Warning: include() [function.include]: Failed opening 'http://www.YourDomain.com/index.php' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in /home/YourUsername/public_html/index.php on line xx*/
</span><span style="color: #0000bb;">?&gt;</span> </span></pre>
<p>What specifically causes this error is the fact that the server has upgraded from PHP 4 to a newer version. In the upgrade, the <strong>allow_url_fopen</strong> is set to <strong>OFF</strong>, which is responsible for disallowing include files to use absolute file paths.</p>
<p>Don&#8217;t be in a rush to turn this off in your system configuration just yet! Any upgrades past PHP 4 will turn <strong>allow_url_fopen</strong> to <strong>OFF</strong> as default due to security concerns. This is most prevalent in cross-site scripting attacks, or XSS attacks. In some cases, malicious users have even enslaved a server to become a spam-email-sending nightmare: all without the administrator noticing!</p>
<p>If there is any part of a website that allows a user to upload data of some sort, there are vulnerabilities that are present with poor coding that may allow malicious users to inject an include statement. By allowing <strong>allow_url_fopen</strong> to be set to <strong>ON</strong>, it allows them to include any file they wish from any website on the Internet. With it set to <strong>OFF</strong>, only documents on the server may be included. This is much safer considering you probably don&#8217;t have malicious code stored on the server. (And theoretically, if you did, hackers probably wouldn&#8217;t be able to find it)</p>
<h3>The First Solution: Use Relative File Paths</h3>
<p>A web server will automatically assume that the code below belongs on the server, and thus, is not a remote file:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">PHP Include File With Relative Paths</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

</span><span style="color: #007700;">include (</span><span style="color: #0000bb;">header</span><span style="color: #007700;">.</span><span style="color: #0000bb;">php</span><span style="color: #007700;">); </span><span style="color: #ff8000;">//This file is in the same directory as the PHP file

</span><span style="color: #007700;">include (</span><span style="color: #0000bb;">includes</span><span style="color: #007700;">/</span><span style="color: #0000bb;">header</span><span style="color: #007700;">.</span><span style="color: #0000bb;">php</span><span style="color: #007700;">); </span><span style="color: #ff8000;">//This file is in a directory under the PHP file

</span><span style="color: #007700;">include (../</span><span style="color: #0000bb;">header</span><span style="color: #007700;">.</span><span style="color: #0000bb;">php</span><span style="color: #007700;">); </span><span style="color: #ff8000;">//This file is in the directory above the current PHP file

</span><span style="color: #0000bb;">?&gt;</span>
</span></pre>
<p>Relative file paths can be used in every legitimate situation an absolute path would be used, although it may take a little more work. As in the example above, you may have to work at determining where the file you wish to include exists in relation to the PHP file being run.</p>
<p>Not your idea of fun? We aren&#8217;t fond of it either, so on to the next solution!</p>
<h3>The Second Solution: Use Another PHP Function</h3>
<p>We may substitute the include statement with <strong>file_get_contents</strong>, which reads an entire file into a string.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">PHP Include With File_Get_Contents</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

$includeFile </span><span style="color: #007700;">= </span><span style="color: #0000bb;">file_get_contents</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"http://www.YourDomain.com/includes/header.php"</span><span style="color: #007700;">);

echo </span><span style="color: #0000bb;">$includeFile</span><span style="color: #007700;">;
</span><span style="color: #0000bb;">?&gt;

</span>
</span></pre>
<p>This is a good alternative to keep the absolute path an option in including a certain file. There are some instances where the above code wouldn&#8217;t come out as planned, depending on the situation. In addition, it adds another line of code that we can relinquish with the best solution: using a server variable.</p>
<h3>The Best Solution: Using Server Variables</h3>
<p>If you don&#8217;t want to spend hours rearranging code, you can do it the easy way with $_SERVER['DOCUMENT_ROOT'].</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">PHP Include Server Variables</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php 

</span><span style="color: #007700;">include </span><span style="color: #0000bb;">$_SERVER</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'DOCUMENT_ROOT'</span><span style="color: #007700;">] . </span><span style="color: #dd0000;">'/includes/header.php'</span><span style="color: #007700;">; 

</span><span style="color: #0000bb;">?&gt;

</span>
</span></pre>
<p>This allows you to keep the absolute path that you&#8217;ve come to be familiar with in using the include statement. Technically, the $_SERVER['DOCUMENT_ROOT'] command gives your path to the public_html directory, as seen below:</p>
<li style="margin-left: 30px;">/home/Your_Username/public_html</li>
<p>Essentially this is the root of your website, www.YourDomain.com, and therefore, you can use it just as you would with any other include statement. Just replace www.YourDomain.com with the server variable and you&#8217;re set!</p>
<h3>Should I Turn On allow_url_fopen?</h3>
<p>The short answer: <strong>no</strong>; allow_url_fopen was turned off for a reason. If you don&#8217;t own your own server, odds are your host won&#8217;t even allow the change in the first place. If you do own your own server, realize that the third solution presented in this tech tip takes almost no time to implement, and only requires that the base URL be replaced with a server variable.</p>
<h4>Closing Comments</h4>
<p>The bad news is that you&#8217;ll probably have to switch many pages in your website to conform to the new standard. The good news is that once that&#8217;s done you&#8217;ll have a more secure server, and by following the new syntax, you&#8217;ll never have to change the code again.</p></div>
]]></content:encoded>
			<wfw:commentRss>http://www.learnphponline.com/errors/url-file-access-is-disabled-in-the-server-configuration/feed</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>Email Activation For Registration Forms</title>
		<link>http://www.learnphponline.com/scripts/email-activation-for-php-forms</link>
		<comments>http://www.learnphponline.com/scripts/email-activation-for-php-forms#comments</comments>
		<pubDate>Fri, 03 Apr 2009 23:31:16 +0000</pubDate>
		<dc:creator>Zachary Schuessler</dc:creator>
				<category><![CDATA[PHP Scripts]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php email]]></category>
		<category><![CDATA[php email activation]]></category>
		<category><![CDATA[php scripts]]></category>
		<category><![CDATA[php tutorials]]></category>

		<guid isPermaLink="false">http://www.learnphponline.com/?p=53</guid>
		<description><![CDATA[How to make use of email activation functionality in PHP registration forms.]]></description>
			<content:encoded><![CDATA[<h3>Email Activation For Registration Forms</h3>
<p style="border-top: 1px solid blue; border-bottom: 1px solid blue; margin: 0px auto; padding: 10px; width: 95%; background-color: #e0eaef; text-indent: 0px;"><strong>Foreword</strong>: There are two good reasons why email activation is a necessary for webmasters. First, it helps root out  spam by requiring user interaction. It also creates a sense of trust, since we can build a certain amount of trust with a user who can confirm they are who they say they are. In this example you will need access to a database (MySQL is what we&#8217;ll use) with proper permissions.</p>
<p>We&#8217;ll start out creating the interface of the form. You will need to create two files: one file to hold the form, the next file to handle the verification process and interface with the database. It doesn&#8217;t necessarily matter what you name your files, but to stay uniform with our examples name the registration and verification files register.php and verify.php, respectively.</p>
<p>Below you will see register.php in action- in all its simplistic glory.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Our Registration Form &#8211; register.php</p>
<pre style="border: 1px solid black; padding: 10px;">&lt;html&gt;
 &lt;body&gt;
  &lt;form action="verify.php" method="post" name="register"&gt;
    Username: &lt;input type="text" name="username" /&gt;
    Password: &lt;input type="text" name="password" /&gt;
    Email: &lt;input type="text" name="email" /&gt;
  &lt;input type="submit" /&gt;
  &lt;/form&gt;
 &lt;/body&gt;

&lt;/html&gt;</pre>
<p>At this point the only things worth mentioning is that we are putting &#8220;verify.php&#8221; as the form action, and naming the form &#8220;register&#8221; with the name command. Go ahead and save this file and upload it to your hosting account- we&#8217;re done with this file for now.</p>
<h3>Groundwork For The Verification Process</h3>
<p>Now let&#8217;s create a file named verify.php. We are using this file for two things. First, we use it to insert data into the database if everything seems to be hunky-dory. But we also use it to confirm the verification code we email the user, so we&#8217;ll need to make use of the &#8220;IF&#8221; selection structure to differentiate between the two processes.</p>
<p>So how do we know if the verify.php file should submit data to our database or verify the activation code a user provides? We&#8217;ll admit that when we said we were done with register.php, we lied. To properly determine if the user is submitting data or verifying a code, we need to add a hidden value on the registration form, as seen below:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Hidden Values For Our Registration Form &#8211; register.php</p>
<pre style="border: 1px solid black; padding: 10px;color:gray;">&lt;html&gt;
 &lt;body&gt;
  &lt;form action="verify.php" method="post" name="register"&gt;
    Username: &lt;input type="text" name="username" /&gt;
    Password: &lt;input type="text" name="password" /&gt;
    Email: &lt;input type="text" name="email" /&gt;
	<strong style="color:black">&lt;input type="hidden" name="form_submitted" value="1"/&gt; </strong>
  &lt;input type="submit" /&gt;
  &lt;/form&gt;
 &lt;/body&gt;
&lt;/html&gt;</pre>
<p>Now we can check to see if this value is set on our verify.php file with the following code:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Selection Structure &#8211; verify.php</p>
<pre style="border: 1px solid black; padding: 10px;color:gray;"><span style="color: #007700;">if (</span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'form_submitted'</span><span style="color: #007700;">] == </span><span style="color: #dd0000;">'1'</span><span style="color: #007700;">) {
</span><span style="color: #007700;">
</span><span style="color: #ff8000;">## Form was submitted,the user is registering!

 </span><span style="color: #007700;">} else{

</span><span style="color: #ff8000;">## No value found, user must be activating their account!

</span><span style="color: #007700;">}</span></pre>
<p>With our form and selection structure in place, we need to go to our &#8220;backend&#8221; and create a database.</p>
<h3>Database Design For Email Activation</h3>
<p>In our example we are creating a table named &#8220;users&#8221; with the fields &#8220;id, status, username, password, email, and activationkey&#8221; &#8211; we encourage you to use the same values for the sake of simplicity. In fact, you can just run the SQL query below and do just that:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">SQL Query Code &#8211; Run Code to Create Table And Fields</p>
<pre style="border: 1px solid black; padding: 10px;">CREATE TABLE IF NOT EXISTS `users` (
  `id` int(11) NOT NULL auto_increment,
  `status` varchar(20) NOT NULL,
  `username` varchar(20) NOT NULL,
  `password` varchar(20) NOT NULL,
  `email` varchar(20) NOT NULL,
  `activationkey` varchar(100) NOT NULL,
  PRIMARY KEY  (`id`),
  UNIQUE KEY `username` (`username`),
  UNIQUE KEY `email` (`email`),
  UNIQUE KEY `activationkey` (`activationkey`)
) ENGINE=MyISAM  DEFAULT CHARSET=latin1 AUTO_INCREMENT=9 ;</pre>
<p>If all has gone well, your database should look something like the following (note if you aren&#8217;t using PHPMyAdmin and MySQL, you may see some differences):</p>
<p><img style="margin: 0px auto; width: 500px; display: block;" title="PHP MySQL Screenshot" src="../mysqltable.jpg" alt="" /></p>
<h3>Inserting Registration Data Into Database With PHP</h3>
<p>Now that we have a good grasp on where we are going, we can go ahead and connect to our database. First we&#8217;ll need to arrange the correct connection statement. We will be using the <span style="color: #0000bb;">mysql_connect</span> and <span style="color: #0000bb;">mysql_select_db functions</span> to make the connection to our database.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Connecting To The Database &#8211; verify.php</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #0000bb;">mysql_connect</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"localhost"</span><span style="color: #007700;">, </span><span style="color: #0000bb;">DATABASE</span><span style="color: #007700;">, </span><span style="color: #0000bb;">PASSWORD </span><span style="color: #007700;">or ;die(</span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());

</span><span style="color: #0000bb;">mysql_select_db</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"USER_TABLENAME "</span><span style="color: #007700;">) or die(</span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());

</span></pre>
<p>Above we can see that the only thing we need to change is the database, password, and table name. Ideally the table name should be &#8220;users&#8221; as per our example. Your password and database name can be created via MySQL if you have the proper permissions. If you don&#8217;t, contact your web host to get a database configured.</p>
<p>So far your verify.php should look like this:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Project Thus Far &#8211; verify.php</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #0000bb;">&lt;?php

mysql_connect</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"localhost"</span><span style="color: #007700;">, </span><span style="color: #0000bb;">"DATABASE"</span><span style="color: #007700;">, </span><span style="color: #dd0000;">"PASSWORD"</span><span style="color: #007700;">) or die(</span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());

</span><span style="color: #0000bb;">mysql_select_db</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"USER_TABLENAME"</span><span style="color: #007700;">) or die(</span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());</span>

<span style="color: #888888;">if ($_POST['form_submitted'] == '1') {</span><span style="color:gray;">
} else {

}
?&gt;</span></pre>
<p>Test the connection by uploading the file to your server and navigating to the file. If an error doesn&#8217;t present itself, it means you successfully connected to your database! (Even if you see a blank page) Now we can create a random key and answer all of the data into our database.</p>
<h3>Creating A Random Key And Inserting Database Values</h3>
<p>We will be using the mt_rand() function to create our random key. Below you&#8217;ll see that we concatenate the function five times in order to get a lengthy string.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Random Number Generator &#8211; verify.php</p>
<pre style="border: 1px solid black; padding: 10px;">$activationKey =  mt_rand() . mt_rand() . mt_rand() . mt_rand() . mt_rand();</pre>
<p>Don&#8217;t get too excited to try it out yet, first let&#8217;s write the code to insert the value into our database.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Inserting Data Into A Database &#8211; verify.php</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #0000bb;">$sql</span><span style="color: #007700;">=</span><span style="color: #dd0000;">"INSERT INTO users (username, password, email, activationkey, status) VALUES ('$_POST[username]', '$_POST[password]', '$_POST[email]', '$activationKey', 'verify')"</span><span style="color: #007700;">;

if (!</span><span style="color: #0000bb;">mysql_query</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$sql</span><span style="color: #007700;">))

  {

  die(</span><span style="color: #dd0000;">'Error: ' </span><span style="color: #007700;">. </span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());

  }</span></pre>
<p>Above you can see we are updating all of the rows with information from our registration field via the $_POST command. We are also including the $activationKey variable and inputting the word &#8216;verify&#8217; into the status field. This is to keep track of who is verified and who isn&#8217;t. If someone isn&#8217;t verified yet, but has registered, we could easily have them request to resend the email instead of having to register again. Oh, technology!</p>
<p>So far the code should be as below:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Script Thus Far &#8211; verify.php</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #0000bb;">&lt;?php

mysql_connect</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"localhost"</span><span style="color: #007700;">, </span><span style="color: #0000bb;">DATABASE</span><span style="color: #007700;">, </span><span style="color: #dd0000;">"PASSWORD"</span><span style="color: #007700;">) or die(</span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());

</span><span style="color: #0000bb;">mysql_select_db</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"USER_TABLENAME "</span><span style="color: #007700;">) or die(</span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());

</span><span style="color: #007700;">if (</span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'form_submitted'</span><span style="color: #007700;">] == </span><span style="color: #dd0000;">'1'</span><span style="color: #007700;">) {</span><span style="color: #007700;">
</span><span style="color: #ff8000;">##User is registering, insert data until ;we can activate it

</span><span style="color: #0000bb;">$activationKey </span><span style="color: #007700;">=  </span><span style="color: #0000bb;">mt_rand</span><span style="color: #007700;">() . </span><span style="color: #0000bb;">mt_rand</span><span style="color: #007700;">() . </span><span style="color: #0000bb;">mt_rand</span><span style="color: #007700;">() . </span><span style="color: #0000bb;">mt_rand</span><span style="color: #007700;">() . </span><span style="color: #0000bb;">mt_rand</span><span style="color: #007700;">();

</span><span style="color: #0000bb;">$sql</span><span style="color: #007700;">=</span><span style="color: #dd0000;">"INSERT INTO users (username, password, email, activationkey, status)

VALUES

('$_POST[username]', '$_POST[password]', '$_POST[email]','$activationKey', 'verify')"</span><span style="color: #007700;">;

if (!</span><span style="color: #0000bb;">mysql_query</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$sql</span><span style="color: #007700;">))

  {

  die(</span><span style="color: #dd0000;">'Error: ' </span><span style="color: #007700;">. </span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());

  }

} else {

}

</span><span style="color: #0000bb;">?&gt;</span></pre>
<h3>Sending The Activation Key</h3>
<p>Sending an email with PHP is painlessly easy- we just have to supply a few values to an already-made function in PHP: the aptly named mail() command. In our example we are using four parameters to send the email: the recipient address, the subject of the email, the message, and our own return address.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Sending Mail With PHP &#8211; verify.php</p>
<pre style="border: 1px solid black; padding: 10px;">	<span style="color: #007700;">echo </span><span style="color: #dd0000;">"An email has been sent to $_POST[email] with an activation key. Please check your mail to complete registration."</span><span style="color: #007700;">;

</span><span style="color: #ff8000;">##Send activation Email

</span><span style="color: #0000bb;">$to      </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$_POST</span><span style="color: #007700;">[</span><span style="color: #0000bb;">email</span><span style="color: #007700;">];

</span><span style="color: #0000bb;">$subject </span><span style="color: #007700;">= </span><span style="color: #dd0000;">" YOURWEBSITE.com Registration"</span><span style="color: #007700;">;

</span><span style="color: #0000bb;">$message </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"Welcome to our website!\r\rYou, or someone using your email address, has completed registration at YOURWEBSITE.com. You can complete registration by clicking the following link:\rhttp://www.YOURWEBSITE.com/verify.php?$activationKey\r\rIf this is an error, ignore this email and you will be removed from our mailing list.\r\rRegards,\ YOURWEBSITE.com Team"</span><span style="color: #007700;">;

</span><span style="color: #0000bb;">$headers </span><span style="color: #007700;">= </span><span style="color: #dd0000;">'From: noreply@ YOURWEBSITE.com' </span><span style="color: #007700;">. </span><span style="color: #dd0000;">"\r\n" </span><span style="color: #007700;">.

    </span><span style="color: #dd0000;">'Reply-To: noreply@ YOURWEBSITE.com' </span><span style="color: #007700;">. </span><span style="color: #dd0000;">"\r\n" </span><span style="color: #007700;">.

    </span><span style="color: #dd0000;">'X-Mailer: PHP/' </span><span style="color: #007700;">. </span><span style="color: #0000bb;">phpversion</span><span style="color: #007700;">();

</span><span style="color: #0000bb;">mail</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$to</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$subject</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$message</span><span style="color: #007700;">, </span><span style="color: #0000bb;">$headers</span><span style="color: #007700;">);

</span></pre>
<p>This should be fairly self-explanatory. Notice that we are using the \r command to force a return- this is to format the email so all the text isn&#8217;t on one line. If you wanted, you could even include HTML and images into the email. We would recommend you didn&#8217;t, however, as many mail platforms today either don&#8217;t support such features or mark most emails that contain them as spam.</p>
<h3>Coding The Verification Checking Process</h3>
<p>We have arrived at the final part of this lesson: checking the verification code and allowing the user to either be registered or tell them they have entered the wrong code and to try again. In this section we will actually grab the current URL, take the query string, and then check our database to see if it matches a record. If it does, we will call the registrant a member and remove the key from our database. Otherwise, tough luck!</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Script Thus Far &#8211; verify.php</p>
<pre style="border: 1px solid black; padding: 10px;">##User isn't registering, check verify code and change activation code to null, status to activated on success

<span style="color: #0000bb;">$queryString </span><span style="color: #007700;">= </span><span style="color: #0000bb;">$_SERVER</span><span style="color: #007700;">[</span><span style="color: #dd0000;">'QUERY_STRING'</span><span style="color: #007700;">];

</span><span style="color: #0000bb;">$query </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"SELECT * FROM users"</span><span style="color: #007700;">;

</span><span style="color: #0000bb;">$result </span><span style="color: #007700;">= </span><span style="color: #0000bb;">mysql_query</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$query</span><span style="color: #007700;">) or die(</span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());

  while(</span><span style="color: #0000bb;">$row </span><span style="color: #007700;">= </span><span style="color: #0000bb;">mysql_fetch_array</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$result</span><span style="color: #007700;">)){

    if (</span><span style="color: #0000bb;">$queryString </span><span style="color: #007700;">== </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">[</span><span style="color: #dd0000;">"activationkey"</span><span style="color: #007700;">]){

       echo </span><span style="color: #dd0000;">"Congratulations!" </span><span style="color: #007700;">. </span><span style="color: #0000bb;">$row</span><span style="color: #007700;">[</span><span style="color: #dd0000;">"username"</span><span style="color: #007700;">] . </span><span style="color: #dd0000;">" is now the proud new owner of a YOURWEBSITE.com account."</span><span style="color: #007700;">;

       </span><span style="color: #0000bb;">$sql</span><span style="color: #007700;">=</span><span style="color: #dd0000;">"UPDATE users SET activationkey = '', status='activated' WHERE (id = $row[id])"</span><span style="color: #007700;">;

       if (!</span><span style="color: #0000bb;">mysql_query</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$sql</span><span style="color: #007700;">))

  {

        die(</span><span style="color: #dd0000;">'Error: ' </span><span style="color: #007700;">. </span><span style="color: #0000bb;">mysql_error</span><span style="color: #007700;">());

  }

    }

  }

</span></pre>
<p>Above we are doing just as we stated. Pay special attention to the fact we are using the UPDATE command in SQL- not INSERT. Also note that we need the while loop to find the exact ID of the member to update- we don&#8217;t want to update everyone in our database! We do this by comparing the current record ID with one from the database- and voila! If a match is found, we can update it.</p>
<p>Finally, we need to add some security to our script. Read our <a title="sql injection tutorial" href="http://www.learnphponline.com/security/sql-injection-prevention-mysql-php">SQL Injection Tutorial</a> and add the updates below:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px;; text-indent: 15px;">Final Result &#8211; verify.php</p>
<pre style="border: 1px solid black; padding: 10px;color:gray;">mysql_connect("localhost", DATABASE, "PASSWORD") or die(mysql_error());

mysql_select_db("USER_TABLENAME") or die(mysql_error());

<span style="color: #888888;">if ($_POST['form_submitted'] == '1') {</span>
##User is registering, insert data until we can activate it

$activationKey =  mt_rand() . mt_rand() . mt_rand() . mt_rand() . mt_rand();
<span style="color:blue;">$username</span> = <span style="color:blue;">mysql_real_escape_string($_POST[username])</span>;
<span style="color:blue;">$password</span> = <span style="color:blue;">mysql_real_escape_string($_POST[password])</span>;

<span style="color:blue;">$email</span> = <span style="color:blue;">mysql_real_escape_string($_POST[email])</span>;

$sql="INSERT INTO users (username, password, email, activationkey, status) VALUES ('<span style="color:blue;">$username</span>', '<span style="color:blue;">$password</span>', '<span style="color:blue;">$email</span>', '$activationKey', 'verify')";

if (!mysql_query($sql))

  {

  die('Error: ' . mysql_error());

  }

echo "An email has been sent to $_POST[email] with an activation key. Please check your mail to complete registration.";

##Send activation Email

$to      = $_POST[email];

$subject = " YOURWEBSITE.com Registration";

$message = "Welcome to our website!\r\rYou, or someone using your email address, has completed registration at YOURWEBSITE.com. You can complete registration by clicking the following link:\rhttp://www.YOURWEBSITE.com/verify.php?$activationKey\r\rIf this is an error, ignore this email and you will be removed from our mailing list.\r\rRegards,\ YOURWEBSITE.com Team";

$headers = 'From: noreply@ YOURWEBSITE.com' . "\r\n" .

    'Reply-To: noreply@ YOURWEBSITE.com' . "\r\n" .

    'X-Mailer: PHP/' . phpversion();

mail($to, $subject, $message, $headers);

} else {

##User isn't registering, check verify code and change activation code to null, status to activated on success

$queryString = $_SERVER['QUERY_STRING'];

$query = "SELECT * FROM users"; 

$result = mysql_query($query) or die(mysql_error());

  while($row = mysql_fetch_array($result)){

    if ($queryString == $row["activationkey"]){

       echo "Congratulations!" . $row["username"] . " is now the proud new owner of an YOURWEBSITE.com account.";

       $sql="UPDATE users SET activationkey = '', status='activated' WHERE (id = $row[id])";

       if (!mysql_query($sql))

  {

        die('Error: ' . mysql_error());

  }

    }

  }

}</pre>
<h4>Verification Key Conclusion</h4>
<p>So where should you take it from here? Obviously we haven&#8217;t included any error checking for input data. What if the user misspelled his or her password? We should probably put another password field in, and check to see if the passwords match. Additionally, we should mask the content in the password field to ensure security.</p>
<p>We might also add a CAPTCHA to prevent the mail server from getting abused by spam bots- something your host would probably appreciate. We could also simplify matters by using functions and cleaning up code.</p>
<p>There are many ways to improve- but be sure to check out our scripts and tutorials section for more information, because we&#8217;ve covered such topics like this in the past.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learnphponline.com/scripts/email-activation-for-php-forms/feed</wfw:commentRss>
		<slash:comments>49</slash:comments>
		</item>
		<item>
		<title>Making Uppercase Words Automatically In PHP With UCWords</title>
		<link>http://www.learnphponline.com/functions/php-ucwords-making-uppercase-words-automatically</link>
		<comments>http://www.learnphponline.com/functions/php-ucwords-making-uppercase-words-automatically#comments</comments>
		<pubDate>Fri, 03 Apr 2009 23:28:19 +0000</pubDate>
		<dc:creator>Zachary Schuessler</dc:creator>
				<category><![CDATA[PHP Functions]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php capitalization]]></category>
		<category><![CDATA[php strings]]></category>
		<category><![CDATA[php tutorials]]></category>
		<category><![CDATA[php ucwords]]></category>
		<category><![CDATA[php uppercase]]></category>

		<guid isPermaLink="false">http://www.learnphponline.com/?p=49</guid>
		<description><![CDATA[Using UCWords to capitalize strings in PHP.]]></description>
			<content:encoded><![CDATA[<p style="border-top: 1px solid blue; border-bottom: 1px solid blue; margin: 0px auto; padding: 10px; width: 95%; background-color: #e0eaef; text-indent: 0px;"><strong>Foreword</strong>: Humans are lazy by nature; we hold this truth to be self-evident. Any function in PHP that is going to save us a bit of time down the road is well worth the time invested in as a result.</p>
<p>Ever since the release of PHP 4, we&#8217;ve had the ability to utilize a function that automatically converts a string to uppercase characters. This function, UCWords, has many uses that aren&#8217;t so obvious at first glance.</p>
<ol>
<li><strong>Title Case</strong> &#8211; While not everyone runs their own news website or even a movie database, we use title case for things such as website titles and website links. You&#8217;ll note in your browser window that your very own LearnPHPOnline.com utilizes the UCWords function to capitalize the title string.</li>
<li><strong>Sanitizing Database Input</strong> &#8211; We are all familiar with the stereotypical Internet user that just has to leave the Caps Lock key on at all times to get their point across. Protection against LOUD TEXT is quite prevalent in forum systems. (Try putting in &#8220;TEST TITLE&#8221; in vBulletin and watch it magically get transformed into &#8220;Test Title&#8221; if the forum is running correct settings.)</li>
<li><strong>Meta Tags</strong> &#8211; While you don&#8217;t see it directly, search engines still look at meta tags to help formulate what a website is and what it has to offer. By setting the &#8220;Title&#8221; attribute to automatically create uppercase words, we can show the search engines how proper we are without risking carpal tunnel syndrome.</li>
<li><strong>Fixing User Input</strong> &#8211; Let&#8217;s say you run your own business, and you contact registered users via E-mail to gain leads. If the user signed up and didn&#8217;t capitalize their name properly, it&#8217;s going to look unprofessional sending an email to them with their name in lowercase. In this situation we could actually lose business due to a lack of proper programming.</li>
</ol>
<h3>What Does The PHP Function UCWords Do?</h3>
<p>The UCWords function automatically capitalizes every word within a given string. The function is especially easy to use since it only accepts a single argument: the string you want to be forced into uppercase stance. We can see an example of the usage of ucwords below, in our news headline demonstration.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">PHP UCWords Example Of Basic Title Casing</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #0000bb;">&lt;?PHP

 $newsHeadline </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"breaking: man finds way to capitalize news headings automatically"</span><span style="color: #007700;">;

 echo </span><span style="color: #0000bb;">ucwords</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$newsHeadline</span><span style="color: #007700;">);</span>

<span style="color: #ff8000;"> //Returns: Breaking: Man Finds Way To Capitalize News Headings Automatically</span>

<span style="color: #0000bb;">?&gt;</span></pre>
<p>So far things look fairly simple. But what exactly constitutes a new word for the UCWords function? Technically defined, a new word is going to be a string of characters following a whitespace, so long as the string doesn&#8217;t begin with a number. What do you think would happen if we were to start a string with an integer? Would the next letter be capitalized? In the below example, we take the real-life Internet company 43Things.com into consideration.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">PHP UCWords Example Of Strings Starting With An Integer</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #0000bb;">&lt;?PHP

 $webpageTitle </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"43things.com"</span><span style="color: #007700;">;
 echo </span><span style="color: #0000bb;">ucwords</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$webpageTitle</span><span style="color: #007700;">);</span>
<span style="color: #ff8000;"> //Returns 43things.com</span>

<span style="color: #0000bb;">?&gt;</span></pre>
<p>Note that in this example, the T will not be forced into capitalization because the string begins with a number. If we were to separate the number and the letter with a space, carriage return, newline, tab, or form-feed, then the letter would indeed be capitalized.</p>
<p>If the UCWords function encounters a string that is already capitalized, the function will not act on the string in question. If, however, the user were to convert the entire string to lower case letters and then pass the result to the UCWords function, we get a result as seen in the below example.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">PHP UCWords Example Of Basic Rules Of Operation</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #0000bb;">&lt;?PHP

 $screamingText </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"THIS IS VERY LOUD TEXT"

 </span><span style="color: #007700;">echo </span><span style="color: #0000bb;">ucwords</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$screamingText</span><span style="color: #007700;">);

</span><span style="color: #ff8000;">//Returns: THIS IS VERY LOUD TEXT

 </span><span style="color: #007700;">echo </span><span style="color: #0000bb;">ucwords</span><span style="color: #007700;">(</span><span style="color: #0000bb;">strtolower</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$screamingText</span><span style="color: #007700;">));
</span><span style="color: #ff8000;">//Returns: This Is Very Loud Text

</span><span style="color: #0000bb;">?&gt;</span></pre>
<h3>Capitalizing Only Certain Words In A String With UCWords</h3>
<p>If we were concerned with only capitalizing a certain part of a string, we could write up a quick function to ensure that words that are commonly not capitalized are ignored. In many headings you may notice prepositions such as &#8220;at&#8221; or &#8220;to&#8221; aren&#8217;t capitalized. The following function solves the problem by accepting only a single argument.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">PHP UCWords Function For Returning Partially Capitalized String</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #0000bb;">&lt;?PHP

    </span><span style="color: #007700;">function </span><span style="color: #0000bb;">smartcase</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$str</span><span style="color: #007700;">) {
       return </span><span style="color: #0000bb;">preg_replace</span><span style="color: #007700;">(
            </span><span style="color: #dd0000;">"/(?&lt;=(?&lt;!:|'s)\W)(A|An|And|At|For|In|Of|On|Or|The|To|With)(?=\W)/e"</span><span style="color: #007700;">,

            </span><span style="color: #dd0000;">'strtolower("$1")'</span><span style="color: #007700;">,
            </span><span style="color: #0000bb;">$str</span><span style="color: #007700;">);

    }

echo </span><span style="color: #0000bb;">smartcase</span><span style="color: #007700;">(</span><span style="color: #0000bb;">ucwords</span><span style="color: #007700;">(</span><span style="color: #dd0000;">"the prepositions in a title can be excluded with this function!"</span><span style="color: #007700;">));</span>

<span style="color: #ff8000;">//Output: The Prepositions in a Title Can Be Excluded with This Function!</span>
<span style="color: #0000bb;">?&gt;</span></pre>
<p>We could add other words to the ignore feature simply by editing the following line:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">PHP UCWords Example Of Basic Title Casing</p>
<pre style="border: 1px solid black; padding: 10px;">     (A|An|And|At|For|In|Of|On|Or|The|To|With)</pre>
<h3>Sanitize Database Input With A Function Using UCWords</h3>
<p>One of the more practical uses for UCWords is to sanitize input when putting information into a database. You may have noticed that some forum systems such as vBulletin will automatically change your thread title into title case if it is in all-caps. (After all, who really likes to read a screaming title?) An example of how this is done can be seen below, by implementing another function.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">PHP UCWords Example Of Sanitizing Input</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #0000bb;">&lt;?PHP</span>

<span style="color: #007700;">function </span><span style="color: #0000bb;">titleCase</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$string</span><span style="color: #007700;">)
     {
     return </span><span style="color: #0000bb;">ucwords</span><span style="color: #007700;">(</span><span style="color: #0000bb;">strtolower</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$string</span><span style="color: #007700;">));

     }</span>

<span style="color: #0000bb;">$threadName </span><span style="color: #007700;">= </span><span style="color: #dd0000;">"CHECK OUT THIS NEW PHP FUNCTION GUYS!"</span><span style="color: #007700;">;
echo </span><span style="color: #0000bb;">titleCase</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$threadName</span><span style="color: #007700;">);</span>

<span style="color: #ff8000;">//Returns: Check Out This New PHP Function Guys!</span>

<span style="color: #0000bb;">?&gt;</span></pre>
<h4>Closing Comments</h4>
<p>UCWords has quite a bit of use to the PHP development community. Other functions that are worthy of noting would be: StrToUpper() , StrToLower() , UCFirst() , and MB_Convert_Case(). Each of these functions are necessary as the Internet shifts to a more and more dynamic environment.</p>
<p style="text-indent: 0px;"><strong>Bottom line</strong>: We can all thank technology for allowing us to reduce the wear and tear on our Shift keys.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learnphponline.com/functions/php-ucwords-making-uppercase-words-automatically/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>What Is PHP?</title>
		<link>http://www.learnphponline.com/getting-started/what-is-php</link>
		<comments>http://www.learnphponline.com/getting-started/what-is-php#comments</comments>
		<pubDate>Fri, 03 Apr 2009 22:27:10 +0000</pubDate>
		<dc:creator>Zachary Schuessler</dc:creator>
				<category><![CDATA[Getting Started]]></category>
		<category><![CDATA[history of php]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[rasmus lerdorf]]></category>
		<category><![CDATA[what is php]]></category>

		<guid isPermaLink="false">http://learnphponline.com/?p=6</guid>
		<description><![CDATA[A brief history of PHP, its creator, and where the language is going in the future.]]></description>
			<content:encoded><![CDATA[<p style="border-top: 1px solid blue; border-bottom: 1px solid blue; margin: 0px auto; padding: 10px; width: 95%; background-color: #e0eaef; text-indent: 0px;"><strong>Foreword</strong>:We have established that PHP is a web development language that can make developers a hefty sum of money. Before we can start learning the language, we need to know a few things. What exactly is PHP, where didit come from, and what exactly does learning the language do for developers?</p>
<h3>What Is PHP?</h3>
<p>PHP was in development in 1994, when creator <strong>Rasmus Lerdorf</strong> wanted a better way to administer his personal homepage. It all began as a few CGI binaries that Lerdorf used to build several interesting applications. This in itself isn&#8217;t so exciting, especially since early forms of PHP couldn&#8217;t even interface with a database! Regardless, Lerdorf started what would become a very prominent web development language.</p>
<p>From thereon out, PHP became an open source piece of software that anyone could contribute work on. Over the years developers have added quite a bit of functionality to the language overall. Through all the changes that occurred over the years, we can now describe PHP as the following:</p>
<ol>
<li><strong>Server-Side Scripting Language</strong> &#8211; In addition to being a scripting language, PHP is a server-side scripting language. This essentially means that PHP runs on the web server end of things (Which is why you should take advantage of our free hosting if you don&#8217;t have hosting already!). As we&#8217;ll learn later, PHP is processed on the server and then output to the browser after a request is made.</li>
<li><strong>Dynamic</strong> &#8211; PHP is a dynamic language, which is to say that it can change based on what we need it to do. On the other hand we have static languages like HTML, which don&#8217;t allow us to do things like pull and store information from a database. Dynamic languages are quickly becoming standard among web applications, with PHP at the forefront.</li>
<li><strong>Object-Oriented Programming </strong> &#8211; A buzzword in the programming industry is object-oriented design. Under this principle, the PHP is able to create &#8220;objects&#8221; that interact with each other. Object-oriented design isn&#8217;t necessary, but many supporters claim that using it helps organization and functionality in an application. We&#8217;ll review the topic more in future chapters, no need to worry about this topic just yet.</li>
<li><strong>LAMP</strong> &#8211; An architecture called LAMP describes the usage of four popular technologies to create functional web applications. The P can stand for several other competing languages, but PHP generally takes the name in addition to Linux, Apache, and MySQL. For now, we don&#8217;t need to delve into such topics; but it&#8217;s good to note LAMP philosophy has dictated the way many web applications have been created.</li>
<li><strong>Free-Form Language </strong> &#8211; Lastly, PHP is a free-form language. A free-form language will not observe whitespace when being output to the browser. This is apparent in HTML as well, where we can spank the Space bar as much as we want and not see any extra spaces when viewing the HTML in a browser.</li>
</ol>
<h3>What Can PHP Can Do?</h3>
<p>What is PHP? So far, we&#8217;ve established that it is a programming language designed for creating web applications. It serves up dynamic content, works on the web server in the operation, and supports new-fangled principles such as object-oriented design. That&#8217;s all great, but what can PHP do exactly?</p>
<p>Take a look around, and we can see the results of PHP everywhere. It is estimated that PHP is in use on over 20 million different websites, and over a million different web servers (including ours!). We use PHP in applications such as simplifying templates, using user registration systems, storing and receiving information into a database, and even other exciting such as editing images on the fly.</p>
<p>PHP is used in many reporting and business applications, where built-in graphing and image creation can take place. Oddly enough, one of the first PHP applications ever created was an application to track statistics and report results in a convenient manner (Think back- remember Rasmus Lerdorf? He used his Personal Home Page language to create his own statistics counter!).</p>
<p>Thanks to recent years of innovation, technologies such as AJAX have made it easier to offer an easier experience for users of web applications. AJAX is used to silently load and store all sorts of information- whereas it was previously required to refresh the webpage or navigate to multiple other websites. We&#8217;ll learn more on AJAX in future sections, which will be a particularly fun section and something to look forward to in learning.</p>
<h3>What Does PHP Do For Development?</h3>
<p>Developers have a lot to benefit from PHP- and not just a surplus income. Rather, developers are able to take advantage of decreased development times, ease of use in deploying applications, and a fantastical support from a very large community.</p>
<p>To help further the process of developing a web application, multiple frameworks are available for<br />
usage. Frameworks reuse many repetitive tasks, such as connecting to a database or storing basic information. Frameworks should not be used until PHP itself is learned, and a learning curve for most PHP<br />
frameworks can be anywhere from a day to a week or more.</p>
<p>Also worthy of noting is the fact that some frameworks have support for security measures that programmers ignore on a common basis. PHP itself is not an insecure language, but it does leave holes where improper developers might leave mistakes in programming. Frameworks help cover up such holes, but regardless of the fact proper security tactics are good to learn as PHP itself is learned. (Don&#8217;t worry, we teach all the security topics you&#8217;ll need for PHP web applications.)</p>
<h4>Closing Comments</h4>
<p>PHP has been in development for a long time and there are plenty of things to learn about the language overall. Rest assured, we&#8217;ll learn more about everything discussed in this introduction to PHP with the coming chapters. Next up: what you&#8217;ll learn from this course and what you can do with the knowledge obtained therein.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learnphponline.com/getting-started/what-is-php/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>SQL Injection: How To Prevent Security Flaws In PHP / MySQL</title>
		<link>http://www.learnphponline.com/security/sql-injection-prevention-mysql-php</link>
		<comments>http://www.learnphponline.com/security/sql-injection-prevention-mysql-php#comments</comments>
		<pubDate>Fri, 03 Apr 2009 23:15:53 +0000</pubDate>
		<dc:creator>Zachary Schuessler</dc:creator>
				<category><![CDATA[PHP Security]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[php mysql]]></category>
		<category><![CDATA[php security]]></category>
		<category><![CDATA[php tutorials]]></category>

		<guid isPermaLink="false">http://www.learnphponline.com/?p=37</guid>
		<description><![CDATA[How to get security flaws in PHP covered up with simple tactics.]]></description>
			<content:encoded><![CDATA[<p style="border-top: 1px solid blue; border-bottom: 1px solid blue; margin: 0px auto; padding: 10px; width: 95%; background-color: #e0eaef; text-indent: 0px;"><strong>Foreword</strong>: SQL injection is a very scary phrase. After all, it has single-handedly been responsible for putting down major government websites and thousands of personal home pages- and everything in between. (Something that has been increasingly popular after the &#8220;Techie&#8221; generation had puberty-riddled children.) Yet believe it or not, guarding against the attack is simple as a couple of lines of code.</p>
<h3>SQL Injection: What It Is</h3>
<p>There was once a famous doctor that had it completely right: never trust your patients. Now this doctor may have only been a sitcom doctor on the show &#8220;House,&#8221; but we&#8217;ll be taking a page from his book. Of course, in our case the patients will actually be Internet users. Don&#8217;t let your guard down! They are conniving, dastardly, plot-making fiends- and you&#8217;ll do well to remember it.</p>
<p>First, Let&#8217;s define an SQL Injection:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Define SQL Injection</p>
<pre style="border: 1px solid black; padding: 10px;">  <strong>SQL Injection</strong> - \S-Q-L-in-'jek-shen\ - Noun
 The technique of inputting malicious data into an SQL statement, which would therefore make the vulnerability present on the database layer. Surprisingly, it seems everyone who has recently taken up learning a web development language has to try the technique out on their favorite websites. Luckily for said websites, this technique isn't at all hard to protect against.</pre>
<p>The technique of inputting malicious data into an SQL statement, which would therefore make the vulnerability present on the database layer. Surprisingly, it seems everyone who has recently taken up learning a web development language has to try the technique out on their favorite websites. Luckily for said websites, this technique isn&#8217;t at all hard to protect against.</p>
<h3>SQL Injection: What It Looks Like</h3>
<p>The vast majority of all SQL injections will take place on an input form. Contrary to popular belief, this isn&#8217;t the only place where we will see them- it&#8217;s also common to manipulate URLs to inject SQL code. (But we&#8217;ll get more into that later.)</p>
<p>The most basic of all SQL injections will look like the following:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">The Basic SQL Injection</p>
<pre style="color: blue;border:1px solid black;">	Variable' or 1=1--</pre>
<p>Let&#8217;s say we have a login form. By inputting the above code, we can use our SQL injection to gain login even without proper credentials! So how&#8217;s it work? Take a look at the &#8220;bigger picture&#8221; below:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">What It Looks Like On The Back-End</p>
<pre style="border: 1px solid black; padding: 10px;">	SELECT * FROM users WHERE username = '<span style="color: blue;">Variable' or 1=1--</span>'</pre>
<p>See how our code is nicely injected into the query? The result of this query will grant us access regardless of the username, since the result of &#8220;1=1&#8243; will always be true. In this case, we bypass the whole selection process.</p>
<p>You may have been wondering what the double dashes are for ( &#8212; ). These dashes at the end tell the SQL server to ignore the rest of the query. If the exploit isn&#8217;t being used on an SQL server, then omitting the double dashes and ending single quote will get the desired results.</p>
<p>Note that while this is the most standard way, it certainly isn&#8217;t the only way that malicious users will gain entry. SQL queries will differ greatly from one syntax to another, and thus, so too should the SQL injection. It&#8217;s also common to see the following:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">More SQL Injection Syntax Fun</p>
<pre style="border: 1px solid black; padding: 10px;">    ') or ('1'='1
    "or "1"="1
    ' or '1'='1
    Or 1=1--
    " or 1=1--
    ' or 1=1--</pre>
<h3>SQL Injection: Attacking Via URLs</h3>
<p>Did you know it was possible to attack an SQL server through a URL? Well, it&#8217;s possible, and usually much more dangerous to webmasters. When using PHP and SQL, there is commonly a URL such as the following:</p>
<ul>
<li>http://YourWebsite.com/login.php?id=2</li>
</ul>
<p>By adding a little SQL to the end of the URL, we can do some very mischievous mischief:</p>
<ul>
<li>http://YourWebsite.com/login.php?id=2<span style="color: blue;">&#8216;; DROP TABLE login; #</span></li>
</ul>
<p>You might be confused by the hash. This little guy is just like the double dash we used earlier; it will tell the SQL query to halt after our input. And if you haven&#8217;t noticed, we just told the server to drop the entire table of users! This is an example of how powerful and dangerous SQL injections can be- and also shows that constant backups are a necessity.</p>
<p>Enough already! Let&#8217;s finally find out how to make sure that little script kiddies aren&#8217;t going to ruin the hard work webmasters and web developers set aside for their projects.</p>
<h3>SQL Injection Prevention: Editing Lengths Of Form Components</h3>
<p>The first step in the process is simple: simply restrict input fields to the absolute minimum- usually anywhere from 7-12 characters is fine. Doing so will make long queries unable to be input, since the field is only enough characters for smaller queries. This will actually not prevent an SQL injection, but will make work harder for those trying to make use of one.</p>
<p><img style="margin: 0px auto; display: block;" title="PHP SQL Injection Can Be Prevented!" src="../php-sql-injection.jpg" alt="PHP MySQL" /></p>
<p>Savvy SQL injection users can simply make a new form and remove the limits on the character length, since the length is in plain HTML and viewable (and editable) by anyone.</p>
<h3>SQL Injection Prevention: Data Type Validation</h3>
<p>Another good idea is to validate any data once it is received. If a user had to input an age, make sure the input is an actual number. If it was a date, make sure the date is in proper format. Again, this will not prevent an SQL injection in itself- it just makes work harder for those trying to exploit an SQL server.</p>
<p><img style="margin: 0px auto; display: block;" title="Validate Form Input Where Possible" src="../validate-form-input.jpg" alt="sql injection prevent" /></p>
<p>Data type validation can be thwarted by modifying the query over a trial-and-error test period. This is still only slowing attackers down- but isn&#8217;t it much more satisfying to have them waste their time before finding out one&#8217;s own query is impervious to harm? Of course! An eye for an eye!</p>
<h3>SQL Injection Prevention: User Privileges</h3>
<p>It&#8217;s nice to be able to create a &#8220;super user&#8221; in one&#8217;s own database that can create, drop, and edit tables at will. The security-obsessive webmaster will want to make individual users that can only do one or two tasks at a time. In effect, this means that SQL injections will only be able to do one or two things at a time.</p>
<p>This is just a little prevention fun, it can certainly still cause a certain amount of danger. If a user is made for deleting tables, than an SQL injection can do the same thing- it just won&#8217;t be able to do much else. Regardless, deleting a table is a very big privilege to handle. This method is still useful for throwing attackers off track, as well as minimizing risk from areas of a website that aren&#8217;t critical to the security of the database.</p>
<h3>SQL Injection Prevention: Magic Quotes (Which Aren&#8217;t So Magical)</h3>
<p>We all love magic. Heck, magic is downright cool. It serves as the basis for great children books (Harry Potter, anyone?) and even has been used for themes in nerdy card games everyone seems to enjoy (Ah, we&#8217;re looking at you, Magic The Gathering!)</p>
<p>One thing that just doesn&#8217;t live up to the magic name is magic quotes. PHP developers thought it would be a wonderful idea to make a process that escapes all incoming data in a PHP script. Sounds like it would fix our problem with SQL injections, but alas, there are better ways.</p>
<p>Anyone who has recommended a fix with magic quotes doesn&#8217;t know what they are talking about. After all, magic quotes are considered deprecated and removed as of PHP version 6. So why such hostility over a process that is seemingly beneficial to our predicament?</p>
<p>The short answer: magic quotes are horrible for portability issues, performance issues, and they mess with other data that doesn&#8217;t need to be escaped.</p>
<ol>
<li>Many scripts made with magic quotes won&#8217;t work on servers that have (intelligently) turned the feature off.</li>
<li>Performance loss is observed because not all of the data is being input into a database- we&#8217;re wasting process time.</li>
<li>Lastly, magic quotes are just inconvenient. They add an extra slash (&#8220;\&#8221;) to all of our form data, even when it might not be needed. To fix this, we have to use another process to fix it (If you are unfortunate enough to have used magic quotes, look up the stripslashes() function, and consider switching if possible)</li>
</ol>
<p>We came close to finding a real solution there- almost! But we did learn something: don&#8217;t use magic quotes, and instead find an alternative that can escape the input data based on what we need: not what we don&#8217;t.</p>
<h3>SQL Injection Prevention: The Solution In Preventing SQL Attacks</h3>
<p>We could&#8217;ve given you the answer right away, but what fun would that have been? Too often, PHP developers are becoming lazy and not following proper security tactics the way they should. By reaching this point in the lecture, you&#8217;ve increased your knowledge on how SQL injections are used, how not to prevent the attacks, and finally: you&#8217;ll learn the right way to keep injection attacks at bay.</p>
<p>We&#8217;ll accomplish this last feat with a simple function that the developers of PHP made especially for SQL injections. We call this function <strong>mysql_real_escape_string()</strong> &#8211; take a look at it below:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">mysql_real_escape_string() In Action!</p>
<pre style="border: 1px solid black; padding: 10px;">    <span style="color: red;">$name</span> = "John";
    <span style="color: red;">$name</span> = <span style="color: blue;">mysql_real_escape_string</span>(<span style="color: red;">$name</span>);
    $SQL = "SELECT * FROM users WHERE username = '<span style="color: red;">$name</span>'";</pre>
<p>Although for a more practical use, we would have the <span style="color: red;">$name</span> variable pointed to a POST result, as seen below:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">$_POST Can Dig In On The Action Too!</p>
<pre style="border: 1px solid black; padding: 10px;">    <span style="color: red;">$name</span> = <span style="color: blue;">mysql_real_escape_string</span>($_POST['user']);</pre>
<p>And we can even make things easier by putting it into one line:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">All In One Line Now; Here We Go!</p>
<pre style="font-size: 11px;">$SQL = "SELECT * FROM users where username = "<span style="color: blue;">mysql_real_escape_string</span>($POST['user']);</pre>
<p>So what&#8217;s the output like if malicious users try to get access to our SQL server? Well glad you asked! Their attempts may look something like this:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Cause And Effect With mysql_real_escape_string()</p>
<pre style="border: 1px solid black; padding: 10px;">    $malcious_input = "' OR 1'";
    <span style="color: green;">// The Above Is The Malicious Input. Don't Be Scared!
    // With The mysql_real_escape_string() usage, the following is obtained:</span>

	\' OR 1\'
    <span style="color: green;">// Notice how the slashes escape the quotes! Now users can't enter malicious data</span></pre>
<p>And the best part is, they just wasted their time and effort for nothing. Now how&#8217;s that for vindication!</p>
<h3>SQL Injection: Closing Comments</h3>
<p>We&#8217;ve learned quite a bit today. SQL injections are bad. All Internet users are equally as bad. Protecting against both ensures a happy and stable web application. And above all else, never use magic quotes! Despite their cleverly disguised name, we&#8217;ve found no evidence of magic.</p>
<p>Lastly, note that there are libraries and classes that can help aid in the fight against SQL injection. Prepared statements are plausible as well, but as for us, we enjoy sticking to the mysql_real_escape_string() function for less headaches.</p>
<p><strong>Bottom Line:</strong> mysql_real_escape_string() &#8211; It doesn&#8217;t have a magically awesome name, but it&#8217;s 24 characters worth of SQL injection-protection goodness.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learnphponline.com/security/sql-injection-prevention-mysql-php/feed</wfw:commentRss>
		<slash:comments>17</slash:comments>
		</item>
		<item>
		<title>Find Odd Or Even Numbers</title>
		<link>http://www.learnphponline.com/php-basics/find-odd-or-even-numbers</link>
		<comments>http://www.learnphponline.com/php-basics/find-odd-or-even-numbers#comments</comments>
		<pubDate>Fri, 03 Apr 2009 23:08:44 +0000</pubDate>
		<dc:creator>Zachary Schuessler</dc:creator>
				<category><![CDATA[PHP Basics]]></category>
		<category><![CDATA[php even numbers]]></category>
		<category><![CDATA[php math]]></category>
		<category><![CDATA[php odd numbers]]></category>
		<category><![CDATA[php tutorial]]></category>

		<guid isPermaLink="false">http://www.learnphponline.com/?p=33</guid>
		<description><![CDATA[Finding odd and even numbers in PHP.]]></description>
			<content:encoded><![CDATA[<p style="border-top: 1px solid blue; border-bottom: 1px solid blue; margin: 0px auto; padding: 10px; width: 95%; background-color: #e0eaef; text-indent: 0px;"><strong>Foreword</strong>: There comes the time in every web developer&#8217;s escapades that he or she needs to find an odd or even number. Perhaps for use in alternating color schemes in blog comments, or even for more serious applications such as error checking. Whatever the case may be, we can write up a simple function or IF statement to do the hard work for us.</p>
<h3>Find An Odd Or Even Number With The IF Statement</h3>
<p>We can check for whether or not a number is even or odd by using the modulus operator seen below.</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php

</span><span style="color: #007700;">if (</span><span style="color: #0000bb;">7 </span><span style="color: #007700;">% </span><span style="color: #0000bb;">2</span><span style="color: #007700;">) {
  echo </span><span style="color: #dd0000;">"7 is odd"</span><span style="color: #007700;">;
} else {
  echo </span><span style="color: #dd0000;">"7 is even"</span><span style="color: #007700;">;
}  

</span><span style="color: #0000bb;">?&gt;</span></span></pre>
<p>We interpret the first line as &#8220;Divide 7 by two, and check for a remainder.&#8221; If there is a remainder (in our case we have 3.5, so a remainder of 0.5), then the number is odd. If there isn&#8217;t a remainder while dividing by two, then that number is even.</p>
<p>You could change the three sevens in the above script to eights, and you will notice the script outputs the number is even, since 8 divided by two is 4 with no remainder.</p>
<h3>Checking For Odd And Even Numbers With A PHP Function</h3>
<p>We can clean up so code here by using a function to do the dirty work for us. First we&#8217;ll stick the if statement into a function and make sure it works, then we&#8217;ll optimize our code once we get the concept up and running.</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php

</span><span style="color: #007700;">function </span><span style="color: #0000bb;">is_odd</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$num</span><span style="color: #007700;">) {
  if (</span><span style="color: #0000bb;">$num </span><span style="color: #007700;">% </span><span style="color: #0000bb;">2 </span><span style="color: #007700;">== </span><span style="color: #0000bb;">0</span><span style="color: #007700;">) {
  return </span><span style="color: #0000bb;">false</span><span style="color: #007700;">;
 } else {
    return </span><span style="color: #0000bb;">true</span><span style="color: #007700;">;
  }
}
</span><span style="color: #ff8000;">############################

</span><span style="color: #007700;">if (</span><span style="color: #0000bb;">is_odd</span><span style="color: #007700;">(</span><span style="color: #0000bb;">4</span><span style="color: #007700;">)) {
 echo </span><span style="color: #dd0000;">"This number is odd."</span><span style="color: #007700;">;
} else {
  echo </span><span style="color: #dd0000;">"This number is even."</span><span style="color: #007700;">;
}

</span><span style="color: #0000bb;">?&gt;</span></span></pre>
<p>The function will do exactly what we just did with our IF statement previously, only this time we can reuse our code and save quite a bit of space. We supplied the <strong>is_odd</strong> function with a plain number; if we wanted we could put in a variable. In fact, we&#8217;ll do just that while we optimize our code in the next example.</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php

</span><span style="color: #007700;">function </span><span style="color: #0000bb;">is_odd</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$num</span><span style="color: #007700;">){
 return </span><span style="color: #0000bb;">$num </span><span style="color: #007700;">% </span><span style="color: #0000bb;">2 </span><span style="color: #007700;">== </span><span style="color: #0000bb;">0 </span><span style="color: #007700;">? </span><span style="color: #0000bb;">false</span><span style="color: #007700;">:</span><span style="color: #0000bb;">true</span><span style="color: #007700;">;
}

</span><span style="color: #0000bb;">$number_To_Check </span><span style="color: #007700;">= </span><span style="color: #0000bb;">3</span><span style="color: #007700;">;
if (</span><span style="color: #0000bb;">is_odd</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$number_To_Check</span><span style="color: #007700;">)) {
  echo </span><span style="color: #dd0000;">"$number_To_Check is odd."</span><span style="color: #007700;">;
} else {
  echo </span><span style="color: #dd0000;">"$number_To_Check is even."</span><span style="color: #007700;">;
}
</span><span style="color: #0000bb;">?&gt;</span></span></pre>
<p>This time around we are saving even more space by using the ternary symbol (?). In this instance the left side of the colon is what gets executed if the statement is true, and the right side is what gets executed if the statement is false. Think of the colon as an &#8220;else&#8221; statement.We can use some pseudo-code to help understand this more:</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php

</span><span style="color: #007700;">function </span><span style="color: #0000bb;">odd_check</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$num</span><span style="color: #007700;">){
 (</span><span style="color: #0000bb;">Is this statement true</span><span style="color: #007700;">) ? </span><span style="color: #0000bb;">yes  </span><span style="color: #007700;">:  </span><span style="color: #0000bb;">no</span><span style="color: #007700;">;
}
</span><span style="color: #0000bb;">?&gt;</span></span></pre>
<p>Back to our case example: since the statement checks for an even number, when the statement is true we return the value &#8220;false&#8221; to indicate it isn&#8217;t an odd number.</p>
<h3>Further Optimization of The Odd Number-Finding Function</h3>
<p>Want to really impress the ladies? Try showing them the following function to find odd and even numbers.</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php

</span><span style="color: #007700;">function </span><span style="color: #0000bb;">is_odd</span><span style="color: #007700;">( </span><span style="color: #0000bb;">$num</span><span style="color: #007700;">)
{
  return( </span><span style="color: #0000bb;">$num </span><span style="color: #007700;">&amp; </span><span style="color: #0000bb;">1 </span><span style="color: #007700;">);
}
</span><span style="color: #ff8000;">########################

</span><span style="color: #007700;">if (</span><span style="color: #0000bb;">is_odd</span><span style="color: #007700;">(</span><span style="color: #0000bb;">7</span><span style="color: #007700;">)) {
    echo </span><span style="color: #dd0000;">"7 is odd"</span><span style="color: #007700;">;
} else {
    echo </span><span style="color: #dd0000;">"7 is even"</span><span style="color: #007700;">;
}
</span><span style="color: #0000bb;">?&gt;</span></span></pre>
<p>This time around we took the equation out. We can do this through using the AND operator (&amp;). If you aren&#8217;t up to speed on binary, consult the chart below to see how we count to five. (Note that we will not go into depth on binary, just give you enough information to understand the AND process.)</p>
<p><img style="margin: 0px auto; display: block; width: 383px; height: 267px;" title="Count To 5 In Binary" src="../binary-counting.jpg" alt="" /></p>
<p>Each place in the above numbers represents a different value. The furthest right number, as you might have guessed represents &#8220;1,&#8221; the next &#8220;2,&#8221; the next &#8220;4,&#8221; and so on. But notice something even more interesting: all the odd numbers have a &#8220;1&#8243; while the even numbers don&#8217;t!</p>
<p>The AND operator is going to compare two binary numbers. If both numbers have a &#8220;true&#8221; (the number 1) value, then the result is true. If either numbers have a 0 (or &#8220;false&#8221;), or both numbers are false, the resulting number is 0. Consult the diagram below for more information.</p>
<p><img style="margin: 0px auto; display: block; width: 383px; height: 267px;" title="Binary AND" src="../binary-and.jpg" alt="" /></p>
<p>Back to our equation. If the ($int &amp; 1) check returns true, the number is odd. Let&#8217;s say we put an odd number such as 3 in. We will compare it against 1, which is already an odd number. 0011 and 0001 can be used in an AND equation to get the result of 1, since both values are 1 (and thus true).</p>
<p>You don&#8217;t necessarily have to understand this jargon to make use of the function- but it&#8217;s good to get the basics of binary down if you haven&#8217;t already.</p>
<h4>Closing Comments</h4>
<p>Now that you can find odd and even numbers, you can accomplish all types of artsy things. You&#8217;ll see this in effect most often on blogs that alternate the background colors of comments to make for easy reading. Lyrics websites do the same thing, and you can even use the same principle to use in more complex equations (which we won&#8217;t get into in the scope of this article).</p>
<p>Try running the following code just for fun:</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php

</span><span style="color: #007700;">function </span><span style="color: #0000bb;">is_odd</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$num</span><span style="color: #007700;">)
{
  return( </span><span style="color: #0000bb;">$num</span><span style="color: #007700;">&amp; </span><span style="color: #0000bb;">1 </span><span style="color: #007700;">);
}

for (</span><span style="color: #0000bb;">$counter </span><span style="color: #007700;">= </span><span style="color: #0000bb;">0</span><span style="color: #007700;">; </span><span style="color: #0000bb;">$counter </span><span style="color: #007700;">&lt;= </span><span style="color: #0000bb;">20</span><span style="color: #007700;">; </span><span style="color: #0000bb;">$counter</span><span style="color: #007700;">++) {
  if (</span><span style="color: #0000bb;">is_odd</span><span style="color: #007700;">(</span><span style="color: #0000bb;">$num</span><span style="color: #007700;">)) {
    echo </span><span style="color: #dd0000;">"&lt;div style='background-color:black;width:800px;height:30px;margin:0px auto;'&gt; &lt;/div&gt;"</span><span style="color: #007700;">;
  } else {

    echo </span><span style="color: #dd0000;">"&lt;div style='background-color:red;width:800px;height:30px;margin:0px auto;'&gt; &lt;/div&gt;"</span><span style="color: #007700;">;
  }
}
</span><span style="color: #0000bb;">?&gt;</span></span></pre>
]]></content:encoded>
			<wfw:commentRss>http://www.learnphponline.com/php-basics/find-odd-or-even-numbers/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>PHP Comments Tutorial</title>
		<link>http://www.learnphponline.com/php-basics/php-comments-tutorial</link>
		<comments>http://www.learnphponline.com/php-basics/php-comments-tutorial#comments</comments>
		<pubDate>Fri, 03 Apr 2009 23:04:42 +0000</pubDate>
		<dc:creator>Zachary Schuessler</dc:creator>
				<category><![CDATA[PHP Basics]]></category>
		<category><![CDATA[commenting in php]]></category>
		<category><![CDATA[php comments]]></category>
		<category><![CDATA[php tutorial]]></category>

		<guid isPermaLink="false">http://www.learnphponline.com/?p=28</guid>
		<description><![CDATA[How to make use of comments within PHP.]]></description>
			<content:encoded><![CDATA[<p style="border-top: 1px solid blue; border-bottom: 1px solid blue; margin: 0px auto; padding: 10px; width: 95%; background-color: #e0eaef; text-indent: 0px;"><strong>Foreword</strong>: Arrogance runs thick among the LearnPHPOnline.com programmers- we hate doing more work than we have to! Commenting is something that is normally ignored by new or lazy programmers, but both groups soon find commenting saves much more time than it takes.</p>
<p>Comments may only viewed by those who make changes to the code itself- the PHP engine doesn&#8217;t parse anything designated as a comment. A good example of this is seen below, where the first line isn&#8217;t being output to the browser, while the second is.</p>
<p><!-- code block 1 --></p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">An Example Of The PHP Comment Syntax</p>
<pre><span style="color: #000000;"><span style="color: #0000bb;">&lt;?php

</span><span style="color: #ff8000;">// This is an example of a comment- this is not being parsed!

</span><span style="color: #007700;">echo </span><span style="color: #dd0000;">"This is being parsed by the PHP engine!"</span><span style="color: #007700;">;

</span><span style="color: #0000bb;">?&gt;</span>
</span></pre>
<p><!-- code block 1 -->We, as able-bodied programmers and individuals, should invest our time into commenting for three primary reasons.</p>
<ol>
<li><strong>Employment</strong> &#8211; If you are working for an employer, or are planning to release your source code to the public, you will need to make use of comments. Employers demand it because if they were to lose you as an employee, they would have to hire someone new to peruse your code. This act with commenting can take hours, and without can take days. This same principle works with open source code, which needs to be readable by others so they know how to operate or modify your program correctly.</li>
<li><strong>Saving time</strong> &#8211; Even if you plan on keeping the code to yourself, you should make use of PHP comments because as your projects grow in scale, you will forget where you place certain bits of code or even what large blocks of code do. Most arrogant programmers do without comments at this stage, claiming they can remember the programs they make. In reality, a programmer will often come back to a certain code block months into the future, and quickly find they will need to take a few hours to reread their code and comment it properly.</li>
<li><strong>Troubleshooting</strong> &#8211; More intelligently, programmers use PHP comments to troubleshoot their applications. When trying to find which code block is causing an error, programmers can quickly section out new additions to the application with a comment block to see if the error subsides. If it does, that&#8217;s where the error lies. If not, the error lies elsewhere.</li>
</ol>
<h3>PHP Commenting Syntax</h3>
<p>You&#8217;ve finally come around, and you want to start commenting your code. Bravo! The first step is to learn how to use it. There are three different ways we can make comments; two of which can be seen below in the example.</p>
<p><!-- code block 2 --></p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Two different Methods of Using PHP Comments</p>
<pre><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

</span><span style="color: #ff8000;">#This is an example comment

//This is another example comment

</span><span style="color: #007700;">echo </span><span style="color: #dd0000;">"Sentence 1."</span><span style="color: #007700;">; </span><span style="color: #ff8000;">//This is our first statement

</span><span style="color: #007700;">echo </span><span style="color: #dd0000;">"&lt;br /&gt;"</span><span style="color: #007700;">; </span><span style="color: #ff8000;">#This is a line break

</span><span style="color: #007700;">echo </span><span style="color: #dd0000;">"Sentence 2."</span><span style="color: #007700;">; </span><span style="color: #ff8000;">// This is our second statement

</span><span style="color: #0000bb;">?&gt;</span>
</span></pre>
<p><!-- code block 2 -->You&#8217;ll notice that both commenting styles, either the &#8220;#&#8221; or the &#8220;//&#8221; versions, are used on a single line. Also note we can use both behind an otherwise functional PHP statement, such as can be seen with our second half of the example. But if we wanted to format comments to span multiple lines, things would get tedious. Luckily, PHP developers can make use of a third way that spans multiple lines as seen below.</p>
<p><!-- code block 3 --></p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Multi-Line Comments in PHP</p>
<pre><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

</span><span style="color: #ff8000;">#This is a

#single line comment

#on multiple lines.

/* This is an easier

way to write comments

on multiple lines */

</span><span style="color: #0000bb;">?&gt;</span>
</span></pre>
<p><!-- code block 3 -->In the above PHP commenting example we see that you can span multiple lines both ways, but the latter example is much easier to work with. These types of multiple line comments are usually placed at the top of a PHP file to explain what it&#8217;s for, what it accomplishes, and any specific information needed to run or edit the file correctly.</p>
<p>Some people like to showoff and get elaborate with their commenting styles. We&#8217;ll agree it&#8217;s great fun, since it actually helps out the readability of comments in many cases. Below is an example of what the LearnPHPOnline.com&#8217;s creative programming department came up with in all but a few seconds of their precious time.</p>
<p><!-- code block 4 --></p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Fancy-Shmancy PHP Comments</p>
<pre><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

</span><span style="color: #ff8000;">#################################

####  My PHP File Version 2.2####

#################################

/* Be sure to edit the server 

password and username details 

to properly configure this 

script to work! */

</span><span style="color: #0000bb;">?&gt;</span>
</span></pre>
<p><!-- code block 4 -->Quite chauvinistic aren&#8217;t they?</p>
<h3>Using PHP Comments To Troubleshoot</h3>
<p>Troubleshooting is a vital part of programming, as we are sure the reader knows already. It&#8217;s always a great idea to take breaks every time a new code block is added to see if everything is still functional. If it isn&#8217;t, we have to get creative in how we find the root of the problem.</p>
<p>Below is an actual flawed script that we created. You will likely see the problem right off, but in a real-world environment, tracking such a problem down will be much more difficult.</p>
<p><!-- code block 5 --></p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Troubleshooting PHP Code With Comments</p>
<pre><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

</span><span style="color: #ff8000;">//This script will output a variable to the browser

</span><span style="color: #0000bb;">$myVar1 </span><span style="color: #007700;">= </span><span style="color: #dd0000;">" meow mix"</span><span style="color: #007700;">;

</span><span style="color: #0000bb;">$myVar2 </span><span style="color: #007700;">= </span><span style="color: #dd0000;">" meow mix';

echo "</span><span style="color: #0000bb;">I want chicken</span><span style="color: #007700;">, </span><span style="color: #0000bb;">I want liver</span><span style="color: #007700;">,</span><span style="color: #dd0000;">" . $myVar1 . $myVar2 . " </span><span style="color: #0000bb;">please deliver</span><span style="color: #007700;">!</span><span style="color: #dd0000;">";

?&gt;</span>
</span></pre>
<p><!-- code block 5 -->When run, this script will give the error &#8220;Parse error: syntax error, unexpected T_STRING&#8221; &#8211; and even though the problem lies on line 4, it points us to line 6. So we&#8217;ll just comment out line 6, and we see that the problem still persists!</p>
<p><!-- code block 6 --></p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Troubleshooting PHP Code With Comments</p>
<pre><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

</span><span style="color: #ff8000;">//This script will output a variable to the browser

</span><span style="color: #0000bb;">$myVar1 </span><span style="color: #007700;">= </span><span style="color: #dd0000;">" meow mix"</span><span style="color: #007700;">;

</span><span style="color: #0000bb;">$myVar2 </span><span style="color: #007700;">= </span><span style="color: #dd0000;">" meow mix';

#echo "</span><span style="color: #0000bb;">I want chicken</span><span style="color: #007700;">, </span><span style="color: #0000bb;">I want liver</span><span style="color: #007700;">,</span><span style="color: #dd0000;">" . $myVar1 . $myVar2 . " </span><span style="color: #0000bb;">please deliver</span><span style="color: #007700;">!</span><span style="color: #dd0000;">";

?&gt;</span>
</span></pre>
<p><!-- code block 6 -->Oh dear, it looks like the problem is still there once we run the application.</p>
<p>Now we&#8217;ll move the comment to line 4, and see what happens. Once we run the application, we see that the browser outputs this string &#8220;I want chicken, I want liver, meow mix please deliver!&#8221; We are missing the second meow mix, obviously, but the script still executed successfully. This means that the problem must lie on or near line 4. Upon further checking, we see that we used an apostrophe to close the variable assignment! Oops! Let&#8217;s fix it and see if it runs now.</p>
<p><!-- code block 7 --></p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Troubleshooting PHP Code With Comments</p>
<pre><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

</span><span style="color: #ff8000;">//This script will output a variable to the browser

</span><span style="color: #0000bb;">$myVar1 </span><span style="color: #007700;">= </span><span style="color: #dd0000;">" meow mix"</span><span style="color: #007700;">;

</span><span style="color: #0000bb;">$myVar2 </span><span style="color: #007700;">= </span><span style="color: #dd0000;">" meow mix"</span><span style="color: #007700;">;

echo </span><span style="color: #dd0000;">"I want chicken, I want liver," </span><span style="color: #007700;">. </span><span style="color: #0000bb;">$myVar1 </span><span style="color: #007700;">. </span><span style="color: #0000bb;">$myVar2 </span><span style="color: #007700;">. </span><span style="color: #dd0000;">" please deliver!"</span><span style="color: #007700;">;

</span><span style="color: #0000bb;">?&gt;</span></span></pre>
<p><!-- code block 7 --><span style="color: #000000;">Success! We now have the correct string output to the browser.</span></p>
<h3>One Last Tip For PHP Comments</h3>
<p>One last thing we need to stress is to use PHP comments in selection structures, such as CASE or IF. If you have an IF statement, always be sure to comment what each branch does, even if you don&#8217;t use one of the choices. An example is below.</p>
<p><!-- code block 8 --></p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">PHP Comments And Selection Structures</p>
<pre><span style="color: #000000;">
<span style="color: #0000bb;">&lt;?php

</span><span style="color: #ff8000;">//This script showcases comments in an IF structure

</span><span style="color: #0000bb;">$clientAge </span><span style="color: #007700;">= </span><span style="color: #0000bb;">18</span><span style="color: #007700;">;

if (</span><span style="color: #0000bb;">$clientAge </span><span style="color: #007700;">&lt; </span><span style="color: #0000bb;">18</span><span style="color: #007700;">) {

echo </span><span style="color: #dd0000;">"You are not able to view this material until you are 18 or older."</span><span style="color: #007700;">;

} else {

</span><span style="color: #ff8000;">//Don't show a notice

</span><span style="color: #007700;">}

</span><span style="color: #0000bb;">?&gt;</span></span></pre>
<p><!-- code block 8 --><span style="color: #000000;">Even though we don&#8217;t need a comment in the else branch, we should put one there because it does amazing things for readability. Once programmers start nesting IF statements inside each other, documenting each IF branch is absolutely vital in understanding how an application functions.</span></p>
<h4>Closing Comments</h4>
<p>Commenting is a vital part of programming, and it serves more purposes than what one would think as we can very well see. Always try to keep documentation on your code where possible, if not for yourself, then for your employers who don&#8217;t like seeing undocumented work from their employees.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learnphponline.com/php-basics/php-comments-tutorial/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>PHP Include Tutorial</title>
		<link>http://www.learnphponline.com/php-basics/php-include-tutorial</link>
		<comments>http://www.learnphponline.com/php-basics/php-include-tutorial#comments</comments>
		<pubDate>Fri, 03 Apr 2009 23:03:23 +0000</pubDate>
		<dc:creator>Zachary Schuessler</dc:creator>
				<category><![CDATA[PHP Basics]]></category>

		<guid isPermaLink="false">http://www.learnphponline.com/?p=26</guid>
		<description><![CDATA[How to include remote files within PHP 4 and PHP 5.]]></description>
			<content:encoded><![CDATA[<p style="border-top: 1px solid blue; border-bottom: 1px solid blue; margin: 0px auto; padding: 10px; width: 95%; background-color: #e0eaef; text-indent: 0px;"><strong>Foreword</strong>: The include statement built into PHP is going to save web developers a heap of time in making their web application. What we&#8217;re going to see in this tutorial is how we use the include statement to duplicate repetitive tasks, clean up code, and overall expand our knowledge on the PHP language. Onward!</p>
<h3>PHP Include Tutorial &#8211; Basics Of The Include Language Construct</h3>
<p>It should be noted that, much like the Echo statement in PHP, Include is not considered an actual function. Although it would behave like a function, we call it a language construct since it is &#8220;built into&#8221; PHP. We consider it an integral part of the language much like the IF statement! (You may notice some language constructs listed as functions- but this is only to improve documentation.)</p>
<p>The primary usage of this language construct in particular is to retrieve a remote file for inclusion into the currently running script. We are going to use the Include construct for three primary reasons:</p>
<ol>
<li><strong>Readability</strong> &#8211; We use the Include construct for replacing many lines of code with but one line of code. This increases the readability of an application, and ultimately lets us troubleshoot bugs quicker than ever before. We use the Include construct for replacing many lines of code with but one line of code. This increases the readability of an application, and ultimately lets us troubleshoot bugs quicker than ever before.</li>
<li><strong>Save Time</strong> &#8211; We save time by using the Include() construct in a very ingenious way. If we were to change a link in our navigation menu, we would have to update that same link hundreds of time on different subpages. But if we used an Include() construct, we could just edit one file and the results on other pages would be instant!</li>
<li><strong>Reliability</strong> &#8211; If an included file doesn&#8217;t exist or has malfunctioned, the server will tell us quite promptly. This is superb for troubleshooting! Error reporting is sometimes seen as a security risk, however, since it commonly shows semi-confidential information in the error.</li>
</ol>
<h3>PHP Include Tutorial &#8211; Examples Of Including A File</h3>
<p>Before we can do dabble with an example, we need to learn how the Include syntax works. Using the Include construct is actually quite easy; we just need to know the URL of the code to be input, wrap it in parenthesis, and add the include statement.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">An Example Of The PHP Include Syntax</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: blue;">&lt;?php</span>
    <span style="color: red;">include</span>("header.php");
    <span style="color: red;">include</span> 'footer.php';

    <span style="color: green;">// Each Way Is Correct, But The First Example Is More Readable</span>
<span style="color: blue;">?&gt;</span></pre>
<p>In the above example we are calling to two different files, in which both are located in the same directory as the page being viewed. Both statements will work, but we personally like the first example since we view it as more readable.</p>
<p>If the file we wanted to include is in the parent directory, we simply use the &#8220;<strong>../</strong>&#8221; notation as seen below.</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Including A PHP File From A Parent Directory</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: blue;">&lt;?php</span>
    <span style="color: red;">include</span>("../header.php");

    <span style="color: green;">// Include A File From Parent Directory</span>
<span style="color: blue;">?&gt;</span></pre>
<p>Note that every time we use the &#8220;../&#8221; notation we go up one level. But things don&#8217;t have to get complicated! We can simplify things by simply using the entire URL as seen below:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">An Easier Way To Include A PHP File</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: blue;">&lt;?php</span>
    <span style="color: red;">include</span>("http://www.YourUrl.com/includes/header.php");

    <span style="color: green;">// Easy File Inclusion</span>
<span style="color: blue;">?&gt;</span></pre>
<h3>PHP Include Tutorial &#8211; An Include Example</h3>
<p>To get started we are going to need at least two files. First, we will take an already-made index.php file as seen below:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Contents Of index.php &#8211; Copy And Paste</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: blue;">&lt;html&gt;
&lt;title&gt;</span>A Basic PHP Website Using Include<span style="color: blue;">&lt;/title&gt;</span>

<span style="color: blue;">&lt;p</span> <span style="color: red;">style</span>="<span style="color: purple;">text-align: center;padding: 10px;"&gt;</span>
<span style="color: blue;">&lt;a</span> <span style="color: red;">href</span>="<span style="color: purple;">#</span>"&gt;Home<span style="color: blue;">&lt;/a&gt;</span>
<span style="color: blue;">&lt;a</span> <span style="color: red;">href</span>="<span style="color: purple;">#</span>"&gt;Subpage 1<span style="color: blue;">&lt;/a&gt;</span>
<span style="color: blue;">&lt;a</span> <span style="color: red;">href</span>="<span style="color: purple;">#</span>"&gt;Subpage 2<span style="color: blue;">&lt;/a&gt;</span>
<span style="color: blue;">&lt;/p&gt;</span>

<span style="color: blue;">&lt;p</span> <span style="color: red;">style</span>="<span style="color: purple;">text-align: center;border: 1px dotted blue;</span>"&gt;Welcome to our website! The links above are being used in includes- saving us time that can be best used for developing better features for this website.<span style="color: blue;">&lt;/p&gt;

<span style="color: blue;">&lt;/body&gt;
&lt;/html&gt;</span>

	</span></pre>
<p>Now create a file called &#8220;header.php&#8221; in another window. We are going to take out some of the contents of the index.php file and replace it with our include statement. Our header.php file should look like the following:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Contents Of header.php &#8211; Copy And Paste</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: blue;">&lt;?php
	&lt;a</span> <span style="color: red;">href</span>="#"&gt;Home<span style="color: blue;">&lt;/a&gt;
	&lt;a</span> <span style="color: red;">href</span>="#"&gt;Subpage 1<span style="color: blue;">&lt;/a&gt;
	&lt;a</span> <span style="color: red;">href</span>="#"&gt;Subpage 2<span style="color: blue;">&lt;/a&gt;
?&gt;</span></pre>
<p>Now we have two separate files. But we have a problem- how do we get the URLs from the header.php file into the index.php file? Simple! Review the code below to see where we put the include statement:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Contents Of header.php &#8211; Copy And Paste</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: blue;">&lt;html<span style="color: blue;">&gt;</span></span>
<span style="color: blue;">&lt;title<span style="color: blue;">&gt;</span></span>A Basic PHP Website Using Include<span style="color: blue;">&lt;/title<span style="color: blue;">&gt;</span></span>
<span style="color: blue;">&lt;body<span style="color: blue;">&gt;</span></span>

<span style="color: blue;">&lt;p</span><span style="color: red;"> style</span>="<span style="color: purple;">text-align: center;padding: 10px;</span>"<span style="color: blue;">&gt;</span>
<span style="color: red;">include</span>("<span style="color: purple;">header.php</span>"); ?<span style="color: blue;">&gt;</span>
<span style="color: blue;">&lt;/p</span><span style="color: blue;">&gt;</span>

<span style="color: blue;">&lt;p</span><span style="color: red;"> style</span>="<span style="color: purple;">text-align: center;border: 1px dotted blue;</span>"<span style="color: blue;">&gt;</span>
Welcome to our website! The links above are being used in includes- saving us time that can be best used for developing better features for this website.
<span style="color: blue;">&lt;/p</span><span style="color: blue;">&gt;</span>

<span style="color: blue;">&lt;/body<span style="color: blue;">&gt;</span></span>
<span style="color: blue;">&lt;/html<span style="color: blue;">&gt;</span></span></pre>
<p>Now upload both files to a same directory and test it out! If you&#8217;d like, you can change around the contents of the header.php file, upload it to your web server, and see the changes in real-time. This may not seem like a big time saver with only one page to use it on, but if we were to have a website filled with 100 pages, this would be quite the little time saver!</p>
<h3>PHP Include Tutorial: Dealing With Variable Scope</h3>
<p>Variable scope sounds like a mean phrase to anyone who doesn&#8217;t know what it is. The scope of a variable is just how far it &#8220;reaches&#8221;- or basically where it can and can&#8217;t be accessed. Variable scope can be seen when looking at two separate PHP scripts. If they are not connected in any way, then the variables of the first script will not interfere with variables in the second. (And vice versa)</p>
<p>We deal with scope in the Include construct quite simply because we are merging two files- so if we are using the same variable name in both scripts, which one gets precedence?</p>
<p>In our previous example we are not dealing with scope since we aren&#8217;t using any variables. But we can change that with the following edits to both index.php and header.php:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">header.php &#8211; Edits To Show Variable Scope</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: blue;">&lt;?php</span>
  <span style="color: navy;">$home</span> = "<span style="color: blue;">&lt;a</span> <span style="color: red;">href</span>='#'<span style="color: blue;">&gt;</span>Home<span style="color: blue;">&lt;a<span style="color: blue;">&gt;</span></span>";
  <span style="color: navy;">$sub1</span> = "<span style="color: blue;">&lt;a</span> <span style="color: red;">href</span>='#'<span style="color: blue;">&gt;</span>Subpage 1<span style="color: blue;">&lt;a<span style="color: blue;">&gt;</span></span>";
  <span style="color: navy;">$sub2</span> = "<span style="color: blue;">&lt;a</span> <span style="color: red;">href</span>='#'<span style="color: blue;">&gt;</span>Subpage 2<span style="color: blue;">&lt;a<span style="color: blue;">&gt;</span></span>";
<span style="color: blue;">?</span><span style="color: blue;">&gt;</span></pre>
<p>We just declared our three variables as our links, so that we may see which variables take precedence in our index.php file. Our index.php file should be updated as seen below:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">Contents Of index.php</p>
<pre style="border: 1px solid black; padding: 10px;">
<span style="color: blue;">&lt;html<span style="color: blue;">&gt;</span></span>
<span style="color: blue;">&lt;title<span style="color: blue;">&gt;</span></span>A Basic PHP Website Using Include<span style="color: blue;">&lt;/title<span style="color: blue;">&gt;</span></span>
<span style="color: blue;">&lt;body<span style="color: blue;">&gt;</span></span>

<span style="color: blue;">&lt;p</span><span style="color: red;"> style</span>="<span style="color: purple;">text-align: center;padding: 10px;</span>"<span style="color: blue;">&gt;</span>
<span style="color: blue;">&lt;?php</span> 

<span style="color: navy;">$home</span> = "";
<span style="color: navy;">$sub1</span> = "";
<span style="color: navy;">$sub2</span> = "";
<span style="color: red;">include</span>("header.php"); 

<span style="color: blue;">echo</span> "$home $sub1 $sub2";
?<span style="color: blue;">&gt;</span>
<span style="color: blue;">&lt;/p</span><span style="color: blue;">&gt;</span>

<span style="color: blue;">&lt;p</span><span style="color: red;"> style</span>="<span style="color: purple;">text-align: center;border: 1px dotted blue;</span>"<span style="color: blue;">&gt;</span>Welcome to our website! The links above are being used in includes- saving us time that can be best used for developing better features for this website.<span style="color: blue;">&lt;/p</span><span style="color: blue;">&gt;</span>

<span style="color: blue;">&lt;/body<span style="color: blue;">&gt;</span></span>
<span style="color: blue;">&lt;/html<span style="color: blue;">&gt;</span></span></pre>
<p>So what do you think the script will output to the browser? As you can see, we declared each variable as an empty string before we included the file. But in the include of the file, we stated that the variables should contain links.</p>
<p>Upon testing this script, one can see that the output is going to be successful in displaying the correct navigation links. From this example it is seen that an included file will indeed overwrite variable values unless otherwise coded.</p>
<p>It should also be made clear that an include file is only going to have access to variables and functions that have been declared before the line used to include the file. In the following example we will declare a variable and reference it within the include file. (No need to test this out unless you want- you can easily see the results with the two short snippets below)</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">index.php &#8211; Testing Variable Scope</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: blue;">&lt;?php</span>
  <span style="color: navy;">$hello</span> = "hello world!";
  <span style="color: red;">include</span>("<span style="color: blue;">echo</span>.php");
?<span style="color: blue;">&gt;</span></pre>
<p>Now for the echo.php file:</p>
<p style="font-size: 10px; color: gray; margin-bottom: -12px; text-indent: 15px;">echo.php &#8211; Testing Variable Scope</p>
<pre style="border: 1px solid black; padding: 10px;"><span style="color: blue;">&lt;?php</span>
  <span style="color: blue;">echo</span>  <span style="color: navy;">$hello</span>;
?<span style="color: blue;">&gt;</span></pre>
<p>Once this script is run, we will see that &#8220;hello world!&#8221; is output to the screen. If we were to put the variable after the include line, we wouldn&#8217;t get any results at all.</p>
<h3>PHP Include Tutorial &#8211; Two Major Security Concerns</h3>
<p>PHP is considered a generally secure language since it hides the contents of the PHP code from the user. (Go ahead and view the source of the HTML of pages we have created- you won&#8217;t see PHP code!) If we take this protection away, anyone can see the contents of our scripts. Luckily you have to make the mistake of not using the PHP extension for this to happen.</p>
<p>It is sometimes common to see some use plain text or even a .INC file type for their includes. If you use the wrong extension or remove the PHP tags from the header.php file we were working with, you&#8217;ll notice that the plain text is output to the screen if you try to access header.php directly. This goes to show that when we use includes, always remember to use the PHP tags to enclose the data if it is using any type of sensitive data. (Or any time at all, since it builds good programming practice!)</p>
<p>The second major security concern is including files from a remote location. If we are only using local resources on our website, such as template files we have created, there is no reason to fret. But if we are pulling information from other websites, which is sometimes illegal in many cases anyway, the remote website can inject code into our websites and get confidential information with ease. As such, developers should only agree to use includes from remote resources if they are to be trusted without doubt.</p>
<h4>Closing Comments</h4>
<p>As we get more familiar with the Include statement, we can get a little more skilled in the way we use it. From using Includes in functions or even using IF statements to selectively include correct files, there is much to expand on here.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.learnphponline.com/php-basics/php-include-tutorial/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
