Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Persist
- function persist($db) {
- try {
- if ($this->id) {
- $sqlSelect = "UPDATE user SET username=:username, password=:password WHERE id = " . $this->id;
- $sthSqlSelect = $db->prepare($sqlSelect);
- $result = $sthSqlSelect->execute([":username" => $this->username, ":password" => $this->password]);
- } else {
- $sqlSelect = "INSERT INTO user (username, password) VALUES (:username, :password)";
- $sthSqlSelect = $db->prepare($sqlSelect);
- $result = $sthSqlSelect->execute([":username" => $this->username, ":password" => $this->password]);
- if ($result)
- $this->id = (int) $db->lastInsertId();
- }
- return $result;
- } catch (PDOException $e) {
- throw new Exception("Username already exists.");
- }
- }
- // Iterador
- // Se resetea el iterador para empezar por el primero
- $matchday->getMatches()->resetIterator();
- // Se recorre la colección de partidas
- while ($match = $matchday->getMatches()->iterate()) {
- //
- }
- // Fetch All
- $sql = 'SELECT * FROM `match` WHERE id_matchday = :id_matchday';
- $sthSql = $db->prepare($sql);
- $sthSql->execute([':id_matchday' => $id]);
- $sthSql->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'Match');
- $result = $sthSql->fetchAll();
- return $result;
- // Get User
- static function getUserByCredentials($db, $username, $password) {
- $sqlSelect = 'SELECT * FROM user WHERE username=:username AND password=:password';
- $sthSqlSelect = $db->prepare($sqlSelect);
- $sthSqlSelect->execute([":username" => $username, ":password" => $password]);
- $sthSqlSelect->setFetchMode(PDO::FETCH_CLASS | PDO::FETCH_PROPS_LATE, 'User');
- $user = $sthSqlSelect->fetch();
- if ($user) {
- $matchdays = Matchday::getLeague($db);
- foreach ($matchdays as $matchday) {
- $user->getLeague()->add($matchday);
- }
- }
- return $user;
- }
- // DB in index.php
- try {
- $db = Database::getConnection();
- } catch (Exception $e) {
- $type = 'danger';
- $output = $e->getMessage();
- include('views/login.php');
- die();
- }
- // Database.php CLASS
- class Database {
- private $host = 'localhost';
- private $user = 'root';
- private $pass = '';
- private $dbname = 'sport';
- protected static $bd = null;
- private function __construct() {
- try {
- self::$bd = new PDO("mysql:host=$this->host;dbname=$this->dbname", $this->user, $this->pass);
- self::$bd->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
- } catch (PDOException $e) {
- switch($e->getCode()) {
- case 2002:
- $msg = "Can't connect to the database, try again later.";
- break;
- default:
- $msg = "Unknown error, try again later.";
- break;
- }
- throw new Exception($msg);
- }
- }
- public static function getConnection() {
- if (!self::$bd) {
- new Database();
- }
- return self::$bd;
- }
- }
- // Auth.php CLASS
- class Auth {
- const SESSION_NAME = 'user';
- protected static $auth = null;
- function __construct() {
- if (session_status() === PHP_SESSION_NONE) {
- session_start();
- }
- }
- public static function getAuth() {
- if (!self::$auth) {
- self::$auth = new Auth();
- }
- return self::$auth;
- }
- function check() {
- return isset ($_SESSION[self::SESSION_NAME]);
- }
- function login($user) {
- $_SESSION[self::SESSION_NAME] = $user;
- }
- function loggedUser() {
- return $_SESSION[self::SESSION_NAME];
- }
- function logout() {
- session_unset();
- session_destroy();
- }
- }
- // Example of view
- <!DOCTYPE html>
- <html>
- <head>
- <meta charset="UTF-8">
- <title>Sports - Login</title>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css" integrity="sha384-rHyoN1iRsVXV4nD0JutlnGaslCJuC7uwjduW9SVrLvRYooPp2bWYgmgJQIXwl/Sp" crossorigin="anonymous">
- <style>
- .link-lookalike {
- background: none;
- border: none;
- color: blue;
- text-decoration: underline;
- cursor: pointer;
- }
- </style>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
- </head>
- <body>
- <div class="container">
- <div class="row main col-sm-4 col-md-offset-4">
- <div class="panel-heading">
- <div class="panel-title text-left">
- <h1 class="title">Login to access</h1>
- <hr />
- </div>
- </div>
- <?php
- if (isset($output)) {
- ?>
- <div class="alert alert-<?php echo $type ?> text-center">
- <?php echo $output ?>
- </div>
- <?php
- }
- ?>
- <form action="index.php" name="login-form" method="POST">
- <span class="input-group text-center">
- <input type="text" class="form-control" placeholder="Username" name="username" required autofocus />
- <span class="input-group-btn" style="width:0px; height:0px;"></span>
- <input type="password" class="form-control" placeholder="Password" name="password" required />
- <span class="input-group-btn">
- <input class="btn btn-primary" type="submit" value="Login" name='login'>
- </span>
- </span>
- </form>
- <form action="index.php" name="login-form" method="POST">
- <p><h6>Don't have an account? <input class="link-lookalike" value="Register here" type="submit" name="register-form"></h6></p>
- </form>
- </div>
- </div>
- </body>
- </html>
- // Intro example controlador
- <?php
- // Importación de clases requeridas
- require_once('class/Database.php');
- require_once('class/Collection.php');
- // Arrancamos la sesión desde la clase singleton Auth
- $auth = Auth::getAuth();
- // Se intenta la conexión con la BD.
- // En caso de error se le mostrará al usuario
- // la vista login.php con un mensaje.
- try {
- $db = Database::getConnection();
- } catch (Exception $e) {
- $type = 'danger';
- $output = $e->getMessage();
- include('views/login.php');
- die();
- }
- // Controlador de la aplicación
- if ($auth->check()) {
- } else {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement