Advertisement
Guest User

Udemy OOP CMS Course

a guest
Mar 16th, 2019
404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.98 KB | None | 0 0
  1. includes/admin_content.php
  2.  
  3. <div class="container-fluid">
  4.  
  5. <!-- Page Heading -->
  6. <div class="row">
  7. <div class="col-lg-12">
  8. <h1 class="page-header">
  9. Admin
  10. <small>Subheading</small>
  11. </h1>
  12.  
  13. <?php
  14.  
  15. // $result_set = User::find_all_users();
  16.  
  17. // while ($row = mysqli_fetch_array($result_set)) {
  18. // echo $row['username'] . "<br>";
  19. // }
  20.  
  21. // $found_user = User::find_user_by_id(2);
  22.  
  23. // $user = User::instantiation($found_user);
  24.  
  25. // echo $user->first_name;
  26. // $users = User::find_all_users();
  27. // foreach ($users as $user) {
  28. // echo $user->id . "<br>";
  29.  
  30. // }
  31.  
  32. $found_user = User::find_user_by_id(2);
  33. echo $found_user->username;
  34.  
  35.  
  36. ?>
  37.  
  38. <ol class="breadcrumb">
  39. <li>
  40. <i class="fa fa-dashboard"></i> <a href="index.html">Dashboard</a>
  41. </li>
  42. <li class="active">
  43. <i class="fa fa-file"></i> Blank Page
  44. </li>
  45. </ol>
  46. </div>
  47. </div>
  48. <!-- /.row -->
  49.  
  50. </div>
  51.  
  52. includes/database.php
  53.  
  54. <?php
  55.  
  56. require_once('new_config.php');
  57.  
  58. class Database {
  59.  
  60. public $connection;
  61.  
  62. function __construct() {
  63. $this->open_db_connection();
  64. }
  65.  
  66. public function open_db_connection() {
  67.  
  68. //$this->connection = mysqli_connect(DB_HOST,DB_USER,DB_PASS,DB_NAME);
  69.  
  70. $this->connection = new mysqli(DB_HOST,DB_USER,DB_PASS,DB_NAME);
  71.  
  72. if($this->connection->connect_errno) {
  73. die('Database connection failed ' . $this->connection->connect_error);
  74. }
  75. }
  76.  
  77. public function query($sql) {
  78. $result = $this->connection->query($sql);
  79. $this->confirm_query($result);
  80. return $result;
  81. }
  82.  
  83. private function confirm_query($result) {
  84. if(!$result) {
  85. die('Query failed' . $this->connection->error);
  86. }
  87. }
  88.  
  89. public function escape_string($string) {
  90. $escaped_string = $this->connection->real_escape_string($string);
  91. return $escaped_string;
  92. }
  93.  
  94. public function the_insert_id() {
  95. return $this->connection->insert_id;
  96. }
  97. }
  98.  
  99. $database = new Database();
  100.  
  101.  
  102.  
  103.  
  104. ?>
  105.  
  106. includes/functions.php
  107.  
  108. <?php
  109.  
  110. function classAutoLoader($class) {
  111. $class = strtolower($class);
  112. $the_path = "includes/{$class}.php";
  113.  
  114. if(is_file($the_path) && !class_exists($class)) {
  115. include $the_path;
  116. }
  117.  
  118. // if(file_exists($the_path)) {
  119. // include($the_path);
  120. // require_once($the_path);
  121. // } else {
  122. // die("File named {$class}.php was not found.");
  123. // }
  124.  
  125. }
  126. function redirect($location) {
  127. header("Location: {$location}");
  128. }
  129.  
  130.  
  131. spl_autoload_register('classAutoLoader');
  132.  
  133.  
  134. ?>
  135.  
  136. includes/header.php
  137.  
  138. <?php require_once('init.php') ?>
  139. <?php ob_start(); ?>
  140.  
  141. <!DOCTYPE html>
  142. <html lang="en">
  143.  
  144. <head>
  145.  
  146. <meta charset="utf-8">
  147. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  148. <meta name="viewport" content="width=device-width, initial-scale=1">
  149. <meta name="description" content="">
  150. <meta name="author" content="">
  151.  
  152. <title>SB Admin - Bootstrap Admin Template</title>
  153.  
  154. <!-- Bootstrap Core CSS -->
  155. <link href="css/bootstrap.min.css" rel="stylesheet">
  156.  
  157. <!-- Custom CSS -->
  158. <link href="css/sb-admin.css" rel="stylesheet">
  159.  
  160. <!-- Custom Fonts -->
  161. <link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
  162.  
  163. <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
  164. <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
  165. <!--[if lt IE 9]>
  166. <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
  167. <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
  168. <![endif]-->
  169.  
  170. </head>
  171.  
  172. <body>
  173.  
  174. <div id="wrapper">
  175.  
  176. includes/init.php
  177.  
  178. <?php
  179.  
  180. require_once('functions.php');
  181. require_once('new_config.php');
  182. require_once('database.php');
  183. require_once('user.php');
  184. require_once('session.php');
  185. require_once('login.php');
  186.  
  187. ?>
  188.  
  189. includes/new_config.php
  190.  
  191. <?php
  192.  
  193. //Database Connection Constants
  194.  
  195. define('DB_HOST','localhost');
  196. define('DB_USER','root');
  197. define('DB_PASS','');
  198. define('DB_NAME','gallery_db');
  199.  
  200.  
  201.  
  202. ?>
  203.  
  204. includes/session.php
  205.  
  206. <?php
  207.  
  208. class Session {
  209.  
  210. private $signed_in = false;
  211. public $user_id;
  212.  
  213. function __construct() {
  214. session_start();
  215. $this->check_the_login();
  216. }
  217.  
  218. public function is_signed_in() {
  219. return $this->signed_in;
  220. }
  221.  
  222. public function login($user) {
  223. if($user) {
  224. $this->user_id = $_SESSION['user_id'] = $user->id;
  225. $this->signed_in = true;
  226. }
  227. }
  228.  
  229. public function logout($user) {
  230. unset($_SESSION['user_id']);
  231. unset($this->user_id);
  232. $this->signed_in = false;
  233. }
  234.  
  235. private function check_the_login() {
  236. if(isset($_SESSION['user_id'])) {
  237. $this->user_id = $_SESSION['user_id'];
  238. $this->signed_in = true;
  239. } else {
  240. unset($this->user_id);
  241. $this->signed_in = false;
  242. }
  243. }
  244. }
  245.  
  246. $session = new Session();
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253. ?>
  254.  
  255. includes/user.php
  256.  
  257. <?php
  258.  
  259. class User {
  260.  
  261. public $id;
  262. public $username;
  263. public $password;
  264. public $first_name;
  265. public $last_name;
  266.  
  267. public static function find_all_users() {
  268. return self::find_this_query("SELECT * FROM users");
  269.  
  270. }
  271.  
  272. public static function find_user_by_id($user_id){
  273. global $database;
  274. $the_result_array = self::find_this_query("SELECT * FROM users WHERE id = $user_id LIMIT 1");
  275.  
  276. return !empty($the_result_array) ? array_shift($the_result_array) : false;
  277.  
  278. }
  279.  
  280.  
  281. public static function find_this_query($sql) {
  282. global $database;
  283. $result_set = $database->query($sql);
  284. $the_object_array = array();
  285.  
  286. while($row = mysqli_fetch_array($result_set)) {
  287. $the_object_array[] = self::instantiation($row);
  288. }
  289. return $the_object_array;
  290. }
  291.  
  292. public static function verify_user($username, $password) {
  293. global $database;
  294. $username = $database->escape_string($username);
  295. $password = $database->escape_string($password);
  296.  
  297. $sql = "SELECT * FROM users WHERE ";
  298. $sql .= "username = '{$username}' ";
  299. $sql .= "AND password = '{$password}' ";
  300. $sql .= "LIMIT 1";
  301. //$sql = "SELECT * FROM users WHERE username = '{$username}' AND password = '{$password}' LIMIT 1";
  302.  
  303. $the_result_array = self::find_this_query($sql);
  304. return !empty($the_result_array) ? array_shift($the_result_array) : false;
  305.  
  306. }
  307.  
  308. public static function instantiation($the_record) {
  309. $the_object = new self;
  310.  
  311. // $the_object->id = $found_user['id'];
  312. // $the_object->username = $found_user['username'];
  313. // $the_object->password = $found_user['password'];
  314. // $the_object->first_name = $found_user['first_name'];
  315. // $the_object->last_name = $found_user['last_name'];
  316.  
  317. foreach ($the_record as $the_attribute => $value ) {
  318. if($the_object->has_the_attribute($the_attribute)) {
  319. $the_object->$the_attribute = $value;
  320. }
  321. }
  322. return $the_object;
  323. }
  324.  
  325. private function has_the_attribute($the_attribute) {
  326. $object_properties = get_object_vars($this);
  327. return array_key_exists($the_attribute, $object_properties);
  328. }
  329. }
  330.  
  331.  
  332.  
  333. ?>
  334.  
  335. index.php
  336.  
  337. <?php include("includes/header.php"); ?>
  338.  
  339. <?php if(!$session->is_signed_in()) {redirect("login.php");} ?>
  340.  
  341. <!-- Navigation -->
  342. <nav class="navbar navbar-inverse navbar-fixed-top" role="navigation">
  343. <!-- Brand and toggle get grouped for better mobile display -->
  344.  
  345. <?php include("includes/top_nav.php"); ?>
  346.  
  347.  
  348. <!-- Sidebar Menu Items - These collapse to the responsive navigation menu on small screens -->
  349. <?php include("includes/side_nav.php"); ?>
  350. <!-- /.navbar-collapse -->
  351. </nav>
  352.  
  353. <div id="page-wrapper">
  354.  
  355. <!-- /.container-fluid -->
  356. <?php include("includes/admin_content.php"); ?>
  357. </div>
  358. <!-- /#page-wrapper -->
  359.  
  360. <?php include("includes/footer.php"); ?>
  361.  
  362. login.php
  363.  
  364. <?php require_once('includes/header.php'); ?>
  365. <?php
  366.  
  367. if($session->is_signed_in()) {
  368. redirect("index.php");
  369. }
  370.  
  371. if(isset($_POST['submit'])) {
  372.  
  373. $username = trim($_POST['username']);
  374. $password = trim($_POST['password']);
  375. ///Method to check database user
  376.  
  377. $user_found = User::verify_user($username, $password);
  378.  
  379. if($user_found) {
  380. $session->login($user_found);
  381. redirect("index.php");
  382. } else {
  383. $the_message = "Your password or username are incorrect";
  384. }
  385. } else {
  386. $the_message = "";
  387. $username = "";
  388. $password = "";
  389. }
  390.  
  391.  
  392. ?>
  393.  
  394.  
  395. <div class="col-md-4 col-md-offset-3">
  396.  
  397. <h4 class="bg-danger"><?php echo $the_message; ?></h4>
  398.  
  399. <form id="login-id" action="" method="post">
  400. <div class="form-group">
  401. <label for="username">Username</label>
  402. <input type="text" class="form-control" name="username" value="<?php echo htmlentities($username); ?>" >
  403. </div>
  404. <div class="form-group">
  405. <label for="password">Password</label>
  406. <input type="password" class="form-control" name="password" value="<?php echo htmlentities($password); ?>">
  407.  
  408. </div>
  409. <div class="form-group">
  410. <input type="submit" name="submit" value="Submit" class="btn btn-primary">
  411. </div>
  412. </form>
  413.  
  414. </div>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement