Web 5: php controlled site with semantic html5

Goal

The student uses basic php to build a full website. Login variables will be kept in memory between page loads using php’s $_SESSION superglobal. In the process, code will be separated to reduce duplication. I will present a prototyping code layout that I have found useful.

The pages must validate correctly with:

Naming conventions:

  • all lowercase with underscores separating words

  • this works well in sql too

Page structure aims to separate out the main logic areas:

  • app logic:

    • holds code that authorizes users for the app, connects to services the whole app needs, etc.

  • page logic deals with:

    • holds code that authorizes users for the page

    • ensuring any parameters sent to the page is valid

    • gets or changes data according to those parameters, i.e. the business requirements

    • puts the returned data or responses into the appropriate data structure; in php, this is usually an array

  • display logic:

    • reformats the returned data or responses into the appropriate layout replacement variables

    • includes the layout.php to fill the template’s display variables

Layout.php aims to provide a general html5 page template for the site:

  • the html5 page contains several php variables that are filled in when the page is included by other php scripts

  • the variables tend to be:

    • $pg_title

    • $header_title

    • $err_msg or user_msg

    • $content

    • any others depending on the business logic

$content:

  • html5 pertinent to the particular page’s content

  • this can be even more complex than the layout.php template

Template’s structure becomes:

  • doctype

  • html

    • head

      • meta

      • style links or declarations

      • javascript links or declarations

      • title

    • body

      • main

        • header

        • content section declaration

          • content variable here

        • footer

Site structure:

  • the login page, index.php

    • contains a form that calls itself when the user submits the form

    • you should already have the logic worked out for this from Web 4

  • the authentication logic:

    • checks that the username and/or password fields have been filled

    • reports with an error message if not

    • checks for the correct user and password

    • reports with an error message if not

    • if correct,

      • set the user session variable, i.e. save the user’s name to be checked later

      • redirects to the welcome.php page

  • the other pages:

  • checks the user session variable, to ensure that they have logged in successfully

  • if not successful, redirects to login page

  • if successful, display the appropriate page content

  • logout:

    • destroys the user session variable

    • redirects to login page

Tutorials

I am not sure how useful LinkedIn Learning is for this.

The Codecourse youtube channel - https://www.youtube.com/playlist?list=PLfdtiltiRHWHjTPiFDRdTOPtSyYfz3iLW, has a set of understandable and comprehensive beginner and intermediate php tutorials as well as great tips. He covers several other technologies as well.

Traversy has 'PHP Front To Back,' https://www.youtube.com/playlist?list=PLillGF-Rfqbap2IB6ZS4BBBcYPagAjpjn, which has some topics and examples not covered by Codecourse.

Basic: The first link might be a good tutorial for this. One correction, is that session_destroy() is nolonger used. Instead, unset a session variable to delete it. Use this code snippet for initializing the session:

session_start();
if ( !isset( $_SESSION['users'] ) ) {
    session_cache_limiter('private, must-revalidate');
    /* configure any default session variables that you need to,i.e. */
    $_SESSION['users'] = [
        'user1'=>'password1',
        'user2'=>'password2',
        etc.
    ];
};

Intermediate: * Codecourse’s 'Tips for cleaner code: Cleaning up IF statements,' https://www.youtube.com/watch?v=ldqDpmMkXgw. * 'Php Session Handling,' http://php.net/manual/en/book.session.php

General References

  1. Tania Rascia has a gem of a site with easy to understand introductory to intermediate tutorials - https://www.taniarascia.com/blog/ - on css, mysql, php, and jquery

  2. https://www.w3schools.com/

  3. https://php.net/ is php’s definitive reference

  4. 'Mozilla Developer Network,' https://developer.mozilla.org/en-US/.