Code Examples

A number of examples of code which may be useful for writing websites is presented here

Apache Web Server

Some issues relating to Local and Remote Apache web servers are considered.

EC2 Debian

Setting up an EC2 instance on Amazon Web Servers and installing a Debian Amazon Machine Image.

Templates

Some useful template for copying and pasting.

File Uploads

An HTML form that uploads a file to my Bytemark Apache server is demonstrated here. It is mainly concerned with the necessary and hopefully secure ownership change of the directory the files are uploaded to.

Reduce Image File Size

An HTML form that uploads an image file and saves it as well as a 1/10th size version is implemented here.

SQLite

An HTML form uploads data to a database on my Bytemark Apache server. It is mainly concerned with the necessary and hopefully secure change of ownership of the SQLite database file and the directory it is in.

Find

grep -R "DOCUMENT_ROOT" . is very good for finding files which contain a particular string (in this case "DOCUMENT_ROOT"). Don't forget the trailing dot!

.htaccess

A .htaccess file can be used to display errors. This advice came from https://stackify.com/display-php-errors/ The required code in the .htaccess file is:


      php_flag display_startup_errors on
      php_flag display_errors on
    

A start has been made to understand Apache Mod_Rewrite rules and apply them to directing all HTTP requests to an index.php file in a particular directory. This could be useful if a routing approach to web building was taken.

Download a CSV File to Browser

Data on the server, whether in a file or generated from a script, can be sent to the browser with appropriate http headers which cause the browser to ask the user to save the data as a file. This is shown with just three lines of PHP.

If you echo PHP_EOL you will get a new line in a text editor and new row in a spreadsheet.

Paths

Note that href values for hyperlinks, including to some resources like CSS files, are URLs. Like paths, in PHP include and require statements, they can be absolute or relative. They are not part of PHP and a lot of what is covered in this section does not apply to them.

A website involves a great deal of linking files, generally in different directories, together. The paths between the linked files have to be described accurately and of course modified whenever the structure of the website is changed. Some approaches can make this easier to manage but any approach will require considerable care.

Paths are either relative or absolute. Relative paths can be convenient to use as they are normally shorter and therefore less effort to write. Absolute paths have the advantage that they will work independently of where in the file system they are used.

Including a file which includes a file is a particular problem although the answer is to make sure you use an absolute path. Then you can choose the most convenient and robust method to form that absolute path but that will depend on personal preferences and priorities.

Composer

Using Composer, the PHP package manager.

Nav

A header with nested dropdowns taken from a tutorial by Dotted Squirrel. The dropdowns can be nested infinitely without changing the CSS.

A debugging excercise resulted in an interesting point being noted about the requirement for focus-within to be applied to an element where its child not sibling ul is the dropdown. Read child.php first and then sibling.php. A semantically better version called semantic.php was also made.

A hamburger icon with a dropdown navigation list is built first. Then the same navigation list is used to build a horizontal flex navigation panel. Finally the two are merged to give responsive navigation which consists of a hamburger on mobiles and flex navigation panel on larger width screens.

Query String Question

I was not sure if query strings could be used when the url ends in the name of a directory inside which there is an index.php file. Both the following URLs, which point to the same file, work on my Bytemark server. Try them: query-strings/index.php?name=Steve and query-strings/?name=Steve.

Stripe Payment

A stripe account was opened, a product created from the Stripe dashboard, and the code to sell that generated by Stripe was added to a webpage. This, Client Only Dashboard approach gives limited control over how a product(s) can be sold but can be extended to allow the user to choose how many units of a product they want to buy.

A start has been made implementing webhooks which enable events occurring on your Stripe account, such as a card payment from a web page, to be sent to your server and logged or otherwise dealt with. Firstly webhooks are created on the Stripe command line and the response from our server detected on the Stripe command line. Then in webhooks-two we detect webhooks on the server sent from Stripe in response to a purchase on our site. We need to extract the information about the purchase event from the webhook so it can be used to fulfill business logic on our site.

A more involved procedure, Client Only API approach, allowing greater control, for dynamic pricing was attempted but is not working.

Server Probe

A number of scripts have been linked to from a page I have called probe. These reveal information about the environment, for example about PHP and Apache, they are served from.

Build Overview

Key steps and the order they are taken in are considered

Geolocation

This page uses javascript to access the user's location with the Geolocation API. The Location is displayed using console.log(position).

EXIF

This page uses PHP's EXIF extension to access and display EXIF data from a jpg file.

Login

Implenting login solutions for websites.

Favicons

How to make those pesky things...

Service Worker

How to implement service workers so that website can have offline functionality.

Dev-Prod-GitHub

A workflow for developing, putting into production, version control and back up.

Compare SQLite databases

Comparing two SQLite databases can be useful for testing and other reasons.

JavaScript Modules

A variable representing a DOM element is declared inside a module. Also, in the module the variable has an event handler added to it. The variable (not the event handler code) is exported from the module and imported into main.js which is linked to from index.php. When invoked from index.php the variable and event handler all work as they should.

JavaScript Delete button: Confirm or Cancel

If a user clicks a delete button it is possibly inadvertant but the consequences may be quite painful. So, a generic method for applying a cancel or confirm step is provided here.

Make, and reuse, Bootable USB Stick

Creating a bootable usb stick is slightly different from just putting data on the stick. Also once the stick does have an iso image on it and is rebootable you can not use it again without reformatting the disk. How I did these procedures is described here.