The Web Programming Checklist (for page at a time applications)

Security

Security Solutions General Principles


Application State vs Browser State (Low level PHP Solutions)

  1. The browsers back button, Bookmarking pages ------------------------------------ Firstpage: ------------------------------------ <?php $postback=mt_rand(); $_SESSION['postback']=$postback; ?> ... <form ... action="https://website.com/secondpage.php"> <input type="hidden" name="postback" value="<?= $postback ?>" /> ... </form> ------------------------------------ Secondpage: ------------------------------------ # check if $_REQUEST['postback']==$_SESSION['postback']
  2. Browser cache
    Solution: Combination of... <head> <meta http-equiv="cache-control" content="max-age=0" /> <meta http-equiv="cache-control" content="no-cache" /> <meta http-equiv="expires" content="0" /> <meta http-equiv="expires" content="Tue, 01 Jan 1980 1:00:00 GMT" /> <meta http-equiv="pragma" content="no-cache" /> </head>
  3. Website performance
    Solution: Allow caching of images, and pages as appropriate, re-use images.
  4. Users can have multiple browsers pointed at the same application. At the same page.
    Solution: In some cases, ie wizards is a good example, definitive application state is maintained in the back end. Application backend knows what the user is supposed to see, no matter what they say they see. Application only handles user requests addressing current application state. All others cause user state to be updated.

Application Mechanics (Low level PHP Solutions)

  1. Forms and page lifecycle
    1. Validation: Where? On the browser (maybe), certainly on the server
    2. What to do if inputs are invalid? Redisplay the form, with error messages and previously filled in values reapplied.
    3. Prefilling form values for update.
    4. Handling a postback vs a first time view.

    Solution: One global approach is to write page code as follows... <?php # logic here to prepopulate default form # variables depending on why we are displaying this page if($isValidationError){ $myField=$_REQUEST['myField']; ... } else if($isFirstView){ $myField=""; ... } else if($isUpdate){ $myField=...# Fetch old values (ie) from database. } ?> <form ... action="https://website.com/secondpage.php"> <input type="text" name="myField" value="<?= $myField ?>" /> ... </form>
  2. Protected content (ie. only authenticated users allowed here). Handling login and different roles (users can still attempt to view pages they should not). Have a stub at the top of your protected pages that ... <?php if($_SESSION['is_logged_in']!="yes"){ header( 'Location: https://mysite.com/login.html' ) ; }
  3. Layout to create a uniform experience: include and require
  4. Design to create a uniform appearance: centralized in CSS
  5. Language independence, content and character sets. Lots to say here. We will say nothing. It is a big issue!

Mid Level Solutions

Build your own framework. Typically looks like:
  1. MVC (Model, View, Controller).

    Note: Page at a time MVC lacks change notification arrow.
  2. Use a front controller. All requests go through the front controller. It runs a state machine, keeping current page state etc. Submits go back to the current page state for processing. A subsequent page state may result.
  3. Maintain client side state on the server. Which page you expect the client to submit from. Include a 'token' with a random value. Check the tokens value when they resubmit.
  4. Validation takes place on the server. Upon submit, the server checks if this is a postback. Sends inputs to the page state for validation. Validation includes collecting error messages in a predefined array. This array appears on the returned page (usually the current page states page).
  5. To prefill/refill form values, input elements must be selectively coded on the back end to populate them with appropriate values.

High level solution: Use an existing Framework

Use a framework (ie) CodeIgniter, Struts, ASP.NET, DJango, RubyOnRails, ... and hope the framework does much of what you need done. They typically include:

Issues:

Application Scalability

Ummm, yea. A whole other topic!