Create co-branded content that converts with Dummies Custom Solutions. Request info today!
Create co-branded content that converts with Custom For Dummies books, AI, videos, infographics, and more. Request info today!
dummies logo
Dummies AI
Browse Book & Article Categories

Book & Article 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
dummies logo
  • Book & Article Categories
    forward arrow
  • Collections
    forward arrow
  • Custom Solutions
  • Dummies AI
  • forward arrow
    Main Menu
  • Book & Article Categories

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

  • Technology
  • Academics & The Arts
  • Home, Auto, & Hobbies
  • Body, Mind, & Spirit
  • Business, Careers, & Money
  • Dummies AI
  • 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
  • Dummies AI
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
Share
Download E-Book
Web Coding & Development All-in-One For Dummies
Cover of Web Coding & Development All-in-One For Dummies, 2nd edition by Paul McFedries.
Explore Book
Rust All-in-One For Dummies
Rust All-in-One For Dummies book cover
Explore Book
Buy NowSubscribe on Perlego
Download E-Book
Web Coding & Development All-in-One For Dummies
Cover of Web Coding & Development All-in-One For Dummies, 2nd edition by Paul McFedries.
Explore Book
Rust All-in-One For Dummies
Rust All-in-One For Dummies book coverExplore Book
Buy NowSubscribe on Perlego
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

Hot off the press

Explore Related content

Resources
Books

General Programming & Web Design

Cheat Sheet

Rust All-in-One For Dummies Cheat Sheet

Master Rust programming! Our Rust cheat sheet simplifies complex ownership rules, essential Cargo commands, and common compiler errors.

View [type]

Cheat Sheet

Button Text

General Programming & Web Design

Articles

Why Local Environments Hold Your Developers Back

Discover how local development environments hinder your developers. Learn why cloud development environments can boost innovation and efficiency.

View [type]

Articles

Button Text

General Programming & Web Design

Cheat Sheet

Building DIY Websites For Dummies Cheat Sheet

Unlock the secrets and best practices that web developers know and implement when building any quality website with this cheat sheet.

View [type]

Cheat Sheet

Button Text

General Programming & Web Design

Cheat Sheet

HTML, CSS, & JavaScript All-in-One For Dummies Cheat Sheet

This Cheat Sheet has handy info you can use when learning the basics of HTML, CSS, and JavaScript, including ready-to-go HTML templates.

View [type]

Cheat Sheet

Button Text

General Programming & Web Design

Articles

How To Automate DevOps in the Cloud

If you are hoping to maximize your DevOps efforts, try automating tasks using your cloud tools. Use this guide to find out how, from Dummies.com.

View [type]

Articles

Button Text

General Programming & Web Design

Articles

Using DevOps To Improve Engineering

Improving your engineering peformance with DevOps can have organization-wide benefits. Use this guide to learn how to make those adjustments.

View [type]

Articles

Button Text

General Programming & Web Design

Articles

Moving to DevOps Processes: From a Line to a Circuit

Find out how the DevOps approach changes development from a linear structure to a cyclical method, using this guide from Dummies.com.

View [type]

Articles

Button Text

General Programming & Web Design

Articles

How to Form DevOps Teams in Your Organization

Want to know how to structure teams in your organization to succeed with DevOps? FInd out with this guide from Dummies.com.

View [type]

Articles

Button Text

General Programming & Web Design

Articles

How to Choose a Cloud Service Provider for DevOps

When implementing a DevOps initiative, you'll want to make sure you have the right cloud provider and tools. Use this guide to evaluate your DevOps options.

View [type]

Articles

Button Text

General Programming & Web Design

Articles

Interview Techniques for Building a DevOps Team: Getting the Right Technical Skills

Hiring candidates with the right technical skills is critical to your DevOps success. Use these techniques to evaluate your DevOps candidates.

View [type]

Articles

Button Text

General Programming & Web Design

Articles

11 Ways DevOps Benefits Your Organization

DevOps offers many benefits for your organization. Discover all the ways a DevOps approach can help your business, from Dummies.com.

View [type]

Articles

Button Text

General Programming & Web Design

Articles

Continuous Integration and Continuous Delivery: Implementing and Benefitting from CI/CD

Organizations can benefit from continuous integration and continuous delivery. Use this guide from Dummies.com to discover how to implement these changes.

View [type]

Articles

Button Text

General Programming & Web Design

Articles

Top 10 DevOps Pitfalls: Why Your Software Projects Fail

DevOps can transform your approach to development, but there are many pitfalls to avoid along the way. Use this guide from Dummies.com to navigate the way.

View [type]

Articles

Button Text

General Programming & Web Design

Articles

What Is DevOps?

What is DevOps? DevOps is an agile-derived approach that helps guide software development to prioritize people over process. Find out more from Dummies.com.

View [type]

Articles

Button Text

General Programming & Web Design

Articles

Take Action with GitHub Actions

Learn about GitHub Actions, one of the newer, most exciting features of GitHub. GitHub Actions makes it possible to create custom workflows on GitHub.

View [type]

Articles

Button Text

General Programming & Web Design

Articles

Hackathons and Other Networking Events for GitHub Software Developers

GitHub hosts and sponsors several conferences throughout the year, which are good opportunities for software developers to network.

View [type]

Articles

Button Text

General Programming & Web Design

Articles

The GitHub Marketplace

Explore the GitHub Marketplace where you can find tools to extend or integrate with GitHub and where you can purchase apps to use with GitHub.

View [type]

Articles

Button Text

General Programming & Web Design

Articles

GitHub Apps and Probot

Learn about GitHub’s Probot framework and walk through how to build a simple GitHub App and explore Glitch, a web application hosting platform.

View [type]

Articles

Button Text

General Programming & Web Design

Articles

How to Write a Great GitHub Pull Request

Learn how to write a great GitHub pull request by knowing your audience, keeping the request focused, and including an action—from Dummies.com.

View [type]

Articles

Button Text

General Programming & Web Design

Articles

What Are GitHub Pull Requests?

Learn all about GitHub pull requests, what they are, how to set the options, and how to create the actual requests—from Dummies.com.

View [type]

Articles

Button Text
Rust All-in-One For Dummies

Rust All-in-One For Dummies

Explore Book
HTML & CSS Essentials For Dummies

HTML & CSS Essentials For Dummies

Explore Book
Building DIY Websites For Dummies

Building DIY Websites For Dummies

Explore Book
HTML, CSS, & JavaScript All-in-One For Dummies

HTML, CSS, & JavaScript All-in-One For Dummies

Explore Book
XML All-in-One Desk Reference For Dummies

XML All-in-One Desk Reference For Dummies

Explore Book
UML 2 For Dummies

UML 2 For Dummies

Explore Book
Windows Game Programming For Dummies

Windows Game Programming For Dummies

Explore Book
XSLT For Dummies

XSLT For Dummies

Explore Book
Visual Basic 6 Database Programming For Dummies

Visual Basic 6 Database Programming For Dummies

Explore Book
Building 3D Digital Games

Building 3D Digital Games

Explore Book
UNIX For Dummies

UNIX For Dummies

Explore Book
Visual Basic 6 For Dummies

Visual Basic 6 For Dummies

Explore Book
Building a Web Site For Dummies

Building a Web Site For Dummies

Explore Book
Perl For Dummies

Perl For Dummies

Explore Book
HTML, XHTML and CSS For Dummies

HTML, XHTML and CSS For Dummies

Explore Book
Ajax For Dummies

Ajax For Dummies

Explore Book
Writing Computer Code

Writing Computer Code

Explore Book
Drupal For Dummies

Drupal For Dummies

Explore Book
VisualBasic .NET For Dummies

VisualBasic .NET For Dummies

Explore Book
Visual Basic .NET Database Programming For Dummies

Visual Basic .NET Database Programming For Dummies

Explore Book
Building Websites All-in-One For Dummies

Building Websites All-in-One For Dummies

Explore Book
UNIX For Dummies Quick Reference

UNIX For Dummies Quick Reference

Explore Book
XML For Dummies

XML For Dummies

Explore Book
Apache, MySQL, and PHP Web Development All-in-One Desk Reference For Dummies

Apache, MySQL, and PHP Web Development All-in-One Desk Reference For Dummies

Explore Book
HTML5 & CSS3 For Dummies

HTML5 & CSS3 For Dummies

Explore Book
Visual Basic .NET All-In-One Desk Reference For Dummies

Visual Basic .NET All-In-One Desk Reference For Dummies

Explore Book
VBA For Dummies

VBA For Dummies

Explore Book
Ruby on Rails For Dummies

Ruby on Rails For Dummies

Explore Book
Beginning Programming All-in-One For Dummies

Beginning Programming All-in-One For Dummies

Explore Book
Web Design All-in-One For Dummies, 2nd Edition

Web Design All-in-One For Dummies, 2nd Edition

Explore Book
Web Coding & Development All-in-One For Dummies

Web Coding & Development All-in-One For Dummies

Explore Book
PHP, MySQL, & JavaScript All-in-One For Dummies

PHP, MySQL, & JavaScript All-in-One For Dummies

Explore Book
GitHub For Dummies

GitHub For Dummies

Explore Book
Functional Programming For Dummies

Functional Programming For Dummies

Explore Book
DevOps For Dummies

DevOps For Dummies

Explore Book
Beginning Programming All-in-One Desk Reference For Dummies

Beginning Programming All-in-One Desk Reference For Dummies

Explore Book

Where to find

shop now

Available at these book sellers across the United States.

web coding & development all-in-one for dummies

Web Coding & Development All-in-One For Dummies

Find a Book Store Near You
Find a Book Store Near You
Shop Now
Shop Now

Quick Links

About For DummiesContact UsActivate Online ContentSite Map

Connect

  • Follow us on FacebookFollow us on InstagramFind us on Xyoutube 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-2026 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.

© 2026 MARVEL

Terms of Use
Privacy Policy
Cookie Settings
Do Not Sell My Personal Info
Perlego logo in black serif font on a white background.

Get a Subscription