How to Create a Web Service: Start to Finish

Read Time: 55 min

There's no two ways about it; putting together a website is hard! You need to know a myriad of programming languages (HTML, PHP, CSS, AJAX, Javascript, MySQL, just to name a few). You also must understand how to combine these tools into one cohesive whole, which is hopefully more than the sum of its parts. These days, we use frameworks, boilerplates and generators, but do you actually - when you get right down to it - know how to build a website from scratch?

This tutorial is meant for those who can write a PHP function, know what jQuery is, can handle their CSS well, but would like some insider info on how a web service is built from scratch. How should I organize my files, where should I keep my functions, how should I start planning the thing, how should I connect to the database, how should I handle AJAX calls and how should I manage my 404 pages are just some of the questions I try to shed some light on here.

Disclaimer

Before we begin, a quick disclaimer: while we are building a functioning service, it does not take huge measures to ensure security and scalability. Basic things like password encryption, .htaccess rules, permission checking are included, but there is much more you can and should do if you want to create something for mass consumption.

The app is so far from perfect, but I thought it would be a refreshing change of pace to show you the "How I get there and plan to continue" process instead of the "Here is something awesome I've developed". Web development is not a one-cycle job where you build something great in step 1 and money rolls in at step 2 (it would be awesome though). You usually iterate through versions of a project, arriving at something unique and great along the way.

I hope to demonstrate the underlying logic of some methods you can use so you can make up your own mind on how to do things. I tried to point out the errors and bad practices (some of which I used) along the way. I encourage you to question as many steps as possible and think of better ways of doing things, maybe even dropping off your own ideas, via the comments.

Introduction

The service we will be creating is called Bonsai Writer, a tool which will convert plain text into HTML. It is aimed mostly at writers but it can also be used as a note stash for more general use.

Personally, I write articles for many magazines, all of which have different styling guidelines. However, when I write, I want to focus on writing not styling. The idea of Bonsai Writer was born, a tool for me to manage my articles easily.

Step 1: Planning our Application

Planning should be the very first thing you do and should take quite a long time. I built Bonsai Writer on a whim with only 20-30 minutes planning and it shows! It works and looks satisfactory, but with a few extra hours it could be much better!

Project Planning

The first step in any project should be the planning of the various stages your project would go through. This can be as elaborate as a Gantt diagram, spanning multiple resources and sub-projects, but it can be as simple as a list and a few words about what you want to do. I suggest planning in proportion to your application. If you are undertaking a six month project, it will do you good to create some documents, spreadsheets and other tools to assist you. For a project as simple as this, however, an ordinary list of goals should suffice.

Wireframing

Wireframing is the process of creating a quick low-fidelity mockup of your website.

As the first step, I I drew two quite basic wireframes on my whiteboard: one depicted the front page, the other the app itself. I highly recommend whiteboards as planning tools. I have a 5x2.5 foot board on my wall, and it helps me tremendously in the planning stages.

Some basic wireframes to point me in the right direction.

As you can see, this is all rather simple, but it gave me a solid point for moving on.

If you are developing an app for wide usage, I suggest spending much more time on wireframing. I have found that the more you wireframe, the faster you will build your website. So even though you are not coding, you are still on your way. A good wireframe could contain more views, indication of what happens when you click an important link, some text explaining what we're looking at and so on. If you'd like to learn more about wireframing, I suggest taking a look at Wireframes Magazine.

Designing

When working on project I like to follow up the wireframing with designing key frames. The website really only has three views so I created rudimentary designs of the front page and app page. I had a solid idea in my head for the error page, so I decided to skip that. This is not great practice. Getting things down on 'paper" can be a huge help. Apart from the fact that you can share it, it also helps to provide you with more perspective. I've many times kept a design idea in my head for ages thinking it was awesome, then, once drawn, it looked really horrible after a few days.

The resulting designs

As you can see, I elaborated on the app wireframe in the design. While this is okay here, in general, it is best not to do so. If this would have happened on a large project, I would have switched back to wireframing to determine the best structure. In an ideal world, I would have thought of this in the initial wireframing process anyway, underlining how important that step is for all future parts of your website.

The design process itself was quite simple. I knew I wanted a nice bonsai tree, so I began by finding a nice image on Photodune. I found a great image of a Maple Bonsai and, after some cropping, cutting and the application of one of the filters, I created the tree you see on the site now.

I decided to go for a brushed pastel feel, so I settled on a peachy color, creating a sort of monotone design. It's fairly obvious that I am no designer, but it will do just fine for out purposes here.

The Coding Plan

So based on the above, what are we building here? It helps me to think of a project from three different angles.

• On the database level. we will need a table for storing articles and users, so it might help to think of a good database structure.
• On the backend, we will need to be able to retrieve and save article and convert plain text to HTML and be able to log users in and out.
• For the frontend, we want to enable users to switch between views (plain text and HTML) with key combinations; we want them to be able to show and hide the menu with keystrokes; we need some sort of lightbox for some popups and a few other minor things.

If you want to go into more detail, you can draw up your database schema, write some skeleton functions, decide what password encoding to use, how to log users in and out (sessions or cookies for example), decide if you want to use a JS framework like jQuery and so on. You can gather all the external resources you need in advance for quick and easy implementation when you get there.

On my end, I also set up a version control system right away, SVN, along with a bug tracking system Redmine. I plan on making this app much better in the future so version control and bug tracking will be useful tools to have. I find that both are of great use on personal or non-collaborative projects as well. Version control gives you great development history (not to mention a backup), and the bug tracker is an execllent place to put bugs you want to fix, ideas you have and other notes.

Step 2: Preparing the Application

Where are we developing?

Now that we know roughly where we're going, let's get cracking. Depending your development environment, some minor basics will need to be modified. The most common setups are:

• Using localhost on your computer and using a subdirectory (eg: http://localhost/bonsaiwriter)
• Using localhost and a virtual host (eg: http://bonsaiwriter)
• Using a remote host with a dedicated domain (eg: http://mybonsaiwriter.com)
• Using a remote host with a subdomain (eg: http://bonsaiwriter.mysite.com)
• Using a remote host and a sub directory (eg: http://mysite.com/bonsaiwriter)

We will create a config file so we can cater for most of the differences there, and I will point out other issues as they arise. For now, make sure you are aware of which method you're using; don't worry, we can make the app work either way.

Creating a basic file structure

I started out by creating some of the files I knew were going to be needed, and placed them in their respective directories. This step enables me to mentally picture how the website will be structured and visualize how files are included into one another as the website runs. Here's what I came up with after a first pass.

• .htaccess
• index.php
• config.php
• footer-app.php
• footer-site.php
• footer-error.php
• 404.php
• style.less
• [DIR] app
• index.php
• [DIR] website
• index.php
• [DIR] js
• [DIR] images
• [DIR] includes
• functions.php
• appFunctions.php

The idea is that the "point of entry" file is always the root index.php file. Using .htaccess rules, all requests are sent to the main index.php file and that file determines what to do with them. This essentially means that no matter what you type in after "bonsaiwriter.com/', the main index.php file will be loaded.

To make sure that we can always use our database connection, function and other defined bits and pieces, we include load.php into every single request using another .htaccess rule. This loader file with pull in the config.php file, set up any variables, make functions we need available and so on - more on this later.

Depending on the page we are on, different files will be included via index.php. If we are looking at the app, the header-app.php file will be included, then the app/index.php file, then the footer-app.php file from the main directory. You may be wondering why the header and footer files are not in their respective directories. The primary reason is for future proofing and ease of use. In reality, the error header is not just a header for errors, it is a header for all those pages which are structured like an error page. It would be more appropriate to name that file any-page-which-just-has-the-tree-and-some-text-header.php. In our case, this is just the error page but later on this may evolve so the file is kept in the root dir to make sure I can easily include it into any file later on.

The js and images directories are empty for now, but I think are rather self explanatory. We will put all our js files into the js directory and all our images into the images directory - not exactly rocket science.

The includes directory will hold any function files or third party files we want to include. The first in there - functions.php - contains general functions which are needed for the site itself to work. A function for displaying the menu or creating an excerpt would be found in here. The appFunctions.php file will include any app specific functions for pulling the data of an article, saving it and so on.

We will manage our website's looks with a .less file.

LESS is a dynamic stylesheet language which allows us to add some programming logic to our CSS, and make working with styles a much cleaner experience.

The use of LESS will require us to reference a Javascript file as well, but this is okay because it provides us with so much flexibility. In a production environment, I would compile the LESS file to a proper CSS file, minify it and use that, but for this project, we will keep things as is.

A badly drawn diagram of how the loader works.

Setting up our loader

I first wanted to put the loader logic in place. The desired outcome is that, when you visit the root URL (http://bonsaiwriter.com in my case), the following things happen.

• load.php fires up
• config.php is included into this file right away to set things up
• our functions are included (functions.php and appFunctions.php)
• a database connection is made
• index.php is finally loaded

To get things going, head into your .htaccess file and add the following line:

 1 php_value auto_prepend_file load.php 

If you are working online, in some cases, your host (for example, Bluehost) won't allow you to include files, via .htaccess. Close and repoen your .htaccess file after trying to load your page and you should see that the auto prepend line has been commented out. If this happens, create a php.ini file in the root directory as well and use the following code in it:

 1 auto_prepend_file = "load.php" 

Next, open load.php, and type, "Hi." Open your index.php file and type ", you are awesome." If you load the website in your browser, you should see "Hi, you are awesome" being shown. This verifies that index.php is being loaded and that load.php is prepended to it. Next, let's set up our load.php file properly using the code below.

 1 /**  2  * The Website Loader File  3  *  4  * This file is responsible for loading the whole website.  5  * It includes the necessary files, fires up the database connection  6  * and so on.  7  *  8  * @author Daniel Pataki  9  * @version 0.1 alpha  10  * @package bonsaiwriter  11  */  12 // Load the configuration file  13 include("config.php");  14 // Load functions  15 include(INCLUDES_PATH."/functions.php");  16 include(INCLUDES_PATH."/appFunctions.php");  17 // Create a database connection  18 // We will add this in soon  19 ?> 

As you can see, I have added documentation to this file using the phpdoc syntax. This won't just be useful if other developers work on the project, but for you as well. If you return to it in two months to fix a bug or two, you will have no idea what's going on anymore. I will leave out the documentation on the following examples but they are there in most files; take a look at the project pack for more information.

Within load.php, the first thing we must do is include our config file, then the two function files. I have used a constant to store the path which is defined in config.php (see the next section). The last thing is establishing a database connection, which we will get to once our config file is in place.

Configuring our App

In order to transfer the app to a different domain and have various frequently used data at our fingertips, it is advisable to create a configuration file. In the first iteration, I defined a handful of constants.

 1 // Site paths and urls  2 define("PATH", "/home6/tastique/public_html/bonsaiwriter");  3 define("SITE_PATH", PATH."/site");  4 define("APP_PATH", PATH."/app");  5 define("INCLUDES_PATH", PATH."/includes");  6 define("URL", "http://bonsaiwriter.com");  7 define("AJAX_URL", URL."/ajax.php");  8 // Database Details  9 define("DB_NAME", "bonsaiwriter");  10 define("DB_USER", "bonsaiwriter");  11 define("DB_PASS", "supersecretpass");  12 define("DB_HOST", "localhost"); 

At this stage, I didn't really think things through so it may well be that we won't use APP_PATH at all, but it seemed like a good idea at the time - we can always delete it later. Never be afraid of refactoring.

These two blocks (paths and db info) will enable you to work on your localhost and then transfer the site to an online host in a matter of seconds. All you need to do is modify the values here.

Connecting to a database

The next stage in our preparation is connecting to a database. First, let's make sure we have created one. If you are using localhost, go into PhpMyAdmin (http://localhost/phpmyadmin, or http://localhost/phpMyAdmin usually) and create a new database by typing its name (bonsaiwriter) into the "Create a new database" field. Go back to the main PhpMyAdmin screen, and click the privileges tab. Click "Add a new user" below the table and use "bonsaiwriter" for the username, "localhost" for the host and "supersecretpass" for your password (although you might want to try a more secure one). Next to "global privileges," click the "check all link" and click go. You have created a database and a user which has all the privileges necessary to work with the database.

If you are online, you might not be able to create a database and a user via PhpMyAdmin. Many hosts restrict the use of this tool and you will have to use a dedicated section in the cPanel or somewhere else to accomplish this. If your host uses cPanel, I suggest using the MySQL Database Wizard to create a database and a user.

Once finished, return to the load.php file, and replace the commented line "We will add this in soon," with the following code:

 1 $bwdb = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);  This will create a new connection for us, and allow us to use the created$bwdb object for out database transaction needs.

Setting up our database tables

Once you have a database set up, you can add tables to it, via PhpMyAdmin. Just go into your database and run the following SQL.

 1 CREATE TABLE IF NOT EXISTS article (  2  ID int(11) NOT NULL AUTO_INCREMENT,  3  uri varchar(5) CHARACTER SET latin1 COLLATE latin1_general_cs NOT NULL,  4  raw text NOT NULL,  5  html text NOT NULL,  6  title varchar(120) NOT NULL DEFAULT 'Untitled Article',  7  user_id int(11) NOT NULL,  8  date_created datetime NOT NULL,  9  date_updated timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,  10  public int(1) NOT NULL DEFAULT '1',  11  PRIMARY KEY (ID),  12  UNIQUE KEY uri (uri)  13 ) ENGINE=MyISAM DEFAULT CHARSET=latin1 ;  14 CREATE TABLE IF NOT EXISTS user (  15  ID int(11) NOT NULL AUTO_INCREMENT,  16  username varchar(18) NOT NULL,  17  password char(40) NOT NULL,  18  email varchar(255) NOT NULL,  19  PRIMARY KEY (ID),  20  UNIQUE KEY username (username,email)  21 ) ENGINE=MyISAM DEFAULT CHARSET=latin1 ; 

This creates the table structure required for storing articles and users. We store things like the plain text and HTML, creation date, etc. for articles, and users consist simply of a username, password and email. Take a look at the created table for more information.

Redirecting all requests to the main index file

Now we're all set: when you load the website everything is prepended, defined, included, connected and so on, but before we move on, I want to add some .htaccess magic to make sure that no matter what URL is entered, index.php is loaded.

The reason for this is because I plan on each article having a URL like this: http://bonsaiwriter.com/8h3ef/ Since I don't want to create directories and index files for all the billions of possibilities I'll just forward all requests to one file and deal with them there. The result of this is that, if you go to http://bonsaiwriter.com/i2e23e/23e23/e23e2/23e23/, you will not get an error page, the contents of index.php will be shown. We will obviously need to remedy this for cases where a page clearly doesn't exist, but more on that in a bit!

To make sure that all your requests are handled by one file, open your .htaccess file and paste in the following lines under the existing prepend rule.

 1 Options -Indexes  2 RewriteEngine on  3 RewriteCond %{REQUEST_URI} ^  4 RewriteCond %{REQUEST_FILENAME} !-f  5 RewriteCond %{REQUEST_FILENAME} !-d  6 RewriteRule ^(.+)?$/index.php [L]  This is where things may be a bit different depending on your setup. If you are using a virtual host, a dedicated domain or a sub-domain, this should work. If you are using a sub directory, you will need to modify the last line, like so:  1 RewriteRule ^(.+)?$ /subdirectory/index.php [L] 

Once this has been put into place, you should be able to type anything into the URL bar after your base domain and it all should still be well - you should see index.php being loaded.

Showing the Right Views

Our index.php file will be our "routing" file. It will determine what you want to see and show it to you. It will know that http://bonsaiwriter.com/about/ exists but http://bonsaieriter.com/aboutus/ does not. It will know that there is an article at http://bonsaiwriter.com/334Dr/ and it will show you the app with the article, however it will detect that there is no article at http://bonsaiwriter.com/wr4rD/, so it will display an error page.

Let's look at the pages we will have first, and then determine how to direct users there. Here's a quick breakdown:

• Landing page (http://bonsaiwriter.com)
• Contact, about and legal pages (http://bonsaiwriter.com/contact/)
• Create a new article (http://bonsaiwriter.com/new/)
• Article pages (http://bonsaiwriter.com/r4J2w/)

Now all we have to do is write rules for these cases and make sure that users are sent to the correct places. Let's start by capturing the page we are on, and stripping out the parts of the URL we don't need.

 1 $page = str_replace("?".$_SERVER["QUERY_STRING"], "", $_SERVER["REQUEST_URI"]);  2 $page = substr($page, 1, -1);  The above code should be placed at the top of the index.php file, and will return the portion of the URL we need. If you go to http://bonsaiwriter.com/about/ it will return "about;" if you go to http://bonsaiwriter.com/this/is/a/page/, it will return "this/is/a/page". The method above will also need to be modified a bit if you are using subdirectories, but this can easily be done by stripping it out from the URL using the str_replace() function or something similar. If the $page variable is empty, we are on the main page. If its value is "new," we want to create a new article. If it is contact, about or legal we want to show the respective pages. In all other cases, we will check if an article exists at the given URL. If it does, then we show the article. If it doesn't, we have a 404 error so an error is shown. Let's look at the code which does all this.

 1 if(empty($page)) {  2  include(SITE_PATH."/index.php");  3  exit();  4 }  5 elseif($page == 'new') {  6  $uri = generate_article_uri();  7 $ID = insert_article($uri);  8  header("Location: ".URL."/".$uri."/");  9  exit();  10 }  11 elseif(in_array($page,$pages) AND file_exists(SITE_PATH."/".$page.".php")) {  12  include(SITE_PATH."/".$page.".php");  13  exit();  14 }  15 elseif(article_exists($page)) {  16  include(APP_PATH."/index.php");  17  exit();  18 }  19 else {  20  include(PATH."/404.php");  21  exit();  22 }  Firstly, we check if the $page variable is empty. If it is, we include the index.php from our site folder, since we want to show the front page.

If the $page variable is "new," we want to create a new article. To accomplish this, we generate a unique article identifier, henceforth called a uri. We then insert a new article into the database with that uri and redirect the user to the article's page. We will discuss these specific functions further down. The next section deals with our predefined pages. I added a simple array to config.php, which contains the pages that we want to have:  1 $pages = array("contact", "about", "legal"); 

If the current page is amongst these, and the file to present the page exists (site/pagename.php), we include that file, otherwise we move on to the next condition.

If the code gets to this next block, we have determined the following:

• We are not showing the main page
• We do not want to create a new article
• We are not showing any predefined page

All that is left is either an article page, or we have an error. In this block, we check if we have an article that has a uri which is the same as the data contained in our $page variable. We use a separate function for that which returns true if an article does exist, and false if it doesn't (more on this soon). If the article does exist, we include the index.php file from the app folder. If none of the above conditions have been met, all we are left with is that this must be an error page so we display the 404.php file. With that complete, we have accounted for all the possibilities that can happen while viewing our little web app. We've defined the set of pages that can be viewed and have said that all other pages are error pages. Functions for our App Let's continue by defining and writing some of the functions that our application will need. I started out with the following six functions - three of which we've used already: • article_exists - When passed a uri as an argument this function will tell us if it exists • generate_article_uri - Generates a random and unique 5 character string to identify an article • insert_article - Add an article to the database • get_article - Retrieves a single article from the database based on its uri • save_article - Saves the details of an article • !convert_plain_to_html - Method for converting raw text to HTML Checking for an existing article The function is extremely simple. We pass it a uri, and determine the database to make sure an article with that uri does not exist.  1 function article_exists($uri) {  2  global $bwdb;  3 $results = $bwdb->query("SELECT ID FROM article WHERE uri = '$uri' ");  4  if($results->num_rows == 0) {  5  return false;  6  }  7  else {  8  return true;  9  }  10 }  We create a result set which consists of all the articles with the given uri. If the number of rows returned is 0, we are fine, the article does not exist. Otherwise, at least one article has been found, so we need to return true - the article exists. Generating a unique uri Generating a unique uri is an integral part of the application. Whenever someone creates a new article, it needs to have a unique identifier, but we don't want extremely long urls either and we don't want to take tons of time finding a unique one. Here is how Bonsai Writer does it.  1 function generate_article_uri() {  2  global$bwdb;  3  $allowed = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";  4 $allowed = str_split($allowed);  5  do {  6 $characters = array();  7  for($i=0;$i shuffle($allowed);  8 $characters[] = $allowed[0];  9  }  10 $uri = implode($characters);  11  }  12  while (article_exists());  13  return$uri;  14 } 

First, we define all the characters we will allow. I have allowed all upper and lower case characters and all the numbers. This will give us 42 to the power of 42 number of variations - we should be safe for a while.

I have given all the allowed characters in the form of a string, and then I split it up into an array using the str_split() function. I then created a which will run five times, choosing a random member of the allowed characters array (by shuffling it on each pass and selecting the first value), adding it to our array of chosen characters. I then implode this chosen array to arrive at the final string.

As you can see, all this is in a do-while loop. This works just like a regular while loop, except that the loop is executed at least once, no matter what. In regular while loops the condition is examined first and, if it is true, the loop is run once followed by the condition being examined again. In do-while loops, the loop is run once, only then is the condition examined.

This way we can create a uri, and then check it against the database using the article_exists() function. As long as that function returns true (which means we have randomly generated a uri which exists) the loop will run again, generating another uri and checking it.

It might seem surprising, but this method is less than great if you have a huge volume website. 49 to the power of 49 is a lot, but let's look at how this could be a problem with a smaller set. Suppose you want to assign people 4 digit unique pin codes. This gives you 10,000 variations. Your site is visited by 20 people a day so 10,000 is fine for a while.

In a year's time, about 7,300 pin codes will be taken so you still have ages to implement a new system. However, since 73% of the pin codes are taken, your random number generator will spit out a taken pin code 73% of the time (essentially 2 out of 3 times). If I went to your website, it may take the system several tries before it finds a unique pin. This equates to extra database queries.

Choosing a right scheme here is important and I think with 49 characters available for 5 spots (that gives you 6 with 83 zeros after it options), we really are safe for a while.

Inserting an article

Initially, when someone creates an article, it has no data - just a uri - so inserting it is rather easy:

Saving the article

I originally wrote a function which converts our plain text to HTML and then saves the article. Subsequently I realized that some separation would be better here. It is good practice to have one function to one thing and one thing alone. The saving function should not be responsible for conversion. We might use a conversion function inside the saving functions but if we separate them the point is that both functions are available separately. Luckily I came back to rewrite the article to seem like this is how I planned it all along.

 1 function action_save_article() {  2  global $bwdb;  3 $title = "";  4  $raw = "";  5 $html = convert_plain_to_html($raw);  6 $uri = $_POST["uri"];  7 $bwdb->query("UPDATE article SET title = '$title', raw = '$raw', html = '$html' WHERE uri = '$_POST[uri]' ");  8 } 

The only things I know with certainty is that the uri will be passed to this function using the post method and that the $html variable will get its data by converting the value in the $raw variable. In any case, this is just a placeholder for now; we'll see how things progress as we go along.

Building the Front Page

Now that we have some basic functions ready, let's make something nice. The front page on the wireframe consists of three sections. There's the top section with the tree and the call to action button, the middle section with the video on the left, and a footer with three links. I actually modified this a bit, simply because I don't have time to do the video now, but the top part remains unchanged at the time of this writing.

If you remember from the previous sections, when you load any page, the main index.php file is used. When you look at the front page, it cleverly recognizes your intent and decides to include the site/index.php file. At this stage, there is still absolutely no output involved anywhere. This file contains the get_header() function which outputs the top of our page followed by some content and then the get_footer() function pulls in the bottom part of our page. Right until you call the get_header() function there is no output so you are free to set cookies, session data, output HTTP headers and so on.

The first thing I figured out was out a good HTML header for the website. The following is the top of the header-site.php file.

 1   2   3  

This is a HTML doctype with the html tag, a get_head() function and the body tag with the view in the id attribute (in this case site, but it could be app, or error as well). I have chosen to use a function to get all the header data like the site title, scripts, styles and so on because its cleaner and provides you with one more level of flexibility. If you look at the code in functions.php for the get_head() function, all it does is pull in the head-site.php file, so let's go there now and take a look.

 1   2   3 Bonsai Writer  4   5   6   7   8   9   10   11   12   13   14  

There might be some extra bits in there in the project files, but this is the gist of it. We have our title and character set defined, a bunch of different favicons tied to the page, our style is included along with the LESS JavaScript pulled in from Google Code.

To get LESS working, you need to include the LESS stylesheet first and then the LESS script. This will automagically make LESS work as intended.

I won't explain the rest of the HTML here; take a look at the header file and the LESS file to see what's going on. There's nothing special about the HTML involved.

Building the Error Page -

When viewing an error page, the 404.php file will be included. This file works just like the index file in the site directory. It includes a header, has some content, and includes the footer. The only added bit is the setting of a header, which we can do at the top of this file since there is no output at that point yet.

Setting the header is extremely important since, otherwise, the user will receive a 200 header - meaning everything is okay. Code-wise, everything is okay, since we are requesting a file (index.php) which is there, so we do have the resource the user is looking for on a file level. However, we know that we don't have the content, so we need to tell the world that this is actually a 404 page.

 1  

Since I presume that the app will have a few different error pages in the future, I decided to modularize the next step instead of coding it directly. I created a fatal_error() function which takes two parameters - a title and some content - and display's a nice error message. The function can be found in functions.php.

 1 function fatal_error($title,$content) {  2  get_header("error");  3  echo "  4 

".$title."  5   6 ";  7  echo "  8  ".$content."
 9 

10  ";  
11    get_footer("error");  
12  } 

The function itself pulls in the error header, adds the title and content, then pulls in the footer. The LESS file makes this all nice and pretty, but otherwise we're done with the error page.

This is a great example of bad planning and coding on my part. First of all, I would not consider a 404 error to be a fatal error so the function is incorrectly named. Also, since I intend to use this function for all errors, it would make much more sense to pass this an error number argument and have it generate the HTTP header itself which, right now, is done outside of the function.

If you are working to quickly put up an alpha version or you're building an advanced wireframe, I think these mistakes are okay. We know they are there, we can come and fix them later; we just want to have something online to get feedback and try for ourselves.

Building the App Page

We've finally arrived to the point where we can build our app. I know in advance that I want four distinct things on this page. I want to make a nice header bar, a sidebar, an editor and a preview area. The header will always be visible, the sidebar can be hidden with a key combination or a click on the tree. By default only the editor will be visible, but using a key combination the editor can be hidden and the preview area can be shown.

I first went into the header-app.php file and made sure that the details of the article being viewed are pulled into a variable. Since we wrote a function for this, it won't cause a huge headache, and i will be able to refer to this later on.

 1 global $article;$article = get_article(); 

I also made sure that jQuery was added to the head-app.php file, pulled from Google Code. I added a file to the js directory named app.js and tied it to the app by adding it to the head-app.php file.

 1   2  

Once done, I went into the app/index.php file and created this skeleton.

 1   2 
 3 
 4 


I made sure to create the menu.php file in the app directory and placed some placeholder text in it. I also hid the preview area using my CSS skills. Additionally I added some rudimentary styling to put everything roughly where it was going to be. Since the next thing on my mind was building some JS controls I gave everything clashing background colors so I could see what was going on.

Showing and hiding elements with JavaScript

It's time to write some JavaScript! My first order of business was to go into app.js, write the following into it, and reload the app page.

 1 $(document).ready(function() {  2  alert("lol");  3 })  On reload, you should see an alert box with the text, "lol," in it. This verifies that your JavaScript file is tied to the app and that jQuery is working as well. Let's continue by enabling users to control the visibility of the menu using the ctrl+space key combination.  1 $(document).keydown(function(e) {  2  if(e.which == 32 && e.ctrlKey == true) {  3  $("#app_menu").toggle();  4  return false;  5  }  6 })  When using key event functions, you can utilize the parameter of the function to obtain all sorts of information about the button which was pushed. The space key has the keycode of 32, and the ctrlKey attribute will be true if the control was pressed. So if the keycode is indeed 32 and the ctrlKey is true, the user has pushed the ctrl+space key combination. In this case, let's toggle the menu - closing it if it is open and vice versa. We also return false to make sure a space is not inserted into the texture of the editor if we are using it. If you want to see all the details related to a key press, you can use the following method which works in most browsers which have a JavaScript debugger built in or Firebug installed.  1 $(document).keydown(function(e) {  2  console.log(e);  3 }) 

Right after I did this, I used some abstraction to separate the toggling of the menu from the event. The two reasons for this are:

1. The toggling of the menu will eventually need much more code than just this one toggle line. We will need to change some classes and even send an AJAX call to store the status of the menu later on so it is kept closed throughout page loads if a user closes it once
2. The menu can be controlled in other ways as well so we don't want to duplicate code. You can also hide and show the menu by clicking on the tree icon so pasting in the code to open the menu and save its state in this eventuality as well is redundant and unnecessary.
 1   2 $(document).keydown(function(e) {  3  if(e.which == 32 && e.ctrlKey == true) {  4  toggle_menu()  5  return false  6  }  7 })  8 function toggle_menu() {  9  if($("#app_menu").is(":visible")) {  10  $("#app_logo").removeClass('current')  11  }  12  else {  13 $("#app_logo").addClass('current')  14  }  15  $("#app_menu").toggle()  16 }  Let's follow the same logic when creating the action which will switch our view from edit to preview mode using the shift+space key combination.  1 $(document).keydown(function(e) {  2  if(e.which == 32 && e.shiftKey == true) {  3  switch_mode()  4  return false  5  }  6 })  7 function switch_mode() {  8  $("#editor").toggle();  9 $("#preview").toggle();  10 } 

With that done, the main part of our application frontend is complete. All that we need to do is save the data in the text field, convert it and make it available in the preview box. Before we do that, I'll make a slight detour to show you how this app handles AJAX requests, but I'll be back to saving our data in no time.

Handling AJAX

If you're familiar with how WordPress handles AJAX requests, then you are on friendly territory. Otherwise, this method might seem a bit complex compared to the functionality we need. I will explain my reasoning for using this method once I've shown you how it actually works.

All AJAX calls in the app are handled by the ajax.php file in the main directory. This file checks that the AJAX call originates from inside the app, that it has the proper parameters, and so on.

To get an AJAX call to work in this app, you will need to do at least three things:

1. Give it the ajax.php file as the target URL
2. Make sure it passes an "action" parameter
3. Make sure there is a function defined in a function file with the name of the passed action parameter

Let's look at the innards of ajax.php to better understand all this.

 1 if(isset($_SERVER["HTTP_REFERER"]) OR empty($_SERVER["HTTP_REFERER"]) OR substr($_SERVER["HTTP_REFERER"],0, strlen(URL)) == URL) {  2 $title = "A fatal error has occurred";  3  $content = "  4   5  It seems like you are trying to initiate an action from outside the app which  6  either means that you are a naughty person, or that a very weird error has occured in our app.  7  Please contact us if you think you have found an error with the app  8   9   10   11  ";  12  fatal_error($title, $content);  13  die();  14 }  15 if(isset($_REQUEST["action"]) OR empty($_REQUEST["action"]) OR is_string($_REQUEST["action"])) {  16  $title = "A fatal error has occured";  17 $content = "  18   19  It seems like there is an incorrect link in our application. It very probable that  20  the action you initiated was not completed. Please contact us  21  if you think you have found an error with the app.  22   23   24   25  ";  26  fatal_error($title,$content);  27  die();  28 }  29 call_user_func($_REQUEST["action"]);  In the first if block, we check the referrer (where we came from) to make sure there is no naughty business. It must be defined, it can not be empty and it must start with our base url. If this condition is not met, we generate an error page. The second block makes sure that an action is defined. If something is wrong, we show an error page again. As an interesting side note, it was at this point that I realized that I should have defined my error header in the fatal_error() function. I also realized that the first error actually is a fatal error while the second isn't; so it would be better to rename the function. If all is well, we call the function, which has the name of our action parameter's value. If you pass "save_article" as the parameter, the save_article() function will be run. In our application, we will only have 1-2 AJAX calls, so we could just create separate files for them. This is a valid opinion but I always try to think ahead. This setup enables me to do much more. In fact, by the time you read this, I may extend it a bit to accommodate for more flexibility. Being able to essentially "plug" our AJAX calls into existing functions makes everyone's lives much easier and is the basis for a plugins system, which may yet pop into existence if the app becomes popular. Converting plain text to HTML At the very root of the service is its capability to convert text to HTML. There are a lot of factors which go into this and a lot more will be developed, but let's look at some of the most common conversions we will need to pull off. We will use a lot of regular expressions to parse the plain text for symbols, characters and whatnot to help us convert it to HTML. Saving the data As the first step, let's make sure that we are saving the changes without converting them. Let's add some JavaScript, which will save our article after every 30th key press.  1 k = 0  2 $(document).keyup( function() {  3  if(k % 30 == 0) {  4  save_article()  5  }  6  k++  7 })  8 function save_article() {  9  raw = $("#editor").val()  10  uri =$("body").attr("data-uri")  11  $.ajax({  12  url: ajaxurl,  13  type: "post",  14  data: {raw : raw, uri : uri, action : "action_save_article"},  15  success: function(response) {  16 $("#preview").html(response)  17  }  18  })  19 } 

First, we define the variable k which holds the number of keys pressed since the page was loaded. We add one to that variable each time a key is pressed. If the variable can be divided by 30 without a remainder we save the article.

The save_article function grabs our plain text from the editor and the uri of the article which is stored as an attribute of the body tag. We then make an AJAX call with the help of jQuery. We set the url to ajaxurl - a variable defined in head-app.php - we set the type to post and define the data we want to send. This data includes the plain text, the uri and an action which is needed for the AJAX call to work; this is the function which will be run. We then take the response of the PHP script we run and output it to the preview box.

We previously created a skeleton for the action_save_article() function in the appFunctions.php file; so let's go there now and modify it.

 1 function action_save_article() {  2  global $bwdb;  3 $raw = $_POST["raw"];  4 $uri = $_POST["uri"];  5 $tpos = strpos($raw, "\n");  6 $title = substr($raw, 0,$tpos);  7  if (empty($title)) {  8 $title = "Untitled Article";  9  }  10  $title =$bwdb->real_escape_string($title);  11 $body = substr($raw,$tpos);  12  $html = convert_plain_to_html($body);  13  echo "  14 

\n".$title."\n  15   16 \n".stripslashes(stripslashes($html));  
17    $bwdb->query("UPDATE article SET title = '$title', raw = '$raw', html = '$html' WHERE uri = '$uri' " );  18 }  19 function convert_plain_to_html($plain) {  
20    return $plain;  21 }  For ease of use, I have pulled the raw text contents into $raw and the uri into $uri. I then found the position of the first line break. Everything before this is the title, everything afterwards is the content. We find the title by using the substr() function and store it in the $title variable. In case it is empty, we make sure we still have a title - "Untitled Article". Finally we make sure to escape the value. This is user input so we need to protect against malicious code being executed on our servers.

The body of the text is everything except the title; so we use substr() again to put the body text into the $body variable. For now, the value of our $html variable is set to be the same as the value of the $body variable. I have created a separate function which will take care of this but for now it just returns the plain text back. We will change this in a second, we just want to make sure this all works. If you type at least 30 characters into your texture and reload the page, the saved text should be pulled into the textarea from the database. Converting the data Now its time to modify the value of the $html variable to get the actual HTML. When planning out how this should work, I laid down three guidelines for myself.

• The basis of converting the text into HTML will be the new line character. I will split the whole text into chunks separated by new lines.
• I will convert these chunks to whatever is needed (a list element, a pre code block, etc.), everything else will be converted to a paragraph.
• These chunks can contain additional markup for inline elements like links, code elements, bold text, etc.

With that in mind, let's give this a go. First, let's separate our text by the new lines, and make each chunk created a paragraph.

 1 function convert_plain_to_html($plain) {  2 $html = explode("\n", $text);  3  foreach($html as $key =>$paragraph) {  4  if(trim($paragraph) == "") {  5  unset($html[$key]);  6  }  7  else {  8 $html[$key] .= "  9   10 \n".$paragraph."\n  11   12 \n";  13  }  14  }  15  $html = implode("\n",$html);  16  $html = str_replace("\n", "  17 ",$html);  18  $html =$bwdb->real_escape_string($html);  19 $html = str_replace("  20 ", "\n", $html);  21  return$html  22 } 

Initially, we use explode to split our text into an array. Each line will be in a separate member in the array. Now let's go through each member of the array and add some paragraph tags. If the line is empty, we unset that member since we don't want to have empty paragraph tags messing up the flow of our document. Otherwise we add a paragraph tag before and after the text.

Notice that I have added newlines before and after the text and after the closing paragraph tag. This is to ensure that the source code looks nice. Instead of having paragraphs one after another in the code, they will be a bit more manageable. Later on I will add tabs as well to make this all indented. This is not essential for our app to work but in many cases authors will have to paste the source code into the WordPress backend for example, so having it nicely presented is a great bonus.

To match this pattern, all we need to do is search for any occurrence of "[Link:" and make sure to grab everything until we get to a closing bracket. We iterate over all the resulting matches and parse out the anchortext and the url, using explode. The first member of the $link array will be the anchortext, the second the url. We then build an HTML link from these two elements. Finally we search for the original link text in the plain text string and replace it with our shiny new link. I also wanted to make sure that inline code can be placed in the following way.  1 You need to use the <code here> tag to create inline code  Once again, let's do some parsing and replacing. You'll want to place the code below, just after or before the previous link checking code.  1 preg_match_all("/@@([^@]*)@@/",$paragraph, $match_codes, PREG_SET_ORDER);  2 foreach($match_codes as $match_code) {  3 $paragraph = str_replace($match_code[0], "".htmlentities($match_code[1])."", $paragraph);  4 }  We parse out those bits which start with a double at sign, then contain anything except an at sign, then end in a double at sign. To ensure that code is shown properly, we convert all the appropriate characters into HTML entities and wrap the code tags around the thing. As I am making my final review of this article I have noticed an error in the above code, but I'll leave it in as a good example of how to arrive at better code through iteration. Normally, it is good practice to search for a string beginning with a specific character and grab everything except some character. This is the method used for HTML tag parsing and other uses, however, it is incorrect in our case. The above regular expression would not play nicely with inline code which contains an at symbol while there is no reason it shouldn't contain one. In our case, the above code should be modified like so:  1 preg_match_all("/(.*)/",$paragraph, $match_codes, PREG_SET_ORDER);  Converting Block Level Elements Converting block level elements is a bit trickier because they require an opening and closing tag on a separate line. If we want to create a list, the markup in plain text would look something like this:  1 Let's create a paragraph here, and then a list underneath.  2 # This is a numbered list  3 # This is its second member  4 # And this is its third member  5 Another paragraph will be written here  It is not enough to detect that one of the lines starts with a hash. We also need to add an opening list tag before the first occurrence and a closing tag after the last element. The way I pulled this off is all right, but it could be much more modular to help me expand on it later. If we get to a list element, before we do any parsing we should add an opening list tag. Since the next list element is a new line we would be adding opening tags in front of every list element. So in addition to adding the opening tag I also set a globally available variable - let's call it $prev_ol - to true. Id we get to a list element and $prev_ol is false we know we just arrived to a new list so we add a start tag, add the list element and set $prev_ol to true, indicating that we are now in a list. Once we get to the next list element the value of $prev_ol is still true so we don't add an opening tag, we just add a list element. Once we get to any element which is not a list element we make sure that if $prev_ol is true, we add a closing list tag and set $prev_ol to false. Our resulting code is the following.  1 function convert_plain_to_html($plain) {  2  $html = explode("\n",$text);  3  $prev_ol = false;  4  foreach($html as $key =>$paragraph) {  5  if(trim($paragraph) == "") {  6  unset($html[$key]);  7  }  8  else {  9  // Parsing inline elements  10  preg_match_all("/\([^]*)/",$paragraph, $match_links, PREG_SET_ORDER);  11  foreach($match_links as $match_link) {  12 $link = explode("|", $match_link[1]);  13 $link = "".$link[0]."";  14 $paragraph = str_replace($match_link[0]."]",$link, $paragraph);  15  }  16  preg_match_all("/@@([^@]*)@@/",$paragraph, $match_codes, PREG_SET_ORDER);  17  foreach($match_codes as $match_code) {  18 $paragraph = str_replace($match_code[0], "".htmlentities($match_code[1])."", $paragraph);  19  }  20  // Parsing block level elements  21  if(isset($match_ulist[1]) AND !empty($match_ulist[1])) {  22 $html[$key] = "";  23  if($prev_ol == false) {  24 $html[$key] .= "  25 
 26 
\n";
 27 
 28    29 
 30 
}
 31 
 32    33 
 34 
$html[$key] .= "  35 
1. \n".$match_olist[1]."\n 2.  36   37   38   39   40  \n";  41   42   43   44 $prev_ol = true;
 45 
 46    47 
 48 
}
 49 
 50    51 
 52 
else {
 53 
 54    55 
 56 
if($prev_ol == true) {  57   58   59 $html[$key] .= "  60   61 \n";  62  }  63 $html[$key] .= "  64   65 \n".$paragraph."\n  
66    
67  \n";  
68          $prev_ol = false;  69  }  70  }  71  }  72 $html = implode("\n", $html);  73 $html = str_replace("\n", "  
74  ", $html);  75 $html = $bwdb->real_escape_string($html);  
76    $html = str_replace("  77 ", "\n",$html);  
78    return $html  79 }  As you can see, the code is rapidly becoming more complex here. We also needed to modify how paragraph tags are handled. Since something is either a list or a paragraph at this point, we need to make sure that if we switch from a list to a paragraph the list which is before it is closed. If we are creating a paragraph and $prev_ol is true (we were handling a list before this paragraph), we need to add a closing tag before we start handling our paragraph. We then need to make sure $prev_ol is set to false again. -- More replacements -- To see more replacements, check out the full function in project files. I have catered for only a handful of possibilities, but I am adding more and more. Most replacements can be categorized into one of the two methods I've described above so adding them should not be a huge problem. - Enabling user accounts - Enabling users for this website was a rather easy process, although this is mainly because of the restricted feature set. The three main areas on Bonsai Writer to handle users are registration, authentication and login detection. Registration Registration works with a simple form which allows users to submit their email address for registration. This is done via an AJAX call which fires the action_register() function, let's take a look.  1 function action_register() {  2  global$bwdb;  3  $_POST["email"] = trim($_POST["email"]);  4  if (!isset($_POST["email"]) OR empty($_POST["email"])) {  5  echo 'please enter an email address';  6  }  7  elseif(!filter_var($_POST["email"], FILTER_VALIDATE_EMAIL)) {  8  echo 'please use a valid address';  9  }  10  elseif(check_user_exists($_POST["email"])) {  11  echo 'that email address has already been registered';  12  }  13  else {  14  if($_POST["notify"] != 'true' ) {  15 $plain_pass = substr(sha1(time().$_POST["email"]."saltword"),0,8);  16 $pass = sha1($plain_pass);  17 $subject = "Welcome To Bonsai Writer";  18  $message = "  19  Hello and Welcome to Bonsai Writer!  20   21  You can now use the password ".$plain_pass." to log in.  
22          We strongly advise changing this when you first log in.  
23    
24          We wish you a nice and productive stay, if you have any comments, suggestions or  
25          problems, feel free to contact us at any time.  
26    
27          Bonsai Writer  
28        ";  
29        $headers = 'MIME-Version: 1.0' . "\r\n";  30 $headers .= 'Content-type: text/html; charset=iso-8859-1' . "\r\n";  
31        $headers .= 'From: Bonsai Writer ' . "\r\n";  32  mail($_POST["email"], $subject,$message, $headers);  33  }  34 $email = $bwdb->real_escape_string($_POST["email"]);  
35      $bwdb->query("INSERT INTO user (email, password) VALUES ('$email', '$pass')");  36  if($_POST["notify"] == "true") {  

A better system

The login system is also somewhat rudimentary and not unhackably safe. I would prefer to use more secure \$_COOKIES or some newer methods available, but this was good as a quick placeholder to test functionality. There is no indication here of any standardization to accommodate for additional login methods such as oAuth or integration with services such as Twitter and Facebook.

Wrapping up the Website

There were a number of other techniques utilized in this app that I haven't covered here but you should be able to find all of them by looking into the files provided. For example, the CSS font-face property is used to embed three fonts to make the website look nice, CSS is used to create the round icons at the top bar in the app and so on.

Colorbox, an excellent jQuery plugin is used to create the modal windows, which appear when you click on the icons, and Elastic is used to make our textarea grow dynamically as we type into it.

A wonderful .htaccess ruleset is in the htaccess file which I retrieved from Perishable Press. This will help keep evil hackers out of your site.

A great number of features can be added as well. This is just the initial incarnation - a test version if you will - of this application. It will support custom CSS embedding, the definition of custom rules and many handy functions to help authors and editors do their job. My goal is to have an instant preview of an article in its native environment (whichever online website I am writing for).

Final Thoughts

While this is in no way an extensive article on how to create a web service or a website, I hope it has provided some insight on the workflow and the logic behind building something for the web. Remember, you don't have to get things right in the first round. You just need to be able to make it better and better!

The block level replacement handling is needs refactoring, but not to worry, I will get in there and overhaul it in the next few weeks to make it leaner and the app will be that much better. This iteration of the app will enable me to test the functionality, find the flaws, gauge the requirements of users and make it better bit by bit. The underlying logic of how I do things will not really change, however I will probably add more planning to the next release.

In this tutorial we've learned how to put a rudimentary website together which is a skill all in itself. I see a lot of people who know PHP, HTML, CSS, Javascript; the only thing they are missing is the ability to put it all together on their own. Keep in mind that this is only one way of doing things. In many cases, it is better to go with popular frameworks and pre built templates, but it is always nice to know how to do it yourself.