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.

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 System

Here we implement a sign-up and login system that stores users' email addresses and hashed passwords in an SQLite database. When a user logs in a session is started which enables the id of the user to be accessed and therefore the website can be personalised for them.