- PHP 99.9%
| app | ||
| config | ||
| includes | ||
| pages | ||
| sql | ||
| tools | ||
| .htaccess | ||
| index.php | ||
| README.md | ||
PHP Login, Registration & Role System
A modular PHP foundation with central routing, role-based access
control, login/registration against a MySQL user table, and a
user-management area for administrators.
Setup in XAMPP
- Copy this entire folder into
C:\xampp\htdocs\<any-folder-name>. The folder name is completely free to choose — routing adapts automatically. - Start Apache and MySQL in XAMPP.
- In phpMyAdmin, create a database, e.g.
meine_datenbank. - Import
sql/database.sqlinto that database — it contains the full table setup (this is the single, complete SQL file for the project). - Open
config/database.phpand adjust the credentials (host, database name, user, password) to match your environment. - Make sure Apache has
mod_rewriteandmod_authz_coreenabled (default in XAMPP) and thatAllowOverride Allis set forhtdocs, so the.htaccessfiles take effect. - Create the first administrator (required — otherwise nobody
can access
/admin/users):- Open in your browser:
http://localhost/<folder-name>/tools/generate_hash.php?pw=test1234 - Copy the generated
INSERTstatement into phpMyAdmin and run it (see also the comments insql/database.sql). - Afterwards, delete
tools/generate_hash.phpor lock down thetools/folder.
- Open in your browser:
- Open in your browser:
http://localhost/<folder-name>/
Flow for new users
- A user registers at
/register→ gets thependingrole. - They can log in, but
/dashboardonly shows a message that the account is awaiting approval. - An administrator opens
/admin/users, clicks "Edit" next to that user, and assigns a proper role (role1,role2,role3, oradmin). - From then on, the user has access to whatever pages their role is allowed to see.
Project structure
config/database.php Central DB connection ($pdo)
config/routes.php Central routing table (URL -> file + roles)
config/roles.php Central list of all roles (used for dropdowns)
app/bootstrap.php Loads DB, session, helpers, auth; blocks direct file access
app/helpers.php url()/redirectTo() (subfolder-safe), flash messages
app/auth.php Login, registration, profile, role checks
app/router.php Reads routes.php, checks access, loads the page
includes/header.php Shared page header incl. water.css, navigation, flash messages
includes/footer.php Shared page footer
pages/*.php The actual page content
sql/database.sql Single SQL file: table creation + first-admin instructions
tools/generate_hash.php Helper script to generate password hashes
Adding a new page
- Create a new file under
pages/, following the pattern ofpages/role1_area.php. Always start withrequire_once __DIR__ . '/../app/bootstrap.php';, and always use theurl('/path')helper for links instead of hardcoded/pathstrings — this keeps the app working in any subfolder. - Add an entry to
config/routes.php:
'/my-url' => [
'file' => 'pages/my_page.php',
'roles' => ['role1', 'role2'], // or ['ALL'] for public, ['AUTH'] for any logged-in user
],
The router automatically takes care of the login and role checks.
Roles
There are five fixed roles (see config/roles.php):
pending— automatically assigned after registration, no extended accessrole1,role2,role3— the three application-level rolesadmin— access to user management (/admin/users)
Use roles => ['ALL'] for public pages (no login required), and
roles => ['AUTH'] for pages any logged-in user may see, regardless
of their specific role.
Notes on the routing/subfolder fix
Page files are required from inside the route() function in
app/router.php, which runs them in that function's local scope.
Because of this, any page that needs the database connection
directly (rather than through a helper function in app/auth.php)
must declare global $pdo; before using it — this is already done
in all pages/admin_*.php files.
The BASE_PATH constant (see app/helpers.php) is derived from
dirname($_SERVER['SCRIPT_NAME']), so the router and every generated
link work correctly regardless of which subfolder of htdocs the
project is installed in.