Skip to content

Instantly share code, notes, and snippets.

@code-boxx
Last active May 4, 2024 18:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save code-boxx/5e0cbc797a61fe938e0868e3702e879c to your computer and use it in GitHub Desktop.
Save code-boxx/5e0cbc797a61fe938e0868e3702e879c to your computer and use it in GitHub Desktop.
PHP MYSQL Admin Panel Template

SIMPLE PHP MYSQL ADMIN PANEL

https://code-boxx.com/simple-php-admin-panel/

IMAGES

pic

NOTES

  1. Create a database and import 1-users.sql.
  2. Open 2-lib-admin.php, change the database settings to your own.
  3. Access 3a-login.php. The default user is joy@doe.com, and the password is 123456.

LICENSE

Copyright by Code Boxx

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

-- (A) USERS TABLE
CREATE TABLE `users` (
`user_id` bigint(20) NOT NULL,
`user_email` varchar(255) NOT NULL,
`user_name` varchar(255) NOT NULL,
`user_password` varchar(255) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
ALTER TABLE `users`
ADD PRIMARY KEY (`user_id`),
ADD UNIQUE KEY `user_email` (`user_email`),
ADD KEY `user_name` (`user_name`);
ALTER TABLE `users`
MODIFY `user_id` bigint(20) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=1;
-- (B) DEFAULT USER
-- EMAIL: JOY@DOE.COM | PASSWORD: 123456
INSERT INTO `users` (`user_id`, `user_email`, `user_name`, `user_password`) VALUES
(1, 'joy@doe.com', 'Joy Doe', '$2y$10$vZJy7y4uqQQTRN3zdi2RE.5ZJJzGEEPnzEjFXm4nEOx023XQ2Qe..');
<?php
class Admin {
// (A) CONSTRUCTOR - CONNECT TO DATABASE
private $pdo = null;
private $stmt = null;
public $error = "";
function __construct () {
$this->pdo = new PDO(
"mysql:host=".DB_HOST.";dbname=".DB_NAME.";charset=".DB_CHARSET,
DB_USER, DB_PASSWORD, [
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC
]);
}
// (B) DESTRUCTOR - CLOSE CONNECTION
function __destruct () {
if ($this->stmt !== null) { $this->stmt = null; }
if ($this->pdo !== null) { $this->pdo = null; }
}
// (C) HELPER FUNCTION - RUN SQL QUERY
function query ($sql, $data=null) : void {
$this->stmt = $this->pdo->prepare($sql);
$this->stmt->execute($data);
}
// (D) GET USER BY ID OR EMAIL
function get ($id) {
$this->query(sprintf("SELECT * FROM `users` WHERE `%s`=?",
is_numeric($id) ? "user_id" : "user_email"
), [$id]);
return $this->stmt->fetch();
}
// (E) SAVE USER
function save ($name, $email, $password, $id=null) {
// (E1) SQL & DATA
$sql = $id==null
? "INSERT INTO `users` (`user_name`, `user_email`, `user_password`) VALUES (?,?,?)"
: "UPDATE `users` SET `user_name`=?, `user_email`=?, `user_password`=? WHERE `user_id`=?" ;
$data = [$name, $email, password_hash($password, PASSWORD_DEFAULT)];
if ($id!=null) { $data[] = $id; }
// (E2) RUN SQL
$this->query($sql, $data);
return true;
}
// (F) VERIFICATION
function verify ($email, $password) {
// (F1) GET USER
$user = $this->get($email);
$pass = is_array($user);
// (F2) CHECK PASSWORD
if ($pass) { $pass = password_verify($password, $user["user_password"]); }
// (F3) REGISTER MEMBER INTO SESSION
if ($pass) {
foreach ($user as $k=>$v) { $_SESSION["admin"][$k] = $v; }
unset($_SESSION["admin"]["user_password"]);
}
// (F4) RESULT
if (!$pass) { $this->error = "Invalid email/password"; }
return $pass;
}
}
// (G) DATABASE SETTINGS - CHANGE TO YOUR OWN
define("DB_HOST", "localhost");
define("DB_NAME", "test");
define("DB_CHARSET", "utf8mb4");
define("DB_USER", "root");
define("DB_PASSWORD", "");
// (H) START!
session_start();
$_ADM = new Admin();
<?php
// (A) LOAD LIBRARY
require "2-lib-admin.php";
// (B) CHECK LOGIN CREDENTIALS
if (count($_POST)!=0) {
$_ADM->verify($_POST["email"], $_POST["password"]);
}
// (C) REDIRECT IF SIGNED IN
if (isset($_SESSION["admin"])) {
header("Location: 5-protected.php");
exit();
} ?>
<!DOCTYPE html>
<html>
<head>
<title>Admin Login</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.5">
<link rel="stylesheet" href="3b-login.css">
</head>
<body>
<!-- (D) LOGIN FORM -->
<?php
if ($_ADM->error!="") { echo "<div class='error'>".$_ADM->error."</div>"; }
?>
<form method="post">
<h1>ADMIN LOGIN</h1>
<label>Email</label>
<input type="email" name="email" required>
<label>Password</label>
<input type="password" name="password" required>
<input type="submit" value="Login">
</form>
</body>
</html>
/* (A) WHOLE PAGE */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
body {
background: #f2f2f2;
}
/* (B) LOGIN FORM */
form {
width: 400px;
padding: 20px;
margin: 0 auto;
border: 1px solid #ccc;
background: #fff;
}
form h1 {
font-size: 28px;
margin: 0 0 20px 0;
}
form label, form input {
display: block;
width: 100%;
}
form label {
color: #7e7e7e;
padding: 10px 0;
}
form input { padding: 10px; }
form input[type=submit] {
margin-top: 20px;
border: 0;
font-weight: 700;
color: #fff;
background: #c51111;
cursor: pointer;
}
/* (C) ERROR */
.error {
padding: 10px;
margin-bottom: 20px;
border: 1px solid #ff7979;
background: #ffdada;
}
<?php
// (A) LOAD LIBRARY
require "2-lib-admin.php";
// (B) LOGOUT
if (isset($_POST["logout"])) { unset($_SESSION["admin"]); }
// (C) REDIRECT IF NOT SIGNED IN
if (!isset($_SESSION["admin"])) {
header("Location: 3a-login.php");
exit();
} ?>
<!DOCTYPE html>
<html>
<head>
<title>Pure HTML CSS Admin Template</title>
<meta charset="utf-8">
<meta name="robots" content="noindex">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.5">
<link rel="stylesheet" href="4c-admin.css">
</head>
<body>
<!-- (D) SIDEBAR -->
<div id="pgside">
<!-- (D1) BRANDING OR USER -->
<form id="pguser" method="post" onclick="if(confirm('Sign Off?')){this.submit();}">
<input type="hidden" name="logout" value="1">
<img src="pic.png" id="pguserimg">
<div class="txt">
<div id="pgusername"><?=$_SESSION["admin"]["user_name"]?></div>
<div id="pguseracct">account | logoff</div>
</div>
</form>
<!-- (D2) MENU ITEMS -->
<a href="#" class="current">
<i class="ico">&#9733;</i>
<i class="txt">Section A</i>
</a>
<a href="#">
<i class="ico">&#9728;</i>
<i class="txt">Section B</i>
</a>
<a href="#">
<i class="ico">&#9737;</i>
<i class="txt">Section C</i>
</a>
</div>
<!-- (E) MAIN -->
<main id="pgmain">
</main>
</body>
</html>
/* (A) GLOBAL */
* {
font-family: Arial, Helvetica, sans-serif;
box-sizing: border-box;
}
body {
display: flex;
min-height: 100vh;
padding: 0; margin: 0;
}
/* (B) SIDEBAR */
/* (B1) SIDEBAR ITSELF */
#pgside {
width: 200px;
transition: width 0.2s;
background: #283039;
}
/* (B2) USER OR BRANDING */
#pgside #pguser {
display: flex;
align-items: center;
padding: 10px;
background: #043368;
cursor: pointer;
}
#pgside #pguserimg {
border-radius: 50%;
width: 50px;
margin-right: 10px;
}
#pgside #pgusername {
color: #ff6a6a;
font-weight: 700;
text-transform: uppercase;
}
#pgside #pguseracct { font-size: 13px; }
/* (B3) SIDEBAR ITEMS */
#pgside, #pgside a { color: #fff; }
#pgside a {
display: block;
padding: 20px;
width: 100%;
text-decoration: none;
cursor: pointer;
}
#pgside a.current { background: #7c1919; }
#pgside a:hover { background: #9b2323; }
/* (B4) SIDEBAR ICONS & TEXT */
#pgside .ico, #pgside .txt { font-style: normal; }
#pgside .ico {
font-size: 1.1em;
margin-right: 10px;
}
/* (B5) SMALL SCREEN TRANSFORMATION */
@media screen and (max-width:768px) {
#pgside { width: 70px; }
#pgside #pguser { justify-content: center; }
#pgside a {
text-align: center;
padding: 20px 0;
}
#pgside .ico {
font-size: 1.5em;
margin-right: 0;
}
#pgside .txt { display: none; }
#pgside #pguserimg { margin-right: 0; }
}
/* (C) MAIN CONTENTS */
#pgmain {
flex-grow: 1;
padding: 20px;
background: #f2f2f2;
}
<?php require "4a-top.php"; ?>
Your dashboard here.
<?php require "4b-bottom.php"; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment