Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- includes/admin_content.php
- <div class="container-fluid">
- <!-- Page Heading -->
- <div class="row">
- <div class="col-lg-12">
- <h1 class="page-header">
- Admin
- <small>Subheading</small>
- </h1>
- <?php
- // $result_set = User::find_all_users();
- // while ($row = mysqli_fetch_array($result_set)) {
- // echo $row['username'] . "<br>";
- // }
- // $found_user = User::find_user_by_id(2);
- // $user = User::instantiation($found_user);
- // echo $user->first_name;
- // $users = User::find_all_users();
- // foreach ($users as $user) {
- // echo $user->id . "<br>";
- // }
- $found_user = User::find_user_by_id(2);
- echo $found_user->username;
- ?>
- <ol class="breadcrumb">
- <li>
- <i class="fa fa-dashboard"></i> <a href="index.html">Dashboard</a>
- </li>
- <li class="active">
- <i class="fa fa-file"></i> Blank Page
- </li>
- </ol>
- </div>
- </div>
- <!-- /.row -->
- </div>
- includes/database.php
- <?php
- require_once('new_config.php');
- class Database {
- public $connection;
- function __construct() {
- $this->open_db_connection();
- }
- public function open_db_connection() {
- //$this->connection = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
- $this->connection = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
- if($this->connection->connect_errno) {
- die('Database connection failed ' . $this->connection->connect_error);
- }
- }
- public function query($sql) {
- $result = $this->connection->query($sql);
- $this->confirm_query($result);
- return $result;
- }
- private function confirm_query($result) {
- if(!$result) {
- die('Query failed' . $this->connection->error);
- }
- }
- public function escape_string($string) {
- $escaped_string = $this->connection->real_escape_string($string);
- return $escaped_string;
- }
- public function the_insert_id() {
- return $this->connection->insert_id;
- }
- }
- $database = new Database();
- ?>
- includes/functions.php
- <?php
- function classAutoLoader($class) {
- $class = strtolower($class);
- $the_path = "includes/{$class}.php";
- if(is_file($the_path) && !class_exists($class)) {
- include $the_path;
- }
- // if(file_exists($the_path)) {
- // include($the_path);
- // require_once($the_path);
- // } else {
- // die("File named {$class}.php was not found.");
- // }
- }
- function redirect($location) {
- header("Location: {$location}");
- }
- spl_autoload_register('classAutoLoader');
- ?>
- includes/header.php
- <?php require_once('init.php') ?>
- <?php ob_start(); ?>
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <meta http-equiv="X-UA-Compatible" content="IE=edge">
- <meta name="viewport" content="width=device-width, initial-scale=1">
- <meta name="description" content="">
- <meta name="author" content="">
- <title>SB Admin - Bootstrap Admin Template</title>
- <!-- Bootstrap Core CSS -->
- <link href="css/bootstrap.min.css" rel="stylesheet">
- <!-- Custom CSS -->
- <link href="css/sb-admin.css" rel="stylesheet">
- <!-- Custom Fonts -->
- <link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
- <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
- <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
- <!--[if lt IE 9]>
- <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
- <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
- <![endif]-->
- </head>
- <body>
- <div id="wrapper">
- includes/init.php
- <?php
- require_once('functions.php');
- require_once('new_config.php');
- require_once('database.php');
- require_once('user.php');
- require_once('session.php');
- require_once('login.php');
- ?>
- includes/new_config.php
- <?php
- //Database Connection Constants
- define('DB_HOST','localhost');
- define('DB_USER','root');
- define('DB_PASS','');
- define('DB_NAME','gallery_db');
- ?>
- includes/session.php
- <?php
- class Session {
- private $signed_in = false;
- public $user_id;
- function __construct() {
- session_start();
- $this->check_the_login();
- }
- public function is_signed_in() {
- return $this->signed_in;
- }
- public function login($user) {
- if($user) {
- $this->user_id = $_SESSION['user_id'] = $user->id;
- $this->signed_in = true;
- }
- }
- public function logout($user) {
- unset($_SESSION['user_id']);
- unset($this->user_id);
- $this->signed_in = false;
- }
- private function check_the_login() {
- if(isset($_SESSION['user_id'])) {
- $this->user_id = $_SESSION['user_id'];
- $this->signed_in = true;
- } else {
- unset($this->user_id);
- $this->signed_in = false;
- }
- }
- }
- $session = new Session();
- ?>
- includes/user.php
- <?php
- class User {
- public $id;
- public $username;
- public $password;
- public $first_name;
- public $last_name;
- public static function find_all_users() {
- return self::find_this_query("SELECT * FROM users");
- }
- public static function find_user_by_id($user_id){
- global $database;
- $the_result_array = self::find_this_query("SELECT * FROM users WHERE id = $user_id LIMIT 1");
- return !empty($the_result_array) ? array_shift($the_result_array) : false;
- }
- public static function find_this_query($sql) {
- global $database;
- $result_set = $database->query($sql);
- $the_object_array = array();
- while($row = mysqli_fetch_array($result_set)) {
- $the_object_array[] = self::instantiation($row);
- }
- return $the_object_array;
- }
- public static function verify_user($username, $password) {
- global $database;
- $username = $database->escape_string($username);
- $password = $database->escape_string($password);
- $sql = "SELECT * FROM users WHERE ";
- $sql .= "username = '{$username}' ";
- $sql .= "AND password = '{$password}' ";
- $sql .= "LIMIT 1";
- //$sql = "SELECT * FROM users WHERE username = '{$username}' AND password = '{$password}' LIMIT 1";
- $the_result_array = self::find_this_query($sql);
- return !empty($the_result_array) ? array_shift($the_result_array) : false;
- }
- public static function instantiation($the_record) {
- $the_object = new self;
- // $the_object->id = $found_user['id'];
- // $the_object->username = $found_user['username'];
- // $the_object->password = $found_user['password'];
- // $the_object->first_name = $found_user['first_name'];
- // $the_object->last_name = $found_user['last_name'];
- foreach ($the_record as $the_attribute => $value ) {
- if($the_object->has_the_attribute($the_attribute)) {
- $the_object->$the_attribute = $value;
- }
- }
- return $the_object;
- }
- private function has_the_attribute($the_attribute) {
- $object_properties = get_object_vars($this);
- return array_key_exists($the_attribute, $object_properties);
- }
- }
- ?>
- index.php
- <?php include("includes/header.php"); ?>
- <?php if(!$session->is_signed_in()) {redirect("login.php");} ?>
- <!-- Navigation -->
- <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
- <!-- Brand and toggle get grouped for better mobile display -->
- <?php include("includes/top_nav.php"); ?>
- <!-- Sidebar Menu Items - These collapse to the responsive navigation menu on small screens -->
- <?php include("includes/side_nav.php"); ?>
- <!-- /.navbar-collapse -->
- </nav>
- <div id="page-wrapper">
- <!-- /.container-fluid -->
- <?php include("includes/admin_content.php"); ?>
- </div>
- <!-- /#page-wrapper -->
- <?php include("includes/footer.php"); ?>
- login.php
- <?php require_once('includes/header.php'); ?>
- <?php
- if($session->is_signed_in()) {
- redirect("index.php");
- }
- if(isset($_POST['submit'])) {
- $username = trim($_POST['username']);
- $password = trim($_POST['password']);
- ///Method to check database user
- $user_found = User::verify_user($username, $password);
- if($user_found) {
- $session->login($user_found);
- redirect("index.php");
- } else {
- $the_message = "Your password or username are incorrect";
- }
- } else {
- $the_message = "";
- $username = "";
- $password = "";
- }
- ?>
- <div class="col-md-4 col-md-offset-3">
- <h4 class="bg-danger"><?php echo $the_message; ?></h4>
- <form id="login-id" action="" method="post">
- <div class="form-group">
- <label for="username">Username</label>
- <input type="text" class="form-control" name="username" value="<?php echo htmlentities($username); ?>" >
- </div>
- <div class="form-group">
- <label for="password">Password</label>
- <input type="password" class="form-control" name="password" value="<?php echo htmlentities($password); ?>">
- </div>
- <div class="form-group">
- <input type="submit" name="submit" value="Submit" class="btn btn-primary">
- </div>
- </form>
- </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement