Advertisement
MikesBarto2002

My autoload doesn't work

Aug 13th, 2013
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.86 KB | None | 0 0
  1. /////  FOLDER STRUCTURE  /////
  2.  
  3. /genesis
  4.     /genesis
  5.         /model
  6.             login.php (namespace Genesis\Model; class Login)
  7.         /view
  8.             login.php
  9.         /controller
  10.             login.php (namespace Genesis\Controller; class Login)
  11.         /lib
  12.             index.php
  13.             autoload.php
  14.             dbconnect.php
  15.     index.php
  16.     config.php
  17.  
  18. /////  /GENESIS/GENESIS/INDEX.PHP  /////
  19.  
  20. <?php
  21.     session_start();
  22.     include_once('lib/index.php');
  23. ?>
  24.  
  25. <!DOCTYPE html>
  26. <html class="no-js">
  27.     <head>
  28.         <meta charset="utf-8">
  29.         <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  30.         <title>Genesis</title>
  31.         <meta name="description" content="">
  32.         <meta name="viewport" content="width=device-width">
  33.  
  34.         <link rel="stylesheet" href="/genesis/css/stylesheets/login.css">
  35.         <link href='http://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic,700italic' rel='stylesheet' type='text/css'>
  36.  
  37.         <script src="/genesis/js/vendor/modernizr-2.6.2-respond-1.1.0.min.js"></script>
  38.     </head>
  39.     <body>
  40.  
  41.         <div class="outer">
  42.             <div class="container">
  43.                 <div class="inner">
  44.                     <?php
  45.                         if (isset($_POST['submit'])) {
  46.                             $login = new \Genesis\Controller\Login($dbh);
  47.                             var_dump($login->validateLogin($_POST['username'], $_POST['password']));
  48.                         } else {
  49.                             $login = new \Genesis\Controller\Login(NULL);
  50.                             $login = $login->viewLogin();  
  51.                         }
  52.                     ?>
  53.                 </div>
  54.             </div>
  55.         </div>
  56.  
  57.         <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
  58.         <script>window.jQuery || document.write('<script src="/genesis/js/vendor/jquery-1.10.1.min.js"><\/script>')</script>
  59.  
  60.         <script src="/genesis/js/plugins.js"></script>
  61.         <script src="/genesis/js/main.js"></script>
  62.  
  63.         <script>
  64.             var _gaq=[['_setAccount','UA-XXXXX-X'],['_trackPageview']];
  65.             (function(d,t){var g=d.createElement(t),s=d.getElementsByTagName(t)[0];
  66.             g.src='//www.google-analytics.com/ga.js';
  67.             s.parentNode.insertBefore(g,s)}(document,'script'));
  68.         </script>
  69.     </body>
  70. </html>
  71.  
  72. /////  /GENESIS/GENESIS/CONFIG.PHP  /////
  73.  
  74. <?php
  75.  
  76.     define('ROOT_DIR', dirname(__FILE__));
  77.     define('HOST', 'localhost');
  78.     define('DATABASE', 'genesis');
  79.     define('USERNAME', 'username');
  80.     define('PASSWORD', 'password');
  81.  
  82. ?>
  83.  
  84. /////  /GENESIS/GENESIS/LIB/INDEX.PHP  /////
  85.  
  86. <?php
  87.  
  88.     require($_SERVER['DOCUMENT_ROOT'] . '/genesis/config.php');
  89.     require('dbconnect.php');
  90.     require('autoload.php');
  91.  
  92. ?>
  93.  
  94. /////  /GENESIS/GENESIS/LIB/AUTOLOAD.PHP  /////
  95.  
  96. <?php
  97.  
  98.     function autoload($className) {
  99.         $className = ltrim($className, '\\');
  100.         $fileName  = '';
  101.         $namespace = '';
  102.         if ($lastNsPos = strripos($className, '\\')) {
  103.             $namespace = substr($className, 0, $lastNsPos);
  104.             $className = substr($className, $lastNsPos + 1);
  105.             $fileName  = str_replace('\\', DIRECTORY_SEPARATOR, $namespace) . DIRECTORY_SEPARATOR;
  106.         }
  107.         $fileName .= str_replace('_', DIRECTORY_SEPARATOR, $className) . '.php';
  108.  
  109.         require $fileName;
  110.  
  111.     }
  112.  
  113. ?>
  114.  
  115. /////  /GENESIS/GENESIS/LIB/DBCONNECT.PHP  /////
  116.  
  117. <?php
  118.  
  119. $dsn = 'mysql:dbname=' . DATABASE . ';host=' . HOST;
  120. $user = USERNAME;
  121. $password = PASSWORD;
  122.  
  123. try {
  124.     $dbh = new PDO($dsn, $user, $password);
  125. } catch (PDOException $e) {
  126.     echo 'Connection failed: ' . $e->getMessage();
  127.     die();
  128. }
  129.  
  130. ?>
  131.  
  132. /////  /GENESIS/GENESIS/CONTROLLER/LOGIN.PHP  /////
  133.  
  134. <?php
  135.  
  136. namespace Genesis\Controller;
  137.  
  138. /**
  139. *
  140. */
  141. class Login {
  142.  
  143.     public $dbh;
  144.     public $username;
  145.     public $password;
  146.    
  147.     function __construct($dbh) {
  148.  
  149.         $this->dbh = $dbh;
  150.  
  151.     }
  152.  
  153.     public function viewLogin() {
  154.  
  155.         include(ROOT_DIR . '/view/login.php');
  156.  
  157.     }
  158.  
  159.     public function validateLogin($username, $password) {
  160.  
  161.         $login = new \Genesis\Model\Login($this->dbh);
  162.         $login = $login->getUser($username, $password);
  163.  
  164.         return $login;
  165.  
  166.     }
  167.  
  168. }
  169.  
  170. ?>
  171.  
  172. /////  /GENESIS/GENESIS/MODEL/LOGIN.PHP  /////
  173.  
  174. <?php
  175.  
  176. namespace \Genesis\Model;
  177.  
  178. /**
  179. *
  180. */
  181. class Login {
  182.  
  183.     private $dbh;
  184.     public $result;
  185.    
  186.     function __construct($dbh) {
  187.        
  188.         $this->dbh = $dbh;
  189.         $this->result = array();
  190.  
  191.     }
  192.  
  193.     public function getUser($username, $password) {
  194.  
  195.         try {
  196.             $sth = $this->dbh->prepare('SELECT id, firstName, lastName, username FROM users WHERE username = :username AND password = :password');
  197.             $sth->bindParam(':username', $username, PDO::PARAM_STR);
  198.             $sth->bindParam(':password', $password, PDO::PARAM_STR);
  199.             $sth->execute();
  200.             $this->result = $sth->fetch(PDO::FETCH_ASSOC);
  201.         } catch (Exception $e) {
  202.             echo 'Getting user info failed: ' . $e->getMessage();
  203.             die();
  204.         }
  205.        
  206.         return $this->result;
  207.  
  208.     }
  209.  
  210. }
  211.  
  212. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement