<?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>thinkMoult &#187; framework</title>
	<atom:link href="http://thinkmoult.com/tag/framework/feed/" rel="self" type="application/rss+xml" />
	<link>http://thinkmoult.com</link>
	<description>Seriously who ever reads this description.</description>
	<lastBuildDate>Sun, 08 Apr 2012 03:22:49 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>How to use CodeIgniter&#8217;s OpenID library to integrate OpenID in your existing user system.</title>
		<link>http://thinkmoult.com/2009/02/22/use-codeigniter-openid-library-to-integrate-openid/</link>
		<comments>http://thinkmoult.com/2009/02/22/use-codeigniter-openid-library-to-integrate-openid/#comments</comments>
		<pubDate>Sun, 22 Feb 2009 08:42:19 +0000</pubDate>
		<dc:creator>Dion Moult</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[library]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[openid]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[register]]></category>

		<guid isPermaLink="false">http://thinkmoult.com/?p=478</guid>
		<description><![CDATA[Apparently OpenID is all the rage nowadays in the coding world and those using the CodeIgniter framework are left all alone without a proper guide on how to get it working. So as a budding programmer I is be halping all of you. Yes, it IS a long post, but it should help if you [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>Apparently OpenID is all the rage nowadays in the coding world and those using the CodeIgniter framework are left all alone without a proper guide on how to get it working. So as a budding programmer I is be halping all of you. Yes, it IS a long post, but it should help if you read it step by step.</p>
<h4>INTRODUCTION</h4>
<p>Here is a nifty short guide to get people to use the CodeIgniter OpenID library to start using OpenID in your web application. Firstly I will assume you already have a registration system of some sort, and if you don&#8217;t, you should, because not everybody knows/cares about OpenID. What we have to do to spread OpenID is to provide a choice to people, not force them upon it &#8211; forcing people would simply get them to leave.</p>
<h4>BRIEFING OF SYSTEM</h4>
<p>So, let&#8217;s say you have a registration system that uses a table to store some sort of user ID (username/email/uid/whatever) and some sort of password. You also have a login form which all works fine, and now you want to allow people to use OpenID on that system.</p>
<p>Here&#8217;s the briefing on how it&#8217;s going to work. I&#8217;m assuming you know how OpenID is used already. I&#8217;m also assuming you are comfortable with PHP. What will happen is that in the place of the username field (or if you want you can make a separate form for it) people have the choice to just type in their OpenID URL, then ignore the password field. When they click the login button, firstly we will do a check for whether or not it is an OpenID URL.  If it is not valid, then we just continue checking if it&#8217;s a proper user account like our previous system and everything is fine. If it IS a valid OpenID URL, then it will perform the usual &#8220;please authenticate&#8221; checks with the OpenID server, and once that&#8217;s done, we&#8217;ll add an OpenID row to a NEW OpenID table which&#8217;ll store this stuff, and that table will also have a UID column, which allows us to bind an OpenID URL to a user account. <em>Note that this is a one way bind, so many OpenIDs can refer to a single user account, but not the other way around</em>.</p>
<p>This means<strong> you will have to create another table in your database</strong>, this depends on what type of database you are using, but in general you should have two columns:</p>
<p>Column 1) openid_url<br />
This would be a varchar field to store a canonicalised version of the openid<br />
url, for example http://foo.bar.com/</p>
<p>Column 2) user_id<br />
This would be an INT that would contain the userid of the username this openid<br />
url is binded to. This should be the key field as it must be unique.</p>
<p>Here is the SQL that should create this for you, but remember it only applies on SQL databases.</p>
<p><code>CREATE TABLE IF NOT EXISTS `user_openids` (<br />
`openid_url` varchar(255) NOT NULL,<br />
`user_id` int(11) NOT NULL,<br />
PRIMARY KEY  (`openid_url`),<br />
KEY `user_id` (`user_id`)<br />
) ENGINE=MyISAM DEFAULT CHARSET=utf8;</code></p>
<p>Now as a final note, what if a user already has account, but wants an OpenID to bind to that one instead of creating a new one? The simplest way to do this is to automatically create a new one, but have an option, perhaps in the user control panel or settings page that will allow them to &#8220;bind&#8221; to an existing account. They will have to provide that account&#8217;s user/pass to authenticate they are allowed to bind to it, and if they do, then the original account will be deleted, the user_id will change in the user_openids table, and the user will have to relogin to continue. Tada. Simple.</p>
<p>OK, let&#8217;s get started.</p>
<h4>STEP 1: Setting up the libraries.</h4>
<p>Alright, the first step is to get the library. You can <a href="http://codeigniter.com/wiki/OpenID/">obtain it from the CodeIgniter Wiki</a>.  Now go and <strong>upload all the files</strong>. This library is &#8230; well &#8230; just a CI library, so that means it&#8217;s limited to CI functions and so it also needs the actual PHP OpenID Library to actually interface with OpenID itself. So go and <a href="http://www.openidenabled.com/php-openid/">download the PHP OpenID Library</a> then <strong>upload the Auth/ directory in your system/application/libraries/ directory</strong>.  Note that this is the same place as the Openid.php file you uploaded from the CI OpenID Library. For those impatient folks who have already run the test controllers, it should&#8217;ve failed on you asking for mkdir permissions. Well, instead of chmodding our entire system, we&#8217;re just going to add those directories for it ourselves. <strong>Go to the CI root directory (the one that contains the system/ directory) and add another directory called tmp/. Go inside tmp/ and then add three more directories called associations/ and nonces/ and temp/</strong>. That should be enough to set up the library.</p>
<h4>STEP 2: Testing if it works.</h4>
<p>The next step, now that you supposedly have the files in the right places, is to check whether or not it works. Thankfully the CI OpenID library has already provided us with a test.php controller. So, what are you waiting for? <strong>Go to yoursite.com/test and put in your OpenID URL and see if it authenticates.</strong> Obviously if you don&#8217;t have an OpenID URL to test with, you need to go get one.  <a href="http://http://openid.net/get/">There are many providers available</a>. If it works flawlessly (don&#8217;t forget to test with a non-valid URL to see if it catches that too) then skip the next paragraph. Or you can read it anyway.</p>
<h4>OPTIONAL: If it doesn&#8217;t work?</h4>
<p>Now, let&#8217;s say it&#8217;s mucked up for you. If you get nonce already used errors, try clearing out all the files in the tmp/ directory you made earlier on. Other nonces errors or authentication/redirection errors can be a problem with your CI configuration. Go to your system/application/config directory and edit your config.php file. Look for the uri_protocol option and try all of the options there. I find AUTO works fine for me but on my remote server apparently it only likes ORIG_PATH_INFO. Another glitch I encountered on my localhost is that the server would deny my authentication request. This is probably an Apache setting on my localhost but I played so much with my Apache settings it&#8217;s probably all my fault. Uploading to my remote server showed it worked perfectly there, but if you&#8217;re having problems testing it on your localhost, you might want to apply this very dirty hack. Go to your system/application/libraries/Auth/ directory and edit the OpenID.php file. The very first function is called isFailure($thing). Now screw them but right now I don&#8217;t care about authentication because I know it works on a non-mucked up server. So comment out the return is_a($thing&#8230; line and add a line after then to just simply return false;. You&#8217;ll get:</p>
<p><code>function isFailure($thing)<br />
{<br />
<strong> //return is_a($thing, 'Auth_OpenID_FailureResponse');</strong><br />
<strong> return False;</strong><br />
}</code></p>
<p><strong>Note that I DO NOT RECOMMEND YOU DO THIS. IT IS A HACK IF YOU HAVE PROBLEMS RUNNING IT ON A TESTING SERVER. MAKE SURE YOU KNOW IT WORKS PROPERLY OTHERWISE.</strong></p>
<p>Ok. Assuming now everything is working fine on the test controller, let&#8217;s start integrating it into our system.</p>
<h4>STEP 3: Port the test controller to your own system.</h4>
<p>Now we having a working test controller, but it isn&#8217;t part of our own system yet. Let&#8217;s say you have another controller which contains a Users class which deals with people logging in. <strong>Comment out all the code so that after they try to login using your existing form, it does nothing</strong>. <em>Therefore, if your login form points to &lt;form action=&#8221;foo/bar&#8221;&gt;, you will comment out all the code in bar()</em>. Now we are going to add the OpenID check.</p>
<p>Because a test controller already exists, this speeds up the reverse engineering process significantly as we already have a working example of use. All we have to do is do code reuse in the right places. This is a bit hard to guide because all of our user systems are different in terms of the code. So here you&#8217;ll have to use your own wits on if you need modifications to work for your own system.</p>
<p>Here is the system flow chart of how things are going to work &#8211; existing processes you should already have on your system are marked in dotted lines:</p>
<p style="text-align: center;"><a href="http://thinkmoult.com/wp-content/uploads/2009/02/diagram1.png"><img class="size-full wp-image-479 aligncenter" title="diagram1" src="http://thinkmoult.com/wp-content/uploads/2009/02/diagram1.png" border="0" alt="diagram1" width="453" height="626" /></a></p>
<p>Firstly we want to look at section <strong>(A)</strong>. This section deals with checking if it is a valid OpenID URL. This can be done by copying the test controller function exactly. I have also added at the beginning some code, so read the comments as to why. Put this code in the function where your login form would normally point to in the &lt;form action=&#8221;foo/bar&#8221;&gt;. So therefore you would put it in the bar() function. <em>For the moment, we are going to disregard going to section </em><strong>(D)</strong><em>, as this is covered later on in </em><strong>Step 4</strong>. Here is the code you have to put in bar():</p>
<p><code> // Before we check if the username is in fact an OpenID, we must secure<br />
// the variable. OpenID refers to the url as $user_id, so be sure to assign<br />
// the value of the inputted openid to $user_id. Here is have used<br />
// $this-&gt;username but this might change on your system.<br />
$user_id = trim($this-&gt;username);<br />
$user_id = htmlspecialchars($user_id);</code></p>
<p><code>// Load the neccessary OpenID libraries as shown by the example.<br />
$this-&gt;lang-&gt;load('openid', 'english');<br />
$this-&gt;load-&gt;library('openid');</code></p>
<p><code>$this-&gt;config-&gt;load('openid');<br />
$req = $this-&gt;config-&gt;item('openid_required');<br />
$opt = $this-&gt;config-&gt;item('openid_optional');<br />
$policy = site_url($this-&gt;config-&gt;item('openid_policy'));<br />
$request_to = site_url($this-&gt;config-&gt;item('openid_request_to'));</code></p>
<p><code>$this-&gt;openid-&gt;set_request_to($request_to);<br />
$this-&gt;openid-&gt;set_trust_root(base_url());<br />
$this-&gt;openid-&gt;set_args(null);<br />
$this-&gt;openid-&gt;set_sreg(true, $req, $opt, $policy);<br />
$pape_policy_uris = array();<br />
$this-&gt;openid-&gt;set_pape(true, $pape_policy_uris);<br />
$this-&gt;openid-&gt;authenticate($user_id);</code></p>
<p>Now, to better integrate into your system (<em>this is optional, but HIGHLY recommended</em>) you should go into your system/application/config directory and edit the openid.php file. Edit it to fit your needs. openid_required is the required information you want to grab from the openid provider, and openid_optional is the optional information you want to grab. That&#8217;s all good and fine, but what we&#8217;re interested in is the <em>openid_request_to</em> variable. This is the link to the function that checks the authenticity of the openid URL.  Right now it goes to the test check function, but I would recreate this check() function in my own controller, as keeping it in a test controller is &#8230; stupid.</p>
<p>So if you change the <em>openid_request_to</em> option, <strong>you need to recreate the check() function</strong>. So it&#8217;s practically a copy and paste. Just copy the check() function from the test controller into the controller you want to store the check() function in. Here is the copy and pasted code, with some added comments to show where things happen:</p>
<p><code>/**<br />
* Checks for proper OpenID authentication.<br />
*/<br />
public function check()<br />
{<br />
$this-&gt;lang-&gt;load('openid', 'english');<br />
$this-&gt;load-&gt;library('openid');<br />
$this-&gt;config-&gt;load('openid');<br />
$request_to = site_url($this-&gt;config-&gt;item('openid_request_to'));</code></p>
<p><code>$this-&gt;openid-&gt;set_request_to($request_to);<br />
$response = $this-&gt;openid-&gt;getResponse();</code></p>
<p><code>switch ($response-&gt;status)<br />
{<br />
<strong> // This is probably the most important bit of the check. The switch case<br />
// statements. They're pretty obvious as to what they do. All the same,<br />
// I have bolded part of their names for the hard of thinking ... haha.</strong><br />
case Auth_OpenID_<strong>CANCEL</strong>:<br />
$data['msg'] = $this-&gt;lang-&gt;line('openid_cancel');<br />
break;<br />
case Auth_OpenID_<strong>FAILURE</strong>:<br />
$data['error'] = $this-&gt;_set_message('openid_failure', $response-&gt;message);<br />
break;<br />
case Auth_OpenID_<strong>SUCCESS</strong>:<br />
$openid = $response-&gt;getDisplayIdentifier();<br />
$esc_identity = htmlspecialchars($openid, ENT_QUOTES);</code></p>
<p><code>$data['success'] = $this-&gt;_set_message('openid_success', array($esc_identity, $esc_identity), array('%s','%t'));</code></p>
<p><code>if ($response-&gt;endpoint-&gt;canonicalID) {<br />
$data['success'] .= $this-&gt;_set_message('openid_canonical', $response-&gt;endpoint-&gt;canonicalID);<br />
}</code></p>
<p><code>$sreg_resp = Auth_OpenID_SRegResponse::fromSuccessResponse($response);<br />
$sreg = $sreg_resp-&gt;contents();</code></p>
<p><code>foreach ($sreg as $key =&gt; $value)<br />
{<br />
$$key = $value;<br />
$data['success'] .= $this-&gt;_set_message('openid_content', array($key, $value), array('%s','%t'));<br />
}</code></p>
<p><code>$pape_resp = Auth_OpenID_PAPE_Response::fromSuccessResponse($response);</code></p>
<p><code>if ($pape_resp)<br />
{<br />
if ($pape_resp-&gt;auth_policies)<br />
{<br />
$data['success'] .= $this-&gt;lang-&gt;line('openid_pape_policies_affected');</code></p>
<p><code>foreach ($pape_resp-&gt;auth_policies as $uri)<br />
{<br />
$data['success'] .= "&lt;li&gt;&lt;tt&gt;$uri&lt;/tt&gt;&lt;/li&gt;";<br />
}</code></p>
<p><code>$data['success'] .= "&lt;/ul&gt;";<br />
}<br />
else<br />
{<br />
$data['success'] .= $this-&gt;lang-&gt;line('openid_pape_not_affected');<br />
}</code></p>
<p><code>if ($pape_resp-&gt;auth_age)<br />
{<br />
$data['success'] .= $this-&gt;_set_message('openid_auth_age', $pape_resp-&gt;auth_age);<br />
}</code></p>
<p><code>if ($pape_resp-&gt;nist_auth_level)<br />
{<br />
$data['success'] .= $this-&gt;_set_message('openid_nist_level', $pape_resp-&gt;nist_auth_level);<br />
}<br />
}<br />
else<br />
{<br />
$data['success'] .= $this-&gt;lang-&gt;line('openid_pape_noresponse');<br />
}</code></p>
<p><code><strong> // At this point, we have authenticated the OpenID COMPLETELY, and<br />
// we have also grabbed whatever information we have requested. For<br />
// example:</strong></code></p>
<p><code><strong>// $nickname is the nickname we have grabbed in openid_required. It<br />
// may or may not be empty.<br />
echo $nickname;<br />
if ( !$nickname )<br />
{<br />
// If $nickname is blank, and we require it for our system, we<br />
// should now execute some code that will ask them to fill out a<br />
// nickname. This is for you to figure out. It will probably<br />
// loading a separate view or redirecting.<br />
}<br />
else<br />
{<br />
// If we have all the required information we need, we can move on<br />
// to section (C) in the flow chart.<br />
}</strong></code></p>
<p><code><strong> </strong><strong> // Remember we had to store the openid URL in a new table so we can<br />
// bind them to users? $esc_identity is the variable you need. Note:<br />
// it is canonicalised - that means that the format is standardized<br />
// such as foo.bar.com becomes http://foo.bar.com/.<br />
echo $esc_identity;</strong></code></p>
<p><code>break;<br />
}</code></p>
<p><code>$data['pape_policy_uris'] = array(<br />
PAPE_AUTH_MULTI_FACTOR_PHYSICAL,<br />
PAPE_AUTH_MULTI_FACTOR,<br />
PAPE_AUTH_PHISHING_RESISTANT<br />
);<br />
<strong> // I have commented out the load-&gt;view because we want to use our own<br />
// view. See flow chart. You will have to edit it so that it does what<br />
// YOU want it to do. All I am doing is showing you how to set up a<br />
// basic structure.</strong></code></p>
<p><code><strong> //$this-&gt;load-&gt;view('view_openid', $data); </strong><br />
}</code></p>
<p>Ok. After you&#8217;re sure you know or have a brief idea of what&#8217;s going on, also note that several other functions are referenced to. <strong>So you&#8217;ll also have to add the _set_message() function to this controller. Just copy and paste from the test.php controller. </strong>MAKE SURE YOU DO THIS.</p>
<p>Now we have a working check() function. So if we&#8217;re on the SUCCESS switch case statement, we now have the requested information we need and an open ID url. The requested information (in the example code above it&#8217;s $nickname) can be used in section <strong>(B)</strong> of the flow diagram, and the canonicalised openid URL can be used in section <strong>(C) </strong>of the flow diagram. So hooray! We now have a working OpenID system. How you are going to display the form to request for additional information is UP TO YOU on how you are going to add it, as it depends on your system.</p>
<h4>STEP 4: Integrate OpenID check with normal usersystem checks.</h4>
<p>We&#8217;re almost done here. Let&#8217;s take another look at section <strong>(A)</strong> of the flow diagram. Through reverse engineering the OpenID library, we can see that when it checks if it is a valid openid URL, and returns FALSE,<em> it will use the _set_message() function to and give an appropriate error message</em>. <strong>This _set_message() function can be found in the system/application/libraries/Openid.php file</strong>. If you look at it, it will echo the error message then call an exit(). Now, we don&#8217;t want to grudge our users with annoying error messages, especially if there is NOTHING wrong if they don&#8217;t provide an openid URL. (<em>Remember, I am assuming you are merging your existing login box with your openID login box. NOT making a new openid login box &#8211; if you are making a new login box, you can pretty much disregard this step</em>.) We also don&#8217;t want to call an exit(), because we want to continue with section <strong>(D)</strong>. So<strong> comment out the echo and the exit line</strong>. After commenting them out, we want to replace the exit with something that will tell our script to skip the rest of our OpenID check and go straight to our usual account checks.</p>
<p>Here&#8217;s how we do it. (There might be alternatives, but this is what I think is the easiest) <strong>We will add a header redirect in place of the exit; line</strong>. Here&#8217;s what you should get:</p>
<p><code>function _set_message($error, $msg, $val = '', $sub = '%s')<br />
{<br />
$CI =&amp; get_instance();<br />
$CI-&gt;lang-&gt;load('openid', 'english');<br />
<strong> //echo str_replace($sub, $val, $CI-&gt;lang-&gt;line($msg));</strong></code></p>
<p><code> </code></p>
<p><code> if ($error)<br />
{<br />
<strong> //exit;</strong><br />
<strong> header( 'Location: /foo/bar/0' ) ;</strong><br />
}<br />
}</code></p>
<p>Where foo/bar goes to the same place as your &lt;form action=&#8221;whatever&#8221;&gt; we talked about in the beginning. <em>Notice I pass a parameter &#8217;0&#8242; to the function</em>. <strong>This is so that we can restructure the existing bar() function to follow this sort of pseudo code</strong>:</p>
<p><code>function bar($check_openid=1)<br />
{<br />
if ( $check_openid )<br />
{<br />
// Here you execute the OpenID check code (NOT THE check() FUNCTION)<br />
// that you added in step 2.<br />
// Note that successful OpenID logins/registrations will occur in<br />
// the check() function, which is elsewhere.<br />
}</code></p>
<p><code> </code></p>
<p><code> if ( !$check_openid )<br />
{<br />
// Now you continue with the code the describes section (D) of the<br />
// flow diagram.<br />
}<br />
}</code></p>
<p>&#8230; and that&#8217;s it! You should now have the basis of a system that accepts both traditional user logins and OpenID. <strong>Obviously you have a lot of extra to add in but if you&#8217;re here anyway you should know how to adapt it to your system and what else you have to add</strong>. Particularly in sections <strong>(B)</strong> and <strong>(C)</strong>. I&#8217;m not going to guide you how to do those steps as it depends on what information you want, how you want to display your request form, and how your database is structured. I&#8217;m very sorry, but <strong>you will have to work it out yourself</strong>. (It shouldn&#8217;t be too hard)</p>
<p>I have tested this as this is a running documentation based on what I have done to integrate OpenID in my own CI run site. However of course there may be bugs and things I&#8217;ve missed out, any comments will be appreciated, and of course I will answer questions (just leave it as a comment)! :) Spelling corrections are also welcome.</p>
<p>Please do NOT copy this post. Just link to this page if you want to share it with others.</p>
<p>I hope it has helped! <strong>Please leave a comment as I love &#8220;thanks yous&#8221;.</strong></p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://thinkmoult.com/2009/02/22/use-codeigniter-openid-library-to-integrate-openid/feed/</wfw:commentRss>
		<slash:comments>96</slash:comments>
		</item>
		<item>
		<title>CodeIgniter &#8211; Simple, clean, MVC framework for PHP.</title>
		<link>http://thinkmoult.com/2008/12/20/codeigniter-simple-clean-mvc-framework-for-php/</link>
		<comments>http://thinkmoult.com/2008/12/20/codeigniter-simple-clean-mvc-framework-for-php/#comments</comments>
		<pubDate>Sat, 20 Dec 2008 12:29:21 +0000</pubDate>
		<dc:creator>Dion Moult</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[codeigniter]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[mvc]]></category>
		<category><![CDATA[oop]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://thinkmoult.com/?p=306</guid>
		<description><![CDATA[People learning PHP go through the wonderful stages of &#60;?php echo 'Hello, world!'; ?&#62;, the then branch off into either: 1) Wow! It&#8217;s so dynamic! Let&#8217;s create everything right here right now! or 2) Wow! It&#8217;s so messed up I don&#8217;t know $int from $char! Unfortunately, the people who belong in (1) tend to spend [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>People learning PHP go through the wonderful stages of <code>&lt;?php echo 'Hello, world!'; ?&gt;</code>, the then branch off into either: 1) Wow! It&#8217;s so dynamic! Let&#8217;s create everything right here right now! or 2) Wow! It&#8217;s so messed up I don&#8217;t know <code>$int</code> from <code>$char</code>! Unfortunately, the people who belong in (1) tend to spend the next couple years in an ignorant bliss, spewing out hundreds of lines of non-OOP code with a structure somewhat like this:</p>
<blockquote>
<pre><code>&lt;?php
include './inc/template/header.php';
if ( $p == '' )
{</code></pre>
<pre style="padding-left: 30px;">include './inc/pages/index.php';</pre>
<pre><code>}
elseif ( $p == 'about')
{</code></pre>
<pre style="padding-left: 30px;">include './inc/pages/about.php';</pre>
<pre><code>}</code></pre>
<pre><strong>[... etc ...]</strong></pre>
<pre><code>else
{</code></pre>
<pre style="padding-left: 30px;">include './inc/template/404.php';</pre>
<pre><code>}
include 'footer.php';
?&gt;</code></pre>
</blockquote>
<p>Well. Look familiar to anybody? (or <em>even worse</em> examples!) Hah! My good sir, you have just missed out on the programming epiphany of the century. Any script kiddy with the guts to program before learning OOP should have their guts removed, quartered, and stuck up as a warning to other coders. So, if you are doing PHP, but you don&#8217;t know what OOP or MVC is &#8211; here is your cue to fricassé your arse and get ready for some late nights of programming ecstasy.</p>
<p>This post is somewhat a half-rant about the disadvantages of coding without any OOP or proper framework, and also a sort of happy unrelated babble talk brought about by me recently opening up (g)Vim again to code some very simple PHP, then taking a look at <em>POSE2</em>. For those that don&#8217;t know, <em>POSE2</em> was the project to basically make E2 as open as possible. It&#8217;s an MVC (Model-View-Controller) framework, with no built-in helper classes. It&#8217;s extremely minimalistic. It was used to construct VisionBin, and <a href="#mce_temp_url#">E2-Productions.com</a> currently runs on it too.</p>
<p>Hilariously enough, I fell into the common programmer trap of recreating the wheel. That&#8217;s why now, instead of advertising my oh-so-awesome <em>POSE2</em> system, I&#8217;m going to be recommending you CodeIgniter. CI is a fast, clean, <em>superbly </em>documented MVC PHP framework. Or so I say before trying it out properly. Let&#8217;s actually see how awesome it is. I will type this post out <em>live</em> as I convert the extremely simple E2 site from <em>POSE2</em> to CI.</p>
<p>Ok, apparently my connection hates me as it keeps on failing to download the tiny CI source files. After several retries, I finally am ready to start &#8230;. ok, uploading onto E2. This is great as I don&#8217;t get a lot of extra unneeded files in the / directory (except for index.php, obviously!), so everything is nicely tucked away in the system directory. Ok, quite a lot of files being uploaded here, but compared to other frameworks, this is slim. Let&#8217;s try visiting it without bothering to read any installation documents. Yep, already, it&#8217;s working without any needed config. I don&#8217;t see any installation docs (perhaps I&#8217;m just blur) but I do notice a config/ directory. It&#8217;s wonderfully documented and there&#8217;s quite a minimal amount to setup. .htaccess is a pain to setup due to specific server configurations (took quite a bit of debugging, but the option to fix it was hidden in the configuration), but other than that, it worked flawlessly. Easy to create new views, models, and controllers. Lots of helper classes &#8211; perfect, looks like exactly what anybody needs. Embeddable views to enable for templating&#8230;perfect.</p>
<p>First impression: wonderful, fast, user-friendly and amazingly well documented. Will give future updates on how I find it as I use it more. Meanwhile, I definitely recommend it to people.</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://thinkmoult.com/2008/12/20/codeigniter-simple-clean-mvc-framework-for-php/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Learn how to make a basic website framework.</title>
		<link>http://thinkmoult.com/2008/03/23/learn-how-to-make-a-basic-website-framework/</link>
		<comments>http://thinkmoult.com/2008/03/23/learn-how-to-make-a-basic-website-framework/#comments</comments>
		<pubDate>Sun, 23 Mar 2008 05:30:00 +0000</pubDate>
		<dc:creator>Dion Moult</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[framework]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[website]]></category>

		<guid isPermaLink="false">http://thinkmoult.com/?p=13</guid>
		<description><![CDATA[This will teach you how to setup one page for all pages! So it&#8217;ll be a lot easier when changing layouts! Not only that, say GOODBYE to /index.php?page=contact&#8230;now you can just have /contact without actually making the directory! This will ALSO teach you how to have a site which you can easily switch version (like [...]
No related posts.]]></description>
			<content:encoded><![CDATA[<p>This will teach you how to setup one page for all pages! So it&#8217;ll be a lot easier when changing layouts!<br />
Not only that, say GOODBYE to /index.php?page=contact&#8230;now you can just have /contact without actually making the directory!<br />
This will ALSO teach you how to have a site which you can easily switch version (like when running parallel systems) when you are working on a new version and you don&#8217;t want others to know. It will have password protection which will allow only authorized users to test the version.<br />
Say goodbye to &#8220;coming soon&#8221; pages!</p>
<p>Ok, first we want a layout&#8230;Let&#8217;s be simple (just forget validation temporarily, this just an example):</p>
<p>My title.&lt;br /&gt;<br />
&lt;a href=&#8221;http://site.com&#8221;&gt;Home&lt;/a&gt;&lt;br /&gt;<br />
&lt;a href=&#8221;http://site.com/asdf&#8221;&gt;asdf&lt;/a&gt;&lt;br /&gt;<br />
content will go here&lt;br /&gt;<br />
Copyright.</p>
<p>Let&#8217;s save that file as index.php. Save it in a directory called &#8220;versionname&#8221; (you can change this, but remember to change the rest of the code as well) Now, replace &#8220;content will go here&#8221; with this:</p>
<p>&lt;?php<br />
if($page == &#8220;&#8221;) {<br />
include &#8220;/home/username/public_html/versionname/home.php&#8221;;<br />
} elseif($page == &#8220;asdf&#8221;) {<br />
include &#8220;/home/username/public_html/versionname/asdf.php&#8221;;<br />
} else {<br />
echo &#8220;error&#8221;;<br />
}<br />
?&gt;</p>
<p>Basically, it will see what the value of the variable $page is and accordingly, it will display the correct page.<br />
Create a new page called home.php (you can put whatever you want in it), and save it in the &#8220;versionname&#8221; directory. Then create a new page called asdf.php and put whatever you want in it and save it in the &#8220;versionname&#8221; directory.</p>
<p>Now try and go to yoursite.com/versionname. It will display your home page. Then go to yoursite.com/versionname/?page=asdf. It will display your asdf page. Let&#8217;s say in the future you decide to change your layout&#8230;you only need to change one page&#8230;the index.php page.</p>
<p>Ok. now instead of ?page=asdf, lets turn it into just /asdf. Go into your public_html folder (or your equivalent of it) and search for .htaccess. Open it up. This is apache server scripting. Won&#8217;t go into much detail though. You&#8217;re probably annoyed because you want your homepage to be in yoursite.com and not yoursite.com/versionname. Simple, add this code in .htaccess:</p>
<p>RewriteEngine on<br />
RewriteBase /</p>
<p>ReWriteRule ^/$ versionname/index.php<br />
ReWriteRule ^$ versionname/index.php</p>
<p>Now, try to go to yoursite.com. Amazing! The URL didn&#8217;t even change! Now to change into /asdf. add this:</p>
<p>ReWriteRule ^asdf/$ versionname/index.php?page=asdf<br />
ReWriteRule ^asdf$ versionname/index.php?page=asdf</p>
<p>Now, go to yoursite.com/asdf. Amazing! It showed the ?page=asdf page!<br />
Ok, now that you&#8217;ve done all that, let&#8217;s say you want a new version, but you don&#8217;t want to delete everything and setup a coming soon page. Create your new version in a directory called &#8220;versionname2&#8243;.<br />
Now, you want people to be able to access your new version, but not all people. So here&#8217;s what you do. Add this to the top of your versionname (not 2) index.php:</p>
<p>&lt;?php<br />
session_start();<br />
header(&#8220;Cache-control: private&#8221;);<br />
if( $_POST[beta] ) {<br />
if( $_POST[pass] == &#8220;yourpassword&#8221; ) {<br />
$_SESSION["beta"] = &#8220;1&#8243;;<br />
}}<br />
if( $_SESSION[beta] == 1 ) {<br />
include &#8220;/home/username/public_html/versionname2/index.php&#8221;;<br />
} else {<br />
?&gt;</p>
<p>And now, add the form below to wherever you want the verification to be:</p>
<p>&lt;form method=&#8221;post&#8221; action=&#8221;"&gt;&lt;br /&gt;<br />
&lt;input name=&#8221;pass&#8221; type=&#8221;password&#8221; class=&#8221;input&#8221;&gt;<br />
&lt;input name=&#8221;beta&#8221; type=&#8221;submit&#8221; value=&#8221;betatest&#8221;&gt;<br />
&lt;/form&gt;</p>
<p>So now, when you type in the password &#8220;yourpassword&#8221; in the form, you will be browsing your new version, with no edited URLs! Of course, to make it fully compatible you need to add the if $page=&#8221;" statements to your versionname2 index.php, but that&#8217;s simple to do.<br />
Ok, you&#8217;ve finished coding your new version. Simple, just go to your .htaccess, and replace all the /versionname to /versionname2. Done! You&#8217;ve succesfully upgraded your site.</p>
<p>Note: this is very basic but I hope it has helped some of you.</p>
<p>No related posts.</p>]]></content:encoded>
			<wfw:commentRss>http://thinkmoult.com/2008/03/23/learn-how-to-make-a-basic-website-framework/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

