<?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>Tom&#039;s Big Box &#187; CMS</title>
	<atom:link href="http://tomsbigbox.com/tag/cms/feed/" rel="self" type="application/rss+xml" />
	<link>http://tomsbigbox.com</link>
	<description>The metaphorical box belonging to Tom</description>
	<lastBuildDate>Fri, 03 Sep 2010 08:47:33 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Building a CMS Part 3</title>
		<link>http://tomsbigbox.com/building-a-cms-part-3/</link>
		<comments>http://tomsbigbox.com/building-a-cms-part-3/#comments</comments>
		<pubDate>Sun, 17 Jan 2010 18:10:57 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Building a CMS]]></category>
		<category><![CDATA[CMS]]></category>
		<category><![CDATA[Content]]></category>
		<category><![CDATA[Content Management System]]></category>
		<category><![CDATA[How to]]></category>

		<guid isPermaLink="false">http://tomsbigbox.com/?p=543</guid>
		<description><![CDATA[And so we've arrived at the last in this set of tutorials; if you've been following along, by now you should have a user system as well as a basic CMS. At the moment we have a very strong base for our CMS but there are still a few things that we need to talk...</p><a class="read-more" href="http://tomsbigbox.com/building-a-cms-part-3/">Continue Reading...</a>]]></description>
			<content:encoded><![CDATA[<p>And so we've arrived at the last in this set of tutorials; if you've been following along, by now you should have a user system as well as a basic CMS. At the moment we have a very strong base for our CMS but there are still a few things that we need to talk about. Firstly style. I like to think of myself as a stylish kind of guy <img src='http://tomsbigbox.com/core/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  and I like to keep my work stylish to, so now were are going to implement some CSS to make our CMS at least moderately aesthetically pleasing. We are going to keep it simple and if you would like to change it feel free to do so, this is just a suggestion; in fact if you are intending on integrating this CMS into your own site, why not give it the same look? </p>
<p>Now before we get to the CSS you will need to copy the tiny bit of code below between your <span class="code-small">&lt;head&gt;</span> and <span class="code-small">&lt;/head&gt;</span> tags of every page that you want styled.</p>
<div class="code">&lt;link href="style.css" rel="stylesheet" media="screen"/&gt;</div>
<p>That's the code for linking the CSS to the page. Right then let's just get this CSS out of the way. So go ahead and create a new file called <span class="code-small">style.css</span> and paste in the following code.</p>
<div class="code">
* {<br />
	margin: 0px;<br />
	padding: 0px;<br />
}</p>
<p>/*---Login Page---*/</p>
<p>#login {<br />
	width:500px;<br />
	height:250px;<br />
	background-color:#fff;<br />
	-moz-border-radius: 10px;<br />
	-webkit-border-radius:10px;<br />
	display: block;<br />
	top:50%;<br />
	left:50%;<br />
	margin-left:-250px;<br />
	margin-top: -125px;<br />
	position: absolute;<br />
}</p>
<p>input.textbox, input.edit {<br />
	width:450px;<br />
	margin:5px 24px 0px 24px;<br />
	border:1px solid #cfcfcf;<br />
	-moz-border-radius:5px;<br />
	-webkit-border-radius:5px;<br />
	height:35px;<br />
	font-family: Georgia, "Times New Roman", Times, serif;<br />
	color: #7f7f7f;<br />
	padding:5px;<br />
	font-size: 20px;<br />
}</p>
<p>input.submit, input.editbutton, #submit {<br />
	width:460px;<br />
	height:45px;<br />
	cursor:pointer;<br />
	font-family:Calibri, Arial, Helvetica, sans-serif;<br />
	font-size:19px;<br />
	color:#FFF;<br />
	background-color:#bfbfbf;<br />
	border:#cfcfcf;<br />
	border-style:solid;<br />
	border-width:1px;<br />
	padding:3px 4px;<br />
	-webkit-border-radius:3px;<br />
	-moz-border-radius:3px;<br />
	margin:5px 24px 0;<br />
}</p>
<p>input.submit:hover, input.editbutton:hover, #submit:hover {<br />
	background-color:#cfcfcf;<br />
}</p>
<p>label {<br />
	font-family:Georgia, "Times New Roman", Times, serif;<br />
	font-size:18px;<br />
	color:#9f9f9f;<br />
	padding:5px 24px 0;<br />
}</p>
<p>/*---Index Page---*/</p>
<p>h1 {<br />
	font-family:Georgia, "Times New Roman", Times, serif;<br />
	font-size: 24px;<br />
	color:#777;<br />
	padding:5px;<br />
}</p>
<p>ul.pagelist1 {<br />
	background-color: #cfcfcf;<br />
	-moz-border-radius: 5px;<br />
	-webkit-border-radius: 5px;<br />
	padding:10px;<br />
	width:680px;<br />
}</p>
<p>li.pageoption {<br />
	background-color: #afafaf;<br />
	width:640px;<br />
	padding:10px;<br />
	-moz-border-radius: 5px;<br />
	-webkit-border-radius:5px;<br />
	display: block;<br />
	margin:10px;<br />
}</p>
<p>li.pageoption:hover {<br />
	background-color: #efefef;<br />
}</p>
<p>a.pagelink {<br />
	font-family:Calibri, Arial, Helvetica, sans-serif;<br />
	color:#000;<br />
	text-decoration: none;<br />
}</p>
<p>span.floatright {<br />
	float: right;<br />
}</p>
<p>/*---Edit Page---*/</p>
<p>textarea.edit {<br />
	width:450px;<br />
	margin:5px 24px 0px 24px;<br />
	border:1px solid #cfcfcf;<br />
	-moz-border-radius:5px;<br />
	-webkit-border-radius:5px;<br />
	height:200px;<br />
	font-family: Calibri, Arial, Helvetica, sans-serif;<br />
	color: #000;<br />
	padding:5px;<br />
	font-size: 20px;<br />
}</p>
<p>input.edit {<br />
	width:450px;<br />
	margin:5px 24px 0px 24px;<br />
	border:1px solid #cfcfcf;<br />
	-moz-border-radius:5px;<br />
	-webkit-border-radius:5px;<br />
	height:35px;<br />
	font-family: Calibri, Arial, Helvetica, sans-serif;<br />
	color: #000;<br />
	padding:5px;<br />
	font-size: 20px;<br />
}</p>
<p>/*---Add New Page, Page---*/</p>
<p>#title {<br />
	width:450px;<br />
	margin:5px 24px 0px 24px;<br />
	border:1px solid #cfcfcf;<br />
	-moz-border-radius:5px;<br />
	-webkit-border-radius:5px;<br />
	height:35px;<br />
	font-family: Georgia, "Times New Roman", Times, serif;<br />
	color: #7f7f7f;<br />
	padding:5px;<br />
	font-size: 20px;<br />
}</p>
<p>#url {<br />
	width:170px;<br />
	margin:5px 0px 0px 0px;<br />
	border:1px solid #cfcfcf;<br />
	-moz-border-radius:5px;<br />
	-webkit-border-radius:5px;<br />
	height:35px;<br />
	font-family: Georgia, "Times New Roman", Times, serif;<br />
	color: #7f7f7f;<br />
	padding:5px;<br />
	font-size: 20px;<br />
}</p>
<p>.infomsg {<br />
	display:block;<br />
	background-color:#fff9d7;<br />
	border-width:1px;<br />
	border-color:#e2c822;<br />
	border-style:solid;<br />
	width:445px;<br />
	color:#000;<br />
	font-family:Calibri, Arial, Helvetica, sans-serif;<br />
	font-size:16px;<br />
	text-align:center;<br />
	margin:10px 0px 10px 20px;<br />
	padding:10px;<br />
}</p>
<p>/*---Signup Page---*/</p>
<p>#signupform input {<br />
	width:450px;<br />
	margin:5px 24px 0px 24px;<br />
	border:1px solid #cfcfcf;<br />
	-moz-border-radius:5px;<br />
	-webkit-border-radius:5px;<br />
	height:35px;<br />
	font-family: Georgia, "Times New Roman", Times, serif;<br />
	color: #7f7f7f;<br />
	padding:5px;<br />
	font-size: 20px;<br />
}
</p></div>
<p>Now that is quite a lot of code indeed. But really all it does is make our site more workable. Next I would like to talk about extending the functionality of this CMS. I am going to be using real world examples that I recently used on a client's site, so they should be relevant.</p>
<p>So let's take a step back and look at what we have done. We have created a CMS that allows users to signup and login. From the index page they can create new pages and edit existing pages. The changes that they make in a friendly noob-based environment as I like to call it, will be reflected on the live site. That's quite impressive. Visit any one of the site's pages and you are viewing dynamically generated content. This approach is excellent for us web designers, and the only people that love it more than we do, are the clients. Think about it, if you had a site that was supposed to display the latest technology news and you had to go through your web designer first, it would take some time, therefore making your site out of date quickly. Clients love the idea that they can easily edit and update the information on their site without needing to know anything technical. But wait! Before you go pitching to a client with the CMS above there are a few things you should know. If I bought a website with the CMS above I wouldn't be happy. Why not? Well because there are some things missing. Firstly think about the navigation in the backend. There is none! Moreover how do I add images and links? Remember I know nothing about code. To overcome such difficulties that you will inevitably face I recommend you work on their solutions.</p>
<p>When the client came to me and asked for a myriad of features I had a number of problems to sort out. Many of the problems I faced were solved using solutions listed in this series of tutorials. But instead of providing you with the exact solutions to some of them, I urge you to try to figure them out for yourself. Let's look at the image and link adding feature for example. Realistically it can be tackled head on very easily. What would we need? Well, we want the URL of the page to be linked to, and what the user wants to display as the link text. Then all we have to do is generate a bit of HTML with that information in it. Go ahead and try and combat the problem. You will need a form and a tip - I used some really simple Javascript to generate the HTML. Once the code is generated the user need only paste it into the editing field of the page.</p>
<p>Now let's discuss an advanced security feature. You could use this for a client who was particularly wary of being hacked. Look at the code below.</p>
<div class="code">
       $ip=$_SERVER['REMOTE_ADDR'];</p>
<p>               if(!filter_var($ip, FILTER_VALIDATE_IP))<br />
               {<br />
                       throw new InvalidArgumentException("IP is not valid");<br />
               }</p>
<p>               $response=@file_get_contents('http://www.netip.de/search?query='.$ip);<br />
               if (empty($response))<br />
               {<br />
                       throw new InvalidArgumentException("Error contacting Geo-IP-Server");<br />
               }</p>
<p>               $patterns=array();<br />
               $patterns["domain"] = '#Domain: (.*?)&nbsp;#i';<br />
               $patterns["country"] = '#Country: (.*?)&nbsp;#i';<br />
               $patterns["town"] = '#City: (.*?)<br#i';</p>
<p>               $ipInfo=array();</p>
<p>               foreach ($patterns as $key => $pattern)<br />
               {<br />
                       $ipInfo[$key] = preg_match($pattern,$response,$value) &#038;& !empty($value[1]) ? $value[1] : 'not found';<br />
               }</p>
</div>
<p>The code above gets the user's IP address and then uses a free service to retrieve some information. With that information you could compare the country the user is in to the country they usually login from, and if it differs ask them to provide some more information like an extra security code. Clients will be impressed by this sort of stuff, especially if you can make it work well and make it slick.</p>
<p>Lastly I want to talk about user details. This is a really simple subject area, but it is important. If your user signs up and accidentally spells their name wrong then they won't be able to change it, and that really isn't great. If you think about it to implement a profile editing page would be easy. I'll take you through the theory. You would create a new page and add a form not dissimilar to that of the form used to signup. You would then populate the form with <span class="code-small">$_SESSION</span> variables, leaving the user free to edit them. When the submit button is clicked the database is updated and everything is great!</p>
<p>The things listed above may seem simple, but adding polish to a site's CMS is a good idea and a requirement if you are working on one for a client. In the end my client's CMS turned into a very powerful one, it looks lovely with a load of fancy icons and animations. I was able to work with the client and help them to achieve their goals because at the end of the day the CMS that I built - much like the one above - was extensible and flexible. </p>
<p>I hope you have enjoyed this series, I have worked hard to iron out any kinks in the code, but if you find one leave a comment below - I'll be most grateful. The same goes for any issues you have with the tutorial. If you have any questions at all, feel free to ask away below. </p>
<p>Now that's over, time for a coffee me thinks!</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://tomsbigbox.com/building-a-cms-part-3/&amp;title=Building+a+CMS+Part+3" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Building+a+CMS+Part+3+-+http://bit.ly/8ZvnXv&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://tomsbigbox.com/building-a-cms-part-3/&amp;t=Building+a+CMS+Part+3" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://tomsbigbox.com/building-a-cms-part-3/&amp;title=Building+a+CMS+Part+3" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://tomsbigbox.com/building-a-cms-part-3/&amp;title=Building+a+CMS+Part+3" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://tomsbigbox.com/building-a-cms-part-3/&amp;title=Building+a+CMS+Part+3" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://tomsbigbox.com/building-a-cms-part-3/&amp;title=Building+a+CMS+Part+3" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-comfeed">
			<a href="http://tomsbigbox.com/building-a-cms-part-3/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://tomsbigbox.com/building-a-cms-part-3/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Building a CMS Part 2</title>
		<link>http://tomsbigbox.com/building-a-cms-part-2/</link>
		<comments>http://tomsbigbox.com/building-a-cms-part-2/#comments</comments>
		<pubDate>Sat, 09 Jan 2010 11:27:36 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Building a CMS]]></category>
		<category><![CDATA[CMS]]></category>
		<category><![CDATA[Content]]></category>
		<category><![CDATA[Content Management System]]></category>
		<category><![CDATA[How to]]></category>

		<guid isPermaLink="false">http://tomsbigbox.com/?p=507</guid>
		<description><![CDATA[So if you're following this set of tutorials you should now have your very own login system that allows users to sign up and login. However these tutorials aren't about building a user base, so we need to add the functionality of a CMS. At the moment if a user logs in they will be...</p><a class="read-more" href="http://tomsbigbox.com/building-a-cms-part-2/">Continue Reading...</a>]]></description>
			<content:encoded><![CDATA[<p>So if you're following this set of tutorials you should now have your very own login system that allows users to sign up and login. However these tutorials aren't about building a user base, so we need to add the functionality of a CMS. At the moment if a user logs in they will be presented with a blank screen which isn't very useful at all, so now we will create a rather friendly member's area. But before we do, let's assess what we need in the member's area. Well to begin with we want to be presented with a list of editable pages right? So we will need to produce a list of all of these pages from the database. With this in mind, create a file called <span class="code-small">index.php</span>, not forgetting to add the normal document base structure, and then copy the code below into the file.</p>
<div class="code">&lt;?php require("connect.php");  session_start(); ?&gt;<br />
&lt;h1&gt;Member's Area&lt;/h1&gt;<br />
&lt;ul&gt;<br />
&lt;?php<br />
$sql = mysql_query('SELECT * FROM content');<br />
$pages = mysql_num_rows($sql);<br />
$pagesedit = $pages + 1;<br />
$pageselector = 1;<br />
echo '&lt;a href="http://mywebsite.com/addpage.php"&gt;&lt;li&gt;[+] &lt;strong&gt;Add new page&lt;/strong&gt;&lt;/li&gt;&lt;/a&gt;';<br />
while($pages &gt;= $pageselector){<br />
$contentresult = mysql_query("SELECT * FROM content WHERE id = '".$pageselector."'");<br />
$contentrow = mysql_fetch_array($contentresult);<br />
echo '&lt;a href="http://mywebsite/edit.php?page='. $contentrow['page'] .'"&gt;&lt;li&gt;&lt;strong&gt;'. $contentrow['title']   .'&lt;/strong&gt;&lt;span&gt;Last updated on &lt;strong&gt;'. $contentrow['lastupdate'] .'&lt;/strong&gt; by &lt;strong&gt;'. $contentrow['updater']   .'&lt;/strong&gt;&lt;/span&gt;&lt;/li&gt;&lt;/a&gt;';<br />
$pageselector++;<br />
}<br />
?&gt;<br />
&lt;/ul&gt;</div>
<p>Now the middle bit with all the PHP might seem a little complex, but if we break it down it makes more sense. At the top we simply select all the records from our "content" table that we created earlier, and then count how many we have with the function <span class="code-small">mysql_num_rows()</span>. Then we echo out a link to a page that we will later use to create new pages. After that we create a while loop which loops through the content of the table, and for each record, it echos a link to another page we haven't yet created, that we will be able to edit pages of the site with. Now I know it might seem like a lot to take in, but if you take a moment to look at it logically it makes perfect sense!</p>
<p>Now if you were to visit that page right now you would likely get an error because we don't have anything in our table yet. To stop this from happening we are going to create a new file called <span class="code-small">addpage.php</span> which we will use to create pages. Now I warn you there is a lot of code for this page, so much in fact I've given it it's own special type of container! But don't worry because all will be explained!</p>
<div class="code">&lt;?php<br />
require("connect.php");<br />
session_start();<br />
mysql_connect($dbhost, $dbuser, $dbpass) or die('&gt;&gt;Error connecting to Database&lt;br /&gt;');<br />
mysql_select_db($dbname) or die('&gt;&gt;Unable to select Database');<br />
if(!empty($_POST['title'])){<br />
$filename = str_replace(" ", "-",$_POST['url']);<br />
$filename1 =  "root/folder/" . $filename . ".php"; //EDIT THIS LINE!<br />
$filehandle = fopen($filename1, 'w') or die("Can't open file");<br />
$blankPage = '<br />
&lt;?php<br />
$currentPage = "'.$_POST[url].'";<br />
require("connect.php");<br />
$result = mysql_query("SELECT * FROM content WHERE page = \'$currentPage\'");<br />
$row = mysql_fetch_array($result);<br />
?&gt;<br />
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;<br />
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;&lt;?php echo $row[title] ?&gt;&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;?php echo "&lt;h1&gt;" . $row[title] . "&lt;/h1&gt;" ?&gt;<br />
&lt;?php echo "&lt;p class=\"text\"&gt;" . nl2br($row[text]) . "&lt;/p&gt;"; ?&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;<br />
';<br />
fwrite($filehandle, $blankPage);<br />
fclose($filehandle);<br />
mysql_query("INSERT INTO content(page,title,text,lastupdate,updater) VALUES('$filename','".ucwords($_POST[title])."','TBC','".gmdate('d-m-y')."','$_SESSION[firstname] $_SESSION[lastname]')") or die(mysql_error());<br />
$file = "http://mywebsite.com/".$filename.".php";<br />
}<br />
?&gt;<br />
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;<br />
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;Add New Page&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;?php if(!empty($file)){ echo '&lt;span class="infomsg"&gt;&lt;a&gt;Here is the URL for the new page: '. $file .'&lt;br /&gt;To edit &lt;a href="http://mywebsite.com/edit.php?page='.str_replace("/", "",$filename).'"&gt;click here&lt;/a&gt;&lt;/a&gt;&lt;/span&gt;'; }; ?&gt;<br />
&lt;form id="editor" action="addpage.php"  method="post"&gt;<br />
&lt;label id="top" for="title"&gt;Page Title: &lt;/label&gt;&lt;br /&gt;<br />
&lt;input type="text" id="title" name="title" value="New Page" /&gt;<br />
&lt;br /&gt;<br />
&lt;br/&gt;<br />
&lt;label for="text"&gt;Path to Page: &lt;/label&gt;&lt;br /&gt;&lt;br /&gt;<br />
&lt;label for="text"&gt;http://mywebsite.com/&lt;/label&gt;<br />
&lt;input type="text" id="url" value="new-page" name="url" /&gt;&lt;label for="text"&gt;.php&lt;/label&gt;<br />
&lt;br /&gt;<br />
&lt;br /&gt;<br />
&lt;input id="submit" type="submit" value="Add Page" /&gt;<br />
&lt;br /&gt;<br />
&lt;br /&gt;<br />
&lt;/form&gt;<br />
&lt;/div&gt;<br />
&lt;/div&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;
</div>
<p>Now I know that's a lot of code, but bare with me here, I'm about to go through it <strong>all</strong> with you now (again if you get it, feel free to skip). So at the top of the code we make our usual connection to our database and then there's a load of code. For the time being skip the rest of the PHP code until you get down to the actual page entitled "Add new Page". On this page we first have an if statement to check if a variable is empty, and if not, then we echo a message to the user so that they know where their newly created file is. Then we have a form into which users can enter a title and a URL for a new page, but what you may have noticed is that this page doesn't post the information to another, but instead to itself - and that's why it is so long. So once the form is submitted we can jump back to the top of the page to examine the rest of the code. After the connection we can see an "if" statement which checks to see if the form has been submitted, if it has, then it executes a large block of code.</p>
<p>At the top of this block of code we have 4 variables. Our <span class="code-small">$filename</span> variable takes the URL that the user has supplied and replaces any spaces with a hyphen - this is because we don't want page URL with spaces in them - one of my pet hates! After this we create an actual URL for the file, now it is important that you get this part right, you cannot have a URL such as "http://yoursite.com/", instead you have to have your folder root, so if your site is inside a folder such as "public_html" then you will put "public_html/", it's basically the furthest back you can go on your server in terms of folders - just keep going up until you can't go up no more! Next we create a file handle which inserts the URL into a more useful variable to be used later on. Next we have our blank page which is a generic skeleton to be used for every page of the site. Notice that the only variable that changes is the that of <span class="code-small">$currentPage</span>, this is the variable used to communicate with the database, so it is imperative that it is correct.</p>
<p>Lastly we have the <span class="code-small">fwrite()</span> function which attempts to write to a file using the URL we supply, and the information we give it with <span class="code-small">$blankPage</span>. If the function cannot find the file specified, then it simply creates it, which is very useful indeed! Then, we close the function and insert all the information into the database, leaving the text field with simply "TBC" or to be confirmed. If you want to change the default text feel free to do so. And finally we set the value of the <span class="code-small">$file</span> variable so that the user can be presented with the URL of the file they just created!</p>
<p>Phew, that was a lot of code!</p>
<p>Now I must warn you that if the page already exists this page wil overwrite it, so be careful when naming pages. But if everything is working visit the page and create a brand new page, call it anything you want and submit the form! After you have done this, go to the index page and you should see the page title, when it was last updated, and who it was last updated by in the list! Great stuff, but we're not done yet, because if you click the link you are going to end up with an error because we haven't yet created the editing page - don't worry it's really simple, so lets do it now. Go ahead and create a new page called <span class="code-small">edit.php</span> and paste the following code into it.</p>
<div class="code">&lt;?php session_start(); $currentPage = $_GET['page'];<br />
$result = mysql_query("SELECT * FROM content WHERE page = '".$currentPage."'");<br />
$row = mysql_fetch_array($result);<br />
if($row['title'] == ""){ echo "&lt;script type=\"text/javascript\"&gt;window.location = \"http://yoursite.com/\"&lt;/script&gt;"; die(); };</p>
<p>?&gt;<br />
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;<br />
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;Editing: &lt;?php echo $row['title']; ?&gt;&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;h1&gt;Editing Page: &lt;?php echo $row['title'] ?&gt;&lt;/h1&gt;<br />
&lt;form id="editor" action="process.php?page=&lt;?php echo $currentPage ?&gt;"  method="post"&gt;<br />
&lt;?php if($_GET['status'] == "success"){ echo "&lt;div id=\"infomsg\"&gt;&lt;p&gt;Page successfully updated!&lt;/p&gt;&lt;/div&gt;"; } ?&gt;<br />
&lt;label id="top" for="title"&gt;Title: &lt;/label&gt;&lt;br /&gt;<br />
&lt;input type="text" id="title" name="title" value="&lt;?php echo $row['title'] ?&gt;" /&gt;<br />
&lt;br /&gt;<br />
&lt;br/&gt;<br />
&lt;label for="text"&gt;Text: &lt;/label&gt;&lt;br /&gt;<br />
&lt;textarea name="text" id="text"&gt;&lt;?php echo $row[text] ?&gt;&lt;/textarea&gt;<br />
&lt;br /&gt;<br />
&lt;br /&gt;<br />
&lt;input id="submit" type="submit" value="Submit Changes" /&gt;<br />
&lt;br /&gt;<br />
&lt;br /&gt;<br />
&lt;/form&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</p>
</div>
<p>So although it may not look it, this page is really simple. At the top we connect to our database and fetch the page supplied by the URL. We then grab all the page's information from the database and fill up some variables with that information. You may have noticed that we use Javascript this time to redirect the user if they aren't logged in, why?  Well because variety is the spice of a CMS! We then fill a text box and a text area with the values from our database. Now again this form submits to another page, and so we are going to have to create it. So create a new page named <span class="code-small">process.php</span> and paste the following code into it.</p>
<div class="code">&lt;?php $currentPage = $_GET['page']; require("connect.php"); session_start(); ?&gt;<br />
&lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt;<br />
&lt;html xmlns="http://www.w3.org/1999/xhtml"&gt;<br />
&lt;head&gt;<br />
&lt;title&gt;Editing&lt;/title&gt;<br />
&lt;/head&gt;<br />
&lt;body&gt;<br />
&lt;?php<br />
$title = $_POST['title'];<br />
$text = $_POST['text'];<br />
if($title == "" || $text == ""){ echo "&lt;h1&gt;No input Recieved!&lt;/h1&gt;"; die(); }<br />
mysql_query("UPDATE content SET title= '$title' WHERE page='$currentPage'") or die('Unable to change Title');<br />
mysql_query("UPDATE content SET text='$text' WHERE page='$currentPage'") or die('Unable to change Text');<br />
mysql_query("UPDATE content SET lastupdate='". date("d-m-y")."' WHERE page='$currentPage'") or die('Unable to insert Date');<br />
mysql_query("UPDATE content SET updater='$_SESSION[firstname] $_SESSION[lastname]' WHERE page='$currentPage'") or die('Unable to insert Name');<br />
?&gt;<br />
&lt;script type="text/javascript"&gt;<br />
document.location = "http://yoursite.com/edit.php?page=&lt;?php echo $currentPage ?&gt;&amp;status=success";<br />
&lt;/script&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;</div>
<p>So the above is a really simple page, basically all it does is take what the user enters into the form and updates the record in the database. Notice the helpful error messages as well; using relevant error messages can help you to track down an error faster, so I defiantly recommend it. Now if the page exists, and <span class="code-small">process.php</span> is able to update it, it should redirect users back to the editing page with a success message. If it has worked you should now see your changes reflected on the page that you were editing.</p>
<p>Now to finish off this part of the tutorial we are going to create the simpelest page ever that will allow you to destroy the session variables and ultimatly log out. We'll call it <span class="code-small">logout.php</span> (unexpected eh?). Create the file and paste in the code below.</p>
<div class="code">&lt;?php session_start(); session_destroy() ?&gt;</div>
<p>And that's it for this tutorial, in the next part we'll look at how we can allow the user to add images and links, as well as the all important styling so that it looks pretty! Stay tuned!</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://tomsbigbox.com/building-a-cms-part-2/&amp;title=Building+a+CMS+Part+2" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Building+a+CMS+Part+2+-+http://bit.ly/6ZHSE8&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://tomsbigbox.com/building-a-cms-part-2/&amp;t=Building+a+CMS+Part+2" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://tomsbigbox.com/building-a-cms-part-2/&amp;title=Building+a+CMS+Part+2" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://tomsbigbox.com/building-a-cms-part-2/&amp;title=Building+a+CMS+Part+2" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://tomsbigbox.com/building-a-cms-part-2/&amp;title=Building+a+CMS+Part+2" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://tomsbigbox.com/building-a-cms-part-2/&amp;title=Building+a+CMS+Part+2" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-comfeed">
			<a href="http://tomsbigbox.com/building-a-cms-part-2/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://tomsbigbox.com/building-a-cms-part-2/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building a CMS Part 1</title>
		<link>http://tomsbigbox.com/building-a-cms-part-1/</link>
		<comments>http://tomsbigbox.com/building-a-cms-part-1/#comments</comments>
		<pubDate>Wed, 06 Jan 2010 17:34:22 +0000</pubDate>
		<dc:creator>Tom</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Building a CMS]]></category>
		<category><![CDATA[CMS]]></category>
		<category><![CDATA[Content]]></category>
		<category><![CDATA[Content Management System]]></category>
		<category><![CDATA[How to]]></category>

		<guid isPermaLink="false">http://tomsbigbox.com/?p=441</guid>
		<description><![CDATA[A content management system (or CMS) is the best way (I think) to maintain a site. This site, like many others uses WordPress, a blogging CMS. There are many options available to us web designers out there, but I think creating your own CMS is a really good way of learning a lot of new techniques....</p><a class="read-more" href="http://tomsbigbox.com/building-a-cms-part-1/">Continue Reading...</a>]]></description>
			<content:encoded><![CDATA[<p>A content management system (or CMS) is the best way (I think) to maintain a site. This site, like many others uses WordPress, a blogging CMS. There are many options available to us web designers out there, but I think creating your own CMS is a really good way of learning a lot of new techniques. So let me tell you what you will learn in this series. We will be starting from scratch, laying out our plans for what we want to achieve with our CMS and how we are going to go about doing it. We will then set up a database for our CMS and make it accessible to a bunch of PHP based pages. We will then create a site that relies on the CMS to display content, and at the same time create the back-end of the CMS where we will be able to add pages, change the content, and upload images. At the end of these tutorials you should have a good idea of what it takes to create a CMS and hopefully appreciate even more, the work of those who maintain services such as WordPress.</p>
<h3 class="subtitle">What do I need to know?</h3>
<ol>
<li>General Understanding of HTML / CSS</li>
<li>Basic knowledge of PHP and MySQL syntax</li>
<li>Access to a server with a MySQL Database &amp; PHPMyAdmin</li>
</ol>
<p>Once you have all of the above we should be able to get going.</p>
<h3 class="subtitle">Let's do this thing!</h3>
<p><!-- INSERT DATA FLOW DIAGRAM HERE! --></p>
<p>Our CMS is going to be rather simple, but will allow for user sign-up, content editing and a few other little features. The backbone of our CMS is going to be our database, and we are going to use PHPMyAdmin to create one. If you have a hosting plan you will need to find out if you have PHPMyAdmin installed on your server - most hosts should offer the service - including <a href="http://www.mediatemple.net/go/order/?refdom=stannschurch.org.uk">Media Temple</a> (shameless plug <img src='http://tomsbigbox.com/core/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' /> ). Many services require you to create the database outside of PHPMyAdmin, so find out how to do so and create one called "main" (all lower-case). Then head over into PHPMyAdmin and select the database on the left. Click "Create New Table" with 6 fields named "content". These fields will be id, page, title, text, lastupdate, updater - exactly like that. Make them all VARCHARs with appropriate lengths.</p>
<p>The next table is to be even more in-depth, it's the users table. Go ahead and create a new table named "users" with 13 fields. They will be - id, username, password, email,  hash, active, firstName, lastName, firstLogin, lastLogin, secretQuestion, secretAnswer, photo. Don't worry - all will be explained with a couple of handy lists below.</p>
<ul>
<li><strong>id</strong> - for internal referencing, which is a necessity</li>
<li><strong>username</strong> - what users will use to login</li>
<li><strong>password</strong> - what users will use to authenticate themselves</li>
<li><strong>email</strong> - for communication with the user</li>
<li><strong>hash</strong> - to validate the user's email address</li>
<li><strong>active</strong> - to make sure the user has activated their email</li>
<li><strong>firstName</strong> - so we know what to call them informally</li>
<li><strong>lastName</strong> - so we know who we have on record</li>
<li><strong>firstLogin</strong> - just a nice little piece of information</li>
<li><strong>lastLogin</strong> - to find out if they are using their account</li>
<li><strong>secretQuestion</strong> - if they forget their password</li>
<li><strong>secretAnswer</strong> - the answer to the one above</li>
<li><strong>photo</strong> - so we can see them!</li>
</ul>
<p>Right, now we know what is what in the land of databases, we need to connect to it. So make sure you have the required credentials to access the database. These are your database user name, your database password and your database name (and very rarely your hostname). Once you've got all of that good stuff head over into your code editor of choice and let the mayhem begin!</p>
<h3 class="subtitle">Our Connect File</h3>
<p>To have a CMS work, we need to be able to communicate with our database, and to do so we will first have to connect to it. And if we want multiple pages of our CMS to be able to communicate with our database we are going to need to create a separate file (the alternative to this, is copying and pasting the code onto every page - but that's a lot of work, and if we happen to change any of the details, a whole lot more). The file itself is really simple, below is the code for the file. Create a new file called <span class="code-small">connect.php</span> and paste in the code below.</p>
<div class="code">&lt;?php<br />
$dbhost = 'localhost'; // You shouldn't need to change this<br />
$dbname = 'db82542_main'; // The name of your database<br />
$dbuser = 'userName'; // Your username<br />
$dbpass = 'password'; // Your password</p>
<p>mysql_connect($dbhost, $dbuser, $dbpass) or die("Error connecting to Database");<br />
mysql_select_db($dbname) or die("Unable to select Database");<br />
?&gt;</p>
</div>
<p>We will now be able to use the PHP function of require to include this in all the files we create. Before we continue, make sure that the file can successfully connect to the database, do this by simply visiting the page. If successful you should see a blank page, if not, you should see an error message. Make sure it works before continuing on.</p>
<h3 class="subtitle">User Authentication</h3>
<p>We want to be able to edit the content of our site right? But what we don't want, is everybody being able to edit the content, or else we could be subject to spammers, hackers, and all kinds of other nasty stuff. So we are going to have to create a user system. You might think this is a long and complex process, but really it's quite simple. We will need to create a login page, followed by one to authenticate the credentials, followed by a "Members Only" area where we will eventually place the main body of our CMS. So let's get started.</p>
<p>Create a new page named <span class="code-small">login.php</span> and insert a basic page structure. Now we need to create a form that users can use to login. The form is going to be very simple with inputs for username, password, and then a submit button.</p>
<div class="code">
<p>&lt;form id="login" action="userauth.php" method="post"&gt;<br />
&lt;label for="username"&gt;Username: &lt;/label&gt;&lt;br /&gt;<br />
&lt;input class="textbox" type="text" name="username" value="" /&gt;<br />
&lt;br /&gt;<br />
&lt;br /&gt;<br />
&lt;label for="password"&gt;Password: &lt;/label&gt;&lt;br /&gt;<br />
&lt;input class="textbox" type="password" name="password" value="" /&gt;<br />
&lt;br /&gt;<br />
&lt;br /&gt;<br />
&lt;input type="submit" class="submit" value="Submit" /&gt;<br />
&lt;/form&gt;</p>
</div>
<p>As you can see from the code above we are going to be needing another file named <span class="code-small">userauth.php</span>, this will be the file that will compare what the user inputs to what we have in our database, so go ahead and create it. So what do we have at the moment? Well we have a login form that posts the information to another page to validate it. Right now if you fill in the form you will be taken to a black page, and that's no good is it?! So let's create the authentication page. Hopefully you've already created the file, open it up and insert the following code (a basic page structure fo this page isn't required).</p>
<div class="code">&lt;?php<br />
require("connect.php");<br />
session_start();<br />
$iusername= mysql_real_escape_string($_POST['username']);<br />
$ipassword = mysql_real_escape_string(md5($_POST['password']));<br />
if(empty($iusername) || empty($ipassword)){ echo "&lt;meta http-equiv=\"refresh\" content=\"0;url=http://yoursite.com/login.php?m=1\"&gt;"; }</p>
<p>$result = mysql_query("SELECT * FROM users WHERE username = '".$iusername."'");<br />
$row = mysql_fetch_array($result);<br />
$dbusername = $row['username'];<br />
$dbpassword = $row['password'];</p>
<p>if($iusername == $dbusername &amp;&amp; $ipassword == $dbpassword) {<br />
$_SESSION['loggedin'] = '1';<br />
$_SESSION['username'] = $iusername;<br />
$_SESSION['password'] = $ipassword;<br />
$_SESSION['firstname'] = $row['firstName'];<br />
$_SESSION['lastname'] = $row['lastName'];<br />
$_SESSION['userid'] = $row['id'];<br />
$_SESSION['photo'] = $row['photo'];<br />
$_SESSION['usertype'] = $row['userType'];<br />
$_SESSION['email'] = $row['email'];<br />
$_SESSION['firstlogin'] = $row['firstLogin'];<br />
$_SESSION['lastlogin'] = $row['lastLogin'];</p>
<p>if(empty($dbusername)){<br />
echo "&lt;meta http-equiv=\"refresh\" content=\"0;url=http://yoursite.com/login.php?m=1\"&gt;"; session_destroy(); echo '1';<br />
};</p>
<p>if($row['active']==0){<br />
echo "&lt;meta http-equiv=\"refresh\" content=\"0;url=http://yoursite.com/login.php?status=noneactive\"&gt;"; session_destroy();<br />
};</p>
<p>if($row['firstLogin'] == 0){<br />
mysql_query("UPDATE users SET firstLogin='$date' WHERE username='$iusername'");<br />
};</p>
<p>mysql_query("UPDATE users SET lastLogin='$date' WHERE username='$iusername'");</p>
<p>echo "&lt;meta http-equiv=\"refresh\" content=\"0;url=http://yoursite.com/index.php\"&gt;";<br />
}<br />
else {<br />
echo "&lt;meta http-equiv=\"refresh\" content=\"0;url=http://yoursite.com/login.php?m=1\"&gt;";<br />
session_destroy();<br />
}<br />
?&gt;</p>
</div>
<p>Right so let me explain the code above (if you get what it's doing feel free to skip this explanation) So at the top of the code we can see that we require our <span class="code-small">connect.php</span> file which allows us to connect to our database (note: if connect.php doesn't exist the page will stop working!) then we start a session, this will be for a set of variables we want other pages to be able to access. After this we assign values to 2 variables and use the PHP function of <span class="code-small">mysql_real_escape_string()</span> which protects us from MySQL Injection which is a very nasty thing indeed! Then we fetch some results from our database and set up two more variables so that we can compare them. Then we use an "if" statement to compare the values, and if the user has entered the correct credentials we do a a few things. Firstly we set up a number of session variables to be accessed by all other pages of the site. The we check to see if the username the user actually inputted exists - if not they are redirected to the login page and we destroy the session. Then we check to see if the user has activated their email address, if not they will be redirected to the login page. If they get through both "ifs" the page then checks to see if this is the first time the user has logged in, and if so, sets the value of the record's "firstLogin" field to today's date and finally redirects the user to the index page for the member's only area. And if the username and password don't match they promptly redirected o the login page while the session is destroyed. Phew! Quite a lot to take in there, but providing you've made it this far we should be getting somewhere.</p>
<p>Now the more astute among you may have realised that we haven't actually got any records in our database, meaning that even if we wanted to, we wouldn't be able to login. So let's get to creating that signup form.</p>
<h3 class="subtitle">The Signup Form</h3>
<p>We are going to create a really simple form so that users can signup, you should be aware that more time should be spent perfecting the form such as adding validation to certain fields and maybe some sort of terms of service agreement, but for now let's just go ahead and create a simple signup form.</p>
<div class="code">&lt;form id="signupform" action="addaccount.php" method="post"&gt;<br />
&lt;label for="username"&gt;Username: &lt;/label&gt;<br />
&lt;input name="username" type="text" value="joebloggs" /&gt;&lt;br /&gt;<br />
&lt;label for="password"&gt;Password: &lt;/label&gt;<br />
&lt;input name="password" type="password" value="password" /&gt;&lt;br /&gt;<br />
&lt;label for="email"&gt;Email Address: &lt;/label&gt;<br />
&lt;input name="email" type="text" value="joe@bloggs.com" /&gt;&lt;br /&gt;<br />
&lt;label for="firstname"&gt;First Name: &lt;/label&gt;<br />
&lt;input name="firstname" type="text" value="Joseph" /&gt;&lt;br /&gt;<br />
&lt;label for="secondname"&gt;Second Name: &lt;/label&gt;<br />
&lt;input name="secondname" type="text" value="Bloggs" /&gt;&lt;br /&gt;<br />
&lt;label for="secretquestion"&gt;Write a secret Question: &lt;/label&gt;<br />
&lt;input name="secretquestion" type="text" value="Favourite Place" /&gt;&lt;br /&gt;<br />
&lt;label for="secretanswer"&gt;Answer to secret Question: &lt;/label&gt;<br />
&lt;input name="secretanswer" type="text" value="New York" /&gt;&lt;br /&gt;<br />
&lt;input class="submit" type="submit" /&gt;<br />
&lt;/form&gt;</p>
</div>
<p>So the above code simple gives us a form whereby users can enter their info and submit it to our system. Again this form posts the information to another file called <span class="code-small">addaccount.php</span>, and we need to create this file. so go ahead and create <span class="code-small">addaccount.php</span> and paste in the following code.</p>
<div class="code">&lt;?php<br />
require("connect.php");<br />
$username = mysql_real_escape_string($_POST['username']);<br />
$password = mysql_real_escape_string(md5($_POST['password']));<br />
$email = mysql_real_escape_string($_POST['email']);<br />
$firstname = mysql_real_escape_string($_POST['firstname']);<br />
$secondname = mysql_real_escape_string($_POST['secondname']);<br />
$secretquestion = mysql_real_escape_string($_POST['secretquestion']);<br />
$secretanswer = mysql_real_escape_string($_POST['secretanswer']);<br />
$hash = rand(1000000, 99999999999);</p>
<p>mysql_query("INSERT INTO users(id, username ,password, email, hash, active, firstName, lastName, firstLogin, lastLogin, secretQuestion, secretAnswer, photo) VALUES(NULL,'$username', '$password', '$email', '$hash', '0', '$firstname', '$secondname', '0', '0', '$secretquestion', '$secretanswer', '0')") or die('Unable to insert Data' . mysql_error());</p>
<p>$message = '<br />
&lt;html&gt;<br />
&lt;body bgcolor="#FFF"&gt;<br />
&lt;p style="font-family:Calibri, Arial, Helvetica, sans-serif; color:#000; font-size:16px;"&gt;Hi ' . $firstname . ', You have just signed up for your account on my website. To activate the account please click, or copy and paste into a browser, the link below.&lt;/p&gt;&lt;br /&gt;&lt;br /&gt;<br />
&lt;p&gt;http://tomsbigbox.com/examples/cms/activate.php?i='.$hash.'&amp;email='.$email.'&lt;/p&gt;<br />
&lt;/body&gt;<br />
&lt;/html&gt;';</p>
<p>$subject = 'My CMS – Please Activate your Account';<br />
$headers = 'From:My Site!' . "\r\nContent-Type: text/html\n";<br />
mail($email, $subject, $message, $headers);</p>
<p>echo "&lt;meta http-equiv=\"refresh\" content=\"0;url=login.php?m=1\"&gt;";<br />
?&gt;</p>
</div>
<p>Right, there certainly is a lot of code there, allow me to explain (again feel free to skip). So at the top we assign value to a set of variables and use the <span class="code-small">mysql_real_escape_string()</span> function to prevent MySQL injection. The <span class="code-small">$hash</span> variable will be used to validate the email address and uses the <span class="code-small">rand()</span> function to create a random number. Next up is the <span class="code-small">mysql_query()</span> function. Now it may look really complex, but realistically it only looks like that because we are inserting a lot of information. So all it is really is a bit of MySQL code to insert all the values into the database. After we have inserted the appropriate data we move onto validating the user's email address. To do this we need to send a message to their email address, so we'll send them a polite message explaining that they have indeed signed up for an account. The body of the message is within the <span class="code-small">$message</span> variable. It is important to note that HTML email is very different to normal web design, so it's best to keep it simple <img src='http://tomsbigbox.com/core/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . Lastly we set the subject and header variables which will tell the email client to render the HTML and then redirect the user to the login page.</p>
<p>If the code is successful and all is well, the user should receive an email with a link to validate the page. You will need to replace the site URL with your own and create a new page called <span class="code-small">activate.php</span>. Copy and paste the following code into this page.</p>
<div class="code">&lt;?php<br />
require("connect.php");<br />
$hash = $_GET['i'];<br />
$email = $_GET['email'];<br />
$result = mysql_query("SELECT * FROM users WHERE email ='$email'");<br />
$row = mysql_fetch_array($result) or die(mysql_error());<br />
if($row['active'] == 1){<br />
echo "&lt;h1&gt;This account has already been activated!&lt;/h1&gt;";<br />
echo "&lt;meta http-equiv=\"refresh\" content=\"2;url=login.php\"&gt;";<br />
}<br />
else if($row['hash'] == $hash){<br />
mysql_query("UPDATE users SET active = '1' WHERE email = '$email'");<br />
echo "&lt;h1&gt;Account Activated!&lt;/h1&gt;";<br />
echo "&lt;a&gt;You will now need to login&lt;/a&gt;";<br />
echo "&lt;meta http-equiv=\"refresh\" content=\"2;url=login.php\"&gt;";<br />
}<br />
else{<br />
echo "&lt;h1&gt;Invalid verfication&lt;/h1&gt;";<br />
echo "&lt;meta http-equiv=\"refresh\" content=\"2;url=login.php\"&gt;";<br />
}<br />
?&gt;</div>
<p>This page is self explanatory, it checks to see if the account has been activated, if not then it activates it, and if it can't find the email address, or the hash is wrong, then it doesn't activate anything! Also notice the 2 second delay on the redirects, allowing the user to read whatever message is outputted by the system.<br />
And that's it for this first part, in the next tutorial we'll look at the member's area and how we can allow the users to edit the content of a site, as well as organising your files and making the whole thing look nice - stay tuned!</p>


<div class="shr-bookmarks shr-bookmarks-expand shr-bookmarks-center shr-bookmarks-bg-enjoy">
<ul class="socials">
		<li class="shr-digg">
			<a href="http://digg.com/submit?phase=2&amp;url=http://tomsbigbox.com/building-a-cms-part-1/&amp;title=Building+a+CMS+Part+1" rel="nofollow" class="external" title="Digg this!">Digg this!</a>
		</li>
		<li class="shr-twitter">
			<a href="http://twitter.com/home?status=Building+a+CMS+Part+1+-+http://bit.ly/4JcbQt&amp;source=shareaholic" rel="nofollow" class="external" title="Tweet This!">Tweet This!</a>
		</li>
		<li class="shr-facebook">
			<a href="http://www.facebook.com/share.php?v=4&amp;src=bm&amp;u=http://tomsbigbox.com/building-a-cms-part-1/&amp;t=Building+a+CMS+Part+1" rel="nofollow" class="external" title="Share this on Facebook">Share this on Facebook</a>
		</li>
		<li class="shr-delicious">
			<a href="http://delicious.com/post?url=http://tomsbigbox.com/building-a-cms-part-1/&amp;title=Building+a+CMS+Part+1" rel="nofollow" class="external" title="Share this on del.icio.us">Share this on del.icio.us</a>
		</li>
		<li class="shr-stumbleupon">
			<a href="http://www.stumbleupon.com/submit?url=http://tomsbigbox.com/building-a-cms-part-1/&amp;title=Building+a+CMS+Part+1" rel="nofollow" class="external" title="Stumble upon something good? Share it on StumbleUpon">Stumble upon something good? Share it on StumbleUpon</a>
		</li>
		<li class="shr-reddit">
			<a href="http://reddit.com/submit?url=http://tomsbigbox.com/building-a-cms-part-1/&amp;title=Building+a+CMS+Part+1" rel="nofollow" class="external" title="Share this on Reddit">Share this on Reddit</a>
		</li>
		<li class="shr-mixx">
			<a href="http://www.mixx.com/submit?page_url=http://tomsbigbox.com/building-a-cms-part-1/&amp;title=Building+a+CMS+Part+1" rel="nofollow" class="external" title="Share this on Mixx">Share this on Mixx</a>
		</li>
		<li class="shr-comfeed">
			<a href="http://tomsbigbox.com/building-a-cms-part-1/feed" rel="nofollow" class="external" title="Subscribe to the comments for this post?">Subscribe to the comments for this post?</a>
		</li>
</ul>
<div style="clear:both;"></div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://tomsbigbox.com/building-a-cms-part-1/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
	</channel>
</rss>
