├── 1.start ├── .htaccess ├── Controllers │ ├── HomeController.php │ └── UserController.php ├── Models │ └── User.php ├── README.md ├── Views │ ├── 404.php │ ├── home.php │ ├── includes │ │ ├── __footer.php │ │ └── __header.php │ ├── login.php │ ├── register.php │ └── results.php ├── assets │ └── css │ │ └── main.css ├── config │ ├── config.php │ └── init.php ├── database.sql └── index.php ├── 2.finish ├── .htaccess ├── Controllers │ ├── HomeController.php │ └── UserController.php ├── Models │ └── User.php ├── README.md ├── Views │ ├── 404.php │ ├── home.php │ ├── includes │ │ ├── __footer.php │ │ └── __header.php │ ├── login.php │ ├── register.php │ └── results.php ├── assets │ └── css │ │ └── main.css ├── class │ ├── Config.php │ ├── Controller.php │ ├── ControllerFactory.php │ ├── Router.php │ ├── Validation.php │ └── index.php ├── config │ ├── config.php │ └── init.php ├── database.sql └── index.php └── README.md /1.start/.htaccess: -------------------------------------------------------------------------------- 1 | Options +FollowSymLinks 2 | RewriteEngine On 3 | RewriteRule ^(.*)$ index.php [NC,L] -------------------------------------------------------------------------------- /1.start/Controllers/HomeController.php: -------------------------------------------------------------------------------- 1 | db = $db; 16 | 17 | } 18 | 19 | public function register() 20 | { 21 | unset($_SESSION['old_user']); 22 | $user = new User($this->db); 23 | 24 | if ($user->validate()) { 25 | $user->createUser(); 26 | $user->saveUser(); 27 | $_SESSION['msg'] = "Welcome, " . $_POST['name']; 28 | header("Location: home"); 29 | } else { 30 | $_SESSION['old_user'] = $_POST; 31 | header("Location: register"); 32 | 33 | } 34 | } 35 | 36 | public function login() 37 | { 38 | $email = $_POST['email']; 39 | $password = $_POST['password']; 40 | $user = new User($this->db); 41 | 42 | if ($user->loginUser($email, $password)) { 43 | $_SESSION['msg'] = "Welcome, " . $_SESSION['user']; 44 | header("Location: home"); 45 | } else { 46 | $_SESSION['msg'] = "Error loging in. Check your email and password"; 47 | header("Location: login"); 48 | } 49 | } 50 | 51 | public function findUser() 52 | { 53 | $user = new User($this->db); 54 | $result = $user->findUsers(); 55 | $_SESSION['result'] = $result; 56 | 57 | if (empty($result)) { 58 | $_SESSION['msg'] = "No results found"; 59 | header("Location: results"); 60 | } else { 61 | unset($_SESSION['msg']); 62 | header("Location: results"); 63 | } 64 | } 65 | 66 | public static function logout() 67 | { 68 | session_destroy(); 69 | header("Location: home"); 70 | } 71 | } -------------------------------------------------------------------------------- /1.start/Models/User.php: -------------------------------------------------------------------------------- 1 | db = $db; 16 | } 17 | 18 | public function createUser() 19 | { 20 | $this->email = $_POST['email']; 21 | $this->name = $_POST['name']; 22 | $this->password = $_POST['password_1']; 23 | } 24 | 25 | 26 | public function saveUser() 27 | { 28 | $hash = password_hash($this->password, PASSWORD_DEFAULT); 29 | 30 | $sql = ("insert into users (email, name, password) values (:email, :name, :password);"); 31 | $statement = $this->db->prepare($sql); 32 | $statement->execute([ 33 | "email" => $this->email, 34 | "name" => $this->name, 35 | "password" => $hash, 36 | ]); 37 | 38 | //TODO login registered user and redirect home 39 | 40 | } 41 | 42 | public function loginUser($email, $password) 43 | { 44 | 45 | $sql = ("select * from users where email = :email"); 46 | $statement = $this->db->prepare($sql); 47 | $statement->execute([ 48 | "email" => $email, 49 | ]); 50 | $result = $statement->fetch(PDO::FETCH_ASSOC); 51 | 52 | if ($result) { 53 | if (password_verify($password, $result['password'])) { 54 | $this->name = $result['name']; 55 | $this->email = $result['email']; 56 | $_SESSION['logged_in'] = true; 57 | $_SESSION['user'] = $this->name; 58 | $_SESSION['user_email'] = $this->email; 59 | return true; 60 | } 61 | } 62 | } 63 | 64 | 65 | /* 66 | * Find user by name or email 67 | * return array 68 | */ 69 | 70 | public function findUsers(): array 71 | { 72 | 73 | $searchTerm = $_POST['search']; 74 | 75 | if (strlen($searchTerm) < 1) return []; 76 | $sql = ("select email, name from users where email like :email or name like :name "); 77 | $statement = $this->db->prepare($sql); 78 | $statement->execute([ 79 | "email" => '%' . $searchTerm . '%', 80 | "name" => '%' . $searchTerm . '%', 81 | ]); 82 | 83 | return $statement->fetchAll(PDO::FETCH_ASSOC); 84 | 85 | } 86 | 87 | private function isRegistered($email) 88 | { 89 | $sql = ("select * from users where email = :email "); 90 | $statement = $this->db->prepare($sql); 91 | $statement->execute([ 92 | "email" => $email, 93 | 94 | ]); 95 | $result = $statement->fetch(PDO::FETCH_ASSOC); 96 | 97 | if ($result) return true; 98 | 99 | 100 | } 101 | 102 | 103 | public function validate() 104 | { 105 | 106 | unset($_SESSION['password_error']); 107 | unset($_SESSION['mail_error']); 108 | 109 | 110 | if ($this->isRegistered($_POST['email'])) { 111 | $_SESSION['mail_error'] = 'User already registered!'; 112 | return false; 113 | }; 114 | 115 | 116 | //Validate email 117 | 118 | 119 | //Check if mail is valid 120 | if (!$this->validateEmail($_POST['email'])) { 121 | $_SESSION['mail_error'] = 'Invalid email address'; 122 | return false; 123 | } 124 | 125 | //Check if both passwords match 126 | if ($_POST['password_1'] != $_POST['password_2']) { 127 | $_SESSION['password_error'] = 'Passwords doas not match'; 128 | return false; 129 | } 130 | 131 | //Check password lenth 132 | 133 | if (!$this->validatePassword($_POST['password_1'])) { 134 | $_SESSION['password_error'] = 'Passwords minimum lenghth is 5 characters'; 135 | return false; 136 | } 137 | 138 | return true; 139 | 140 | } 141 | 142 | private function validateEmail($email) 143 | { 144 | if (filter_var($email, FILTER_VALIDATE_EMAIL)) { 145 | return true; 146 | } else { 147 | return false; 148 | } 149 | } 150 | 151 | private function validatePassword($password) 152 | { 153 | 154 | //Set password minimum length 155 | if (strlen($password) < 5) { 156 | return false; 157 | } else return true; 158 | 159 | } 160 | 161 | } -------------------------------------------------------------------------------- /1.start/README.md: -------------------------------------------------------------------------------- 1 | login 2 | test@applicableprogramming.com 3 | test 4 | -------------------------------------------------------------------------------- /1.start/Views/404.php: -------------------------------------------------------------------------------- 1 |