dummies logo
Articles

Article Categories

close
TechnologyAcademics & The ArtsHome, Auto, & HobbiesBody, Mind, & SpiritBusiness, Careers, & Money
Books

Book Categories

close
TechnologyAcademics & The ArtsHome, Auto, & HobbiesBody, Mind, & SpiritBusiness, Careers, & Money
Collections

Collections

Explore all collections
close
BYOB (Be Your Own Boss)Be a Rad DadCareer ShiftingContemplating the CosmosFor Those Seeking Peace of MindFor the Aspiring AficionadoFor the Budding Cannabis EnthusiastFor the College BoundFor the Exam-Season CrammerFor the Game Day Prepper
Custom Solutions
Log in
dummies logo
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
  • Article Categories
    forward arrow
  • Book Categories
    forward arrow
  • Collections
    forward arrow
  • Custom Solutions
  • Log In
  • forward arrow
    Main Menu
  • Article Categories

  • Technology
  • Academics & The Arts
  • Home, Auto, & Hobbies
  • Body, Mind, & Spirit
  • Business, Careers, & Money
  • Log In
  • forward arrow
    Main Menu
  • Book Categories

  • Technology
  • Academics & The Arts
  • Home, Auto, & Hobbies
  • Body, Mind, & Spirit
  • Business, Careers, & Money
  • Log In
  • forward arrow
    Main Menu
  • Collections

    Explore all collections
  • BYOB (Be Your Own Boss)
  • Be a Rad Dad
  • Career Shifting
  • Contemplating the Cosmos
  • For Those Seeking Peace of Mind
  • For the Aspiring Aficionado
  • For the Budding Cannabis Enthusiast
  • For the College Bound
  • For the Exam-Season Crammer
  • For the Game Day Prepper
  • Log In
Home
Technology Articles
Programming & Web Design Articles
General Programming & Web Design Articles

Creating the App Startup Files for Your Web App

By: 
Paul McFedries
|
Updated:  
2018-07-12 18:25:12
|
From The Book:  
Web Coding & Development All-in-One For Dummies
Download E-Book
Web Coding & Development All-in-One For Dummies
Explore Book
HTML & CSS Essentials For Dummies
Explore Book
Buy On Amazon
All web apps perform a number of chores at the beginning of any task. On the back end, these initialization chores include starting a user session and connecting to the database, and on the front end the startup includes outputting the app's common HTML (especially the <head> section) and including the app’s common components, such as a header and footer.

Rather than repeating the code for these startup chores in every file, you should create two files — one for the back end initialization and one for the front end’s common code — and then include the files as you begin each web app task.

Creating the back-end initialization file

When performing any task, a typical web app must first run through a number of back-end chores, including the following:
  • Setting the error reporting level
  • Starting a session for the current user, if one hasn’t been started already
  • Creating a token for the session
  • Including common files, such as a file of constants used throughout the app
  • Connecting to the database, if the app uses server data
You should store this file in your web app’s private/common/ directory.:

// Start a session session_start();

// Have we not created a token for this session, // or has the token expired? if (!isset($_SESSION['token']) || time() > $_SESSION['token_expires']){ $_SESSION['token'] = bin2hex(openssl_random_pseudo_bytes(16)); $_SESSION['token_expires'] = time() + 900; $_SESSION['log_id'] = 1; }

// Include the app constants include_once 'constants.php';

// Connect to the database $mysqli = new MySQLi(HOST, USER, PASSWORD, DATABASE);

// Check for an error if($mysqli->connect_error) { echo 'Connection Failed! Error #' . $mysqli->connect_errno . ': ' . $mysqli->connect_error; exit(0); } ?>

This code cranks up the error reporting to 11 for the purposes of debugging, starts a new session, creates a session token (if needed), includes the constants file (which contains the database credentials), and then connects to the database and creates a MySQLi object. Note, too, that $_SESSION['log_id'] was set to 1, but this is temporary.

You want to use error_reporting(E_ALL | E_STRICT) when you’re developing your web app because you want the PHP processor to let you know when something’s amiss, either as an error (E_ALL) or as non-standard PHP code (E_STRICT). However, you certainly don't want your app’s users to see these errors or warnings, so when you’re ready for your web app to go live, edit initialization.php to follow this statement:

error_reporting(E_ALL | E_STRICT)
with these statements:
ini_set('display_errors', 0);
ini_set('log_errors', 1);
ini_set('error_log', '../private/logs/error_log');
These statements configure PHP to not display errors onscreen, but to log them to a file, the name and path of which is specified in the final statement.

Creating the front-end common files

Each page of your web app has a common structure. For example, the top part of each page includes the following elements:
  • The DOCTYPE and the <html> tag
  • The head element, including the <meta> tags, page title, CSS <link> tags, and JavaScript <script> tags
  • An event handler for jQuery's ready event
  • The <body> tag
  • Common page elements, such as the <header>, <nav>, and <main> tags
Here's an example, which is named public/common/top.php:



  
  
  FootPower! | <?php echo $page_title ?>
  
  
  


  

In this code, note that the page title is given by the following inline PHP:
The idea here is that each page will set the $page_title variable just before including top.php, which enables you to define a custom title for each page. For example, the home page might do this:
Note that this same title also gets inserted in the page header element, within the <h1> tag.

Most web apps also include a sidebar — defined by an <aside> tag — that includes info common to all pages, such as a description of the app, instructions for using the app, the latest app news, or a newsletter sign-up form. For this sidebar, create a separate file called, say, public\common\sidebar.php and include your code:

Your sidebar text and tags go here
Finally, you need a file to handle the common elements that appear at the bottom of each page, including the </main> closing tag, a footer, and the </body> and </html> closing tags. For this code, create a separate file called, say, public\common\bottom.php and add your code:
  
Copyright Your Name
The footer uses the PHP statement echo date('Y') to output the current year for the Copyright notice. This file also adds references to the app's two external JavaScript files: data.js and user.js. Adding these at the bottom of the page (instead of the usual place in the page's head section) ensures that your JavaScript code can work with the elements added to the page on the fly.

Building the app home page

With the initialization files in place, it’s time to build the skeleton for the app’s home page. At the moment, this page is nothing but PHP:
="code">
Main app content goes here
Save this file as index.php in the web root directory.

About This Article

This article is from the book: 

Web Coding & Development All-in-One For Dummies

About the book author:

Paul McFedries is a Google® Workspace administrator, a thankless job if ever there was one. Paul is also a full-time technical writer who has somehow found the time to write more than 100 books that have sold more than four million copies worldwide.

This article can be found in the category: 

General Programming & Web Design
Trending
Marvel Comics For Dummies Cheat Sheet
How to Make a Margarita
How to Write Effective AI Prompts for Different Real World Uses
Football For Dummies (USA Edition) Cheat Sheet
MacOS Monterey For Dummies Cheat Sheet
From Category
General Programming & Web Design
Why Local Environments Hold Your Developers Back
Building DIY Websites For Dummies Cheat Sheet
HTML, CSS, & JavaScript All-in-One For Dummies Cheat Sheet
How To Automate DevOps in the Cloud
Using DevOps To Improve Engineering

Quick Links

About For DummiesContact UsActivate Online Content

Connect

  • facebook iconX (formally known as Twitter) iconyoutube icon

About Dummies

Dummies has always stood for taking on complex concepts and making them easy to understand. Dummies helps everyone be more knowledgeable and confident in applying what they know. Whether it's to pass that big test, qualify for that big promotion or even master that cooking technique; people who rely on dummies, rely on it to learn the critical skills and relevant information necessary for success.

 Terms of Use Privacy PolicyCookies Settings Do Not Sell My Personal Info - CA Only

Copyright © 2000-2025 by John Wiley & Sons, Inc., or related companies. All rights reserved, including rights for text and data mining and training of artificial technologies or similar technologies.
© 2025 MARVEL

Terms of Use
Privacy Policy
Cookie Settings
Do Not Sell My Personal Info - CA Only