├── simpleusers ├── su.inc.php ├── config.inc.php ├── install.php └── users.obj.php ├── logout.php ├── tables.sql ├── deleteuser.php ├── removeinfo.php ├── LICENSE.txt ├── csrf.php ├── login.php ├── newuser.php ├── changepassword.php ├── users.php ├── README.txt ├── userinfo.php ├── README.markdown ├── index.php └── reference.html /simpleusers/su.inc.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /simpleusers/config.inc.php: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /logout.php: -------------------------------------------------------------------------------- 1 | logoutUser(); 16 | header("Location: index.php"); 17 | 18 | ?> -------------------------------------------------------------------------------- /tables.sql: -------------------------------------------------------------------------------- 1 | CREATE TABLE IF NOT EXISTS `users` ( 2 | `userId` int(11) NOT NULL auto_increment, 3 | `uUsername` varchar(128) NOT NULL, 4 | `uPassword` varchar(255) NOT NULL, 5 | `uActivity` datetime NOT NULL, 6 | `uCreated` datetime NOT NULL, 7 | PRIMARY KEY (`userId`), 8 | UNIQUE KEY `uUsername` (`uUsername`) 9 | ) ENGINE=MyISAM AUTO_INCREMENT=1 ; 10 | 11 | CREATE TABLE IF NOT EXISTS `users_information` ( 12 | `userId` int(11) NOT NULL, 13 | `infoKey` varchar(128) NOT NULL, 14 | `InfoValue` text NOT NULL, 15 | KEY `userId` (`userId`) 16 | ) ENGINE=MyISAM; 17 | -------------------------------------------------------------------------------- /deleteuser.php: -------------------------------------------------------------------------------- 1 | logged_in ) 17 | { 18 | header("Location: login.php"); 19 | exit; 20 | } 21 | 22 | // If the user is logged in, we can safely proceed. 23 | $userId = $_GET["userId"]; 24 | 25 | //Delete the user (plain and simple) 26 | $SimpleUsers->deleteUser($userId); 27 | header("Location: users.php"); 28 | 29 | ?> -------------------------------------------------------------------------------- /removeinfo.php: -------------------------------------------------------------------------------- 1 | logged_in ) 17 | { 18 | header("Location: login.php"); 19 | exit; 20 | } 21 | 22 | // If the user is logged in, we can safely proceed. 23 | 24 | $userId = $_GET["userId"]; 25 | $db_key = urldecode($_GET["db_key"]); 26 | 27 | $SimpleUsers->removeInfo($db_key, $userId); 28 | header("Location: users.php"); 29 | exit; 30 | 31 | 32 | ?> -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) [year] [fullname] 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /csrf.php: -------------------------------------------------------------------------------- 1 | validateToken(); 19 | 20 | if($csrf) 21 | { 22 | // Proceed with the code to be executed if data hasn't been tampered 23 | } 24 | 25 | } // Validation end 26 | 27 | ?> 28 | 29 | 30 |
31 |76 | 77 | CSRF successfully validated 78 | 79 | CSRF didn't validate - data probably have been tampered with! 80 | 81 |
82 | 83 | 84 | 95 | 96 | 97 | -------------------------------------------------------------------------------- /login.php: -------------------------------------------------------------------------------- 1 | loginUser($_POST["username"], $_POST["password"]); 20 | if(!$res) 21 | $error = "You supplied the wrong credentials."; 22 | else 23 | { 24 | header("Location: users.php"); 25 | exit; 26 | } 27 | 28 | } // Validation end 29 | 30 | ?> 31 | 32 | 33 | 34 |79 | 80 |
81 | 82 | 83 | 99 | 100 | 101 | 102 | -------------------------------------------------------------------------------- /newuser.php: -------------------------------------------------------------------------------- 1 | createUser($_POST["username"], $_POST["password"]); 25 | 26 | if(!$res) 27 | $error = "Username already taken."; 28 | else 29 | { 30 | header("Location: users.php"); 31 | exit; 32 | } 33 | } 34 | 35 | } // Validation end 36 | 37 | ?> 38 | 39 | 40 | 41 |86 | 87 |
88 | 89 | 90 | 106 | 107 | 108 | -------------------------------------------------------------------------------- /changepassword.php: -------------------------------------------------------------------------------- 1 | logged_in ) 17 | { 18 | header("Location: login.php"); 19 | exit; 20 | } 21 | 22 | // If the user is logged in, we can safely proceed. 23 | 24 | 25 | $userId = $_GET["userId"]; 26 | 27 | $user = $SimpleUsers->getSingleUser($userId); 28 | if( !$user ) 29 | die("The user could not be found..."); 30 | 31 | 32 | // Validation of input 33 | if( isset($_POST["password"]) ) 34 | { 35 | if( empty($_POST["password"]) ) 36 | $error = "You have to choose a password"; 37 | else 38 | { 39 | // Input validation is ok, set the password and then redirect 40 | $SimpleUsers->setPassword($_POST["password"], $user["userId"]); 41 | header("Location: users.php"); 42 | exit; 43 | } 44 | 45 | } // Validation end 46 | 47 | ?> 48 | 49 | 50 | 51 |96 | 97 |
98 | 99 | 100 | 112 | 113 | 114 | -------------------------------------------------------------------------------- /users.php: -------------------------------------------------------------------------------- 1 | logged_in ) 17 | { 18 | header("Location: login.php"); 19 | exit; 20 | } 21 | 22 | // If the user is logged in, we can safely proceed. 23 | $users = $SimpleUsers->getUsers(); 24 | 25 | ?> 26 | 27 | 28 | 29 || Username | 76 |Last activity | 77 |Created | 78 |79 | |
|---|---|---|---|
| 84 | Create new user | Logout 85 | | 86 ||||
| 92 | | 93 | | 94 | | ">Delete | ">User info | ">Change password | 95 |
87 | 88 |
89 |90 | 91 |
92 | 93 | 94 | 130 | 131 |116 | 117 |
118 | 119 | 120 | 146 | 147 | 148 | -------------------------------------------------------------------------------- /README.markdown: -------------------------------------------------------------------------------- 1 | SimpleUsers v2.0 2 | ================ 3 | 4 | 5 | ##WARNING: 6 | This is a simple user management script that is currently only available because it's in use some places. If possible, find other solutions for your user management handling. 7 | 8 | Requirements 9 | ------------ 10 | 11 | SimpleUsers needs PHP 5.5, MySQL (no special version needed) and the MySQL Improved extension (http://php.net/mysqli). 12 | 13 | SKILLS 14 | ------ 15 | Basic knowledge of PHP is required. Assuming that you know basic PHP, you most likely do know your way around HTML. 16 | 17 | IS IT SECURE? 18 | ------------- 19 | SimpleUsers doesn't store password in clear text which makes the application secure for you AND your users. 20 | Every user gets a unique 128 character long salt prepended to their chosen password, 21 | which then gets stored in the database as a SHA1 hash. The hashing is one way. 22 | Additionally, to avoid SQL injection attacks, SimpleUsers uses prepared statements, when communicating with the database. 23 | 24 | CSRF (Cross-site request forgery) protection has been integrated - the methods doesn't require user is logged as CSRF also could be prefered in other situations.54 | Software 55 |
56 |57 | SimpleUsers needs PHP 5, MySQL (no special version needed) and the MySQL Improved extension (http://php.net/mysqli). 58 |
59 | 60 | 61 |62 | Skills 63 |
64 |65 | Basic knowledge of PHP is required. Assuming that you know basic PHP, you most likely do know your way around HTML. 66 |
67 | 68 |69 | Is it secure? 70 |
71 |
72 | SimpleUsers doesn't store password in clear text which makes the application secure for you AND your users.
73 | Every user gets a unique 128 character long salt prepended to their chosen password,
74 | which then gets stored in the database as a SHA1 hash. The hashing is one way.
75 | Additionally, to avoid SQL injection attacks, SimpleUsers uses prepared statements, when communicating with the database.
76 |
79 | CSRF (Cross-site request forgery) protection has been integrated - the methods doesn't require user is logged as CSRF also could be prefered in other situations.
80 | The CSRF protection works only with POST data (as GET data already is too easy to manipulate).
81 |
84 | Please be aware that SimpleUsers isn't a fullblown application; it's not safer than what you use SimpleUsers in.
85 | When developing your application, think of SimpleUsers as an aid in adding user management to your application.
86 |
90 | It's simple.
91 | What you need to do, is to edit the configuration file, located at simpleusers/config.inc.php, then run the install script which is located at simpleusers/install.php
92 |
97 | Include and use 98 |
99 |100 | You simply make sure that your sessions has been started (http://php.net/session_start), include the su.inc.file, located at simpleusers/su.inc.php 101 | and finally, you create an instance of the SimpleUsers object - now you're ready to go. 102 |
103 | 104 |105 | Sample files are included with this package 106 |
107 |
108 | The following files are simply sample files and should not be used as useradministration `as is`.
109 | The sample files are only for reference and learning purposes, so you can build SimpleUsers into your own application.
110 |
123 | Not all the methods available in SimpleUsers is covered in these files.
124 | A full reference can be found in reference.html.
125 |
130 | Please make sure you configured the config file! 131 |
132 |
133 | It might seem like a trivial thing to mention, but when things doesn't work as expected, the most likely cause is a misconfigured configuration file.
134 | The config file is located at simpleusers/config.inc.php and is fairly simple to understand.
135 |
139 | Did you run the install script? 140 |
141 |
142 | If not, you would probably want to; this will create the tables in your database needed for this to work.
143 | The install script is located at simpleusers/install.php.
144 |
146 | If you did, you should remove or rename the file or something similar, to avoid unwelcome users having access to it. 147 |
148 | 149 | 150 | -------------------------------------------------------------------------------- /simpleusers/users.obj.php: -------------------------------------------------------------------------------- 1 | Please add `session_start();` initially in your file before any output."); 32 | } 33 | 34 | $this->mysqli = new mysqli($GLOBALS["mysql_hostname"], $GLOBALS["mysql_username"], $GLOBALS["mysql_password"], $GLOBALS["mysql_database"]); 35 | if ($this->mysqli->connect_error) { 36 | throw new Exception("MySQL connection could not be established: " . $this->mysqli->connect_error); 37 | } 38 | 39 | $this->_validateUser(); 40 | $this->_populateUserdata(); 41 | $this->_updateActivity(); 42 | } 43 | 44 | /** 45 | * Returns a (int)user id, if the user was created succesfully. 46 | * If not, it returns (bool)false. 47 | * 48 | * @param $username 49 | * @param $password 50 | * @return bool|int 51 | * @throws Exception 52 | */ 53 | 54 | public function createUser($username, $password) 55 | { 56 | $password = password_hash($password, PASSWORD_DEFAULT); 57 | 58 | $sql = "INSERT INTO users VALUES (NULL, ?, ?, NOW(), NOW())"; 59 | if (!$this->stmt = $this->mysqli->prepare($sql)) { 60 | throw new Exception("MySQL Prepare statement failed: " . $this->mysqli->error); 61 | } 62 | 63 | $this->stmt->bind_param("ss", $username, $password); 64 | if ($this->stmt->execute()) { 65 | return $this->stmt->insert_id; 66 | } 67 | 68 | return false; 69 | } 70 | 71 | /** 72 | * Pairs up username and password as registrered in the database. 73 | * If the username and password is correct, it will return (int)user id of 74 | * the user which credentials has been passed and set the session, for 75 | * use by the user validating. 76 | * 77 | * @param $username 78 | * @param $password 79 | * @return bool 80 | * @throws Exception 81 | */ 82 | 83 | public function loginUser($username, $password) 84 | { 85 | $sql = "SELECT userId, uPassword FROM users WHERE uUsername=? LIMIT 1"; 86 | if (!$this->stmt = $this->mysqli->prepare($sql)) { 87 | throw new Exception("MySQL Prepare statement failed: " . $this->mysqli->error); 88 | } 89 | 90 | $this->stmt->bind_param("s", $username); 91 | $this->stmt->execute(); 92 | $this->stmt->store_result(); 93 | 94 | if ($this->stmt->num_rows == 0) { 95 | return false; 96 | } 97 | 98 | $this->stmt->bind_result($userId, $userPassword); 99 | $this->stmt->fetch(); 100 | 101 | 102 | if(!password_verify($password, $userPassword)) { 103 | return false; 104 | } 105 | 106 | $_SESSION[$this->sessionName]["userId"] = $userId; 107 | $this->logged_in = true; 108 | 109 | return $userId; 110 | } 111 | 112 | /** 113 | * Sets an information pair, consisting of a key name and that keys value. 114 | * The information can be retrieved with this objects getInfo() method. 115 | * 116 | * @param $key 117 | * @param $value 118 | * @param null $userId Can be used if administrative control is needed 119 | * @return bool 120 | * @throws Exception 121 | */ 122 | 123 | public function setInfo($key, $value, $userId = null) 124 | { 125 | if ($userId == null) { 126 | if (!$this->logged_in) 127 | return false; 128 | } 129 | 130 | $reservedKeys = array("userId", "uUsername", "uActivity", "uCreated", "uLevel"); 131 | if (in_array($key, $reservedKeys)) 132 | throw new Exception("User information key \"" . $key . "\" is reserved for internal use!"); 133 | 134 | if ($userId == null) 135 | $userId = $_SESSION[$this->sessionName]["userId"]; 136 | 137 | if ($this->getInfo($key, $userId)) { 138 | $sql = "UPDATE users_information SET infoValue=? WHERE infoKey=? AND userId=? LIMIT 1"; 139 | if (!$this->stmt = $this->mysqli->prepare($sql)) { 140 | throw new Exception("MySQL Prepare statement failed: " . $this->mysqli->error); 141 | } 142 | 143 | $this->stmt->bind_param("ssi", $value, $key, $userId); 144 | $this->stmt->execute(); 145 | } else { 146 | $sql = "INSERT INTO users_information VALUES (?, ?, ?)"; 147 | if (!$this->stmt = $this->mysqli->prepare($sql)) { 148 | throw new Exception("MySQL Prepare statement failed: " . $this->mysqli->error); 149 | } 150 | 151 | $this->stmt->bind_param("iss", $userId, $key, $value); 152 | $this->stmt->execute(); 153 | } 154 | 155 | return true; 156 | } 157 | 158 | /** 159 | * Use this function to retrieve user information attached to a certain user 160 | * that has been set by using this objects setInfo() method. 161 | * 162 | * @param $key 163 | * @param null $userId Can be used if administrative control is needed 164 | * @return bool|string 165 | * @throws Exception 166 | */ 167 | 168 | public function getInfo($key, $userId = null) 169 | { 170 | 171 | if ($userId == null) { 172 | if (!$this->logged_in) 173 | return false; 174 | 175 | $userId = $_SESSION[$this->sessionName]["userId"]; 176 | } 177 | 178 | $sql = "SELECT infoValue FROM users_information WHERE userId=? AND infoKey=? LIMIT 1"; 179 | if (!$this->stmt = $this->mysqli->prepare($sql)) { 180 | throw new Exception("MySQL Prepare statement failed: " . $this->mysqli->error); 181 | } 182 | 183 | $this->stmt->bind_param("is", $userId, $key); 184 | $this->stmt->execute(); 185 | $this->stmt->store_result(); 186 | 187 | if ($this->stmt->num_rows == 0) { 188 | return ""; 189 | } 190 | 191 | $this->stmt->bind_result($value); 192 | $this->stmt->fetch(); 193 | 194 | return $value; 195 | 196 | } 197 | 198 | /** 199 | * Use this function to permanently remove information attached to a certain user 200 | * that has been set by using this objects setInfo() method. 201 | * 202 | * @param $key 203 | * @param null $userId Can be used if administrative control is needed 204 | * @return bool 205 | * @throws Exception 206 | */ 207 | 208 | public function removeInfo($key, $userId = null) 209 | { 210 | 211 | if ($userId == null) { 212 | if (!$this->logged_in) { 213 | return false; 214 | } 215 | 216 | $userId = $_SESSION[$this->sessionName]["userId"]; 217 | } 218 | 219 | $sql = "DELETE FROM users_information WHERE userId=? AND infoKey=? LIMIT 1"; 220 | if (!$this->stmt = $this->mysqli->prepare($sql)) { 221 | throw new Exception("MySQL Prepare statement failed: " . $this->mysqli->error); 222 | } 223 | 224 | $this->stmt->bind_param("is", $userId, $key); 225 | $this->stmt->execute(); 226 | 227 | if ($this->stmt->affected_rows > 0) { 228 | return true; 229 | } 230 | 231 | return false; 232 | } 233 | 234 | 235 | /** 236 | * Use this function to retrieve all user information attached to a certain user 237 | * that has been set by using this objects setInfo() method into an array. 238 | * 239 | * @param null $userId 240 | * @return array|bool 241 | * @throws Exception 242 | */ 243 | 244 | public function getInfoArray($userId = null) 245 | { 246 | if ($userId == null) { 247 | 248 | if (!$this->logged_in) { 249 | return false; 250 | } 251 | 252 | $userId = $_SESSION[$this->sessionName]["userId"]; 253 | } 254 | 255 | $sql = "SELECT infoKey, infoValue FROM users_information WHERE userId=? ORDER BY infoKey ASC"; 256 | if (!$this->stmt = $this->mysqli->prepare($sql)) { 257 | throw new Exception("MySQL Prepare statement failed: " . $this->mysqli->error); 258 | } 259 | 260 | $this->stmt->bind_param("i", $userId); 261 | $this->stmt->execute(); 262 | $this->stmt->store_result(); 263 | 264 | $userInfo = array(); 265 | if ($this->stmt->num_rows > 0) { 266 | $this->stmt->bind_result($key, $value); 267 | while ($this->stmt->fetch()) { 268 | $userInfo[$key] = $value; 269 | } 270 | } 271 | 272 | $user = $this->getSingleUser($userId); 273 | $userInfo = array_merge($userInfo, $user); 274 | asort($userInfo); 275 | 276 | return $userInfo; 277 | } 278 | 279 | /** 280 | * Logout the active user, unsetting the userId session. 281 | * This is a void function 282 | */ 283 | 284 | public function logoutUser() 285 | { 286 | if (isset($_SESSION[$this->sessionName])) { 287 | unset($_SESSION[$this->sessionName]); 288 | } 289 | 290 | $this->logged_in = false; 291 | } 292 | 293 | /** 294 | * Update the users password with this function. 295 | * Generates a new salt and a sets the users password with the given parameter 296 | * 297 | * @param $password 298 | * @param null $userId Can be used if administrative control is needed 299 | * @return bool 300 | * @throws Exception 301 | */ 302 | 303 | public function setPassword($password, $userId = null) 304 | { 305 | 306 | if ($userId == null) { 307 | $userId = $_SESSION[$this->sessionName]["userId"]; 308 | } 309 | 310 | $password = password_hash($password, PASSWORD_DEFAULT); 311 | 312 | $sql = "UPDATE users SET uPassword=? WHERE userId=? LIMIT 1"; 313 | if (!$this->stmt = $this->mysqli->prepare($sql)) { 314 | throw new Exception("MySQL Prepare statement failed: " . $this->mysqli->error); 315 | } 316 | 317 | $this->stmt->bind_param("si", $password,$userId); 318 | return $this->stmt->execute(); 319 | } 320 | 321 | /** 322 | * Returns an array with each user in the database. 323 | * 324 | * @return array 325 | */ 326 | 327 | public function getUsers() 328 | { 329 | 330 | $sql = "SELECT DISTINCT userId, uUsername, uActivity, uCreated FROM users ORDER BY uUsername ASC"; 331 | 332 | if (!$this->stmt = $this->mysqli->prepare($sql)) { 333 | throw new Exception("MySQL Prepare statement failed: " . $this->mysqli->error); 334 | } 335 | 336 | $this->stmt->execute(); 337 | $this->stmt->store_result(); 338 | 339 | if ($this->stmt->num_rows == 0) { 340 | return array(); 341 | } 342 | 343 | $this->stmt->bind_result($userId, $username, $activity, $created); 344 | 345 | $users = array(); 346 | 347 | $i = 0; 348 | while ($this->stmt->fetch()) { 349 | $users[$i]["userId"] = $userId; 350 | $users[$i]["uUsername"] = $username; 351 | $users[$i]["uActivity"] = $activity; 352 | $users[$i]["uCreated"] = $created; 353 | 354 | $i++; 355 | } 356 | 357 | return $users; 358 | 359 | } 360 | 361 | /** 362 | * Gets the basic info for a single user based on the userId 363 | * 364 | * @param null $userId Can be used if administrative control is needed 365 | * @return bool 366 | * @throws Exception 367 | */ 368 | 369 | public function getSingleUser($userId = null) 370 | { 371 | 372 | if ($userId == null) { 373 | $userId = $_SESSION[$this->sessionName]["userId"]; 374 | } 375 | 376 | $sql = "SELECT userId, uUsername, uActivity, uCreated FROM users WHERE userId=? LIMIT 1"; 377 | if (!$this->stmt = $this->mysqli->prepare($sql)) { 378 | throw new Exception("MySQL Prepare statement failed: " . $this->mysqli->error); 379 | } 380 | 381 | $this->stmt->bind_param("i", $userId); 382 | $this->stmt->execute(); 383 | $this->stmt->store_result(); 384 | 385 | if ($this->stmt->num_rows == 0) { 386 | return false; 387 | } 388 | 389 | $this->stmt->bind_result($userId, $username, $activity, $created); 390 | $this->stmt->fetch(); 391 | 392 | $user["userId"] = $userId; 393 | $user["uUsername"] = $username; 394 | $user["uActivity"] = $activity; 395 | $user["uCreated"] = $created; 396 | 397 | return $user; 398 | 399 | } 400 | 401 | /** 402 | * Deletes all information regarding a user. 403 | * This is a void function. 404 | * 405 | * @param $userId 406 | * @throws Exception 407 | */ 408 | 409 | public function deleteUser($userId) 410 | { 411 | $sql = "DELETE FROM users WHERE userId=?"; 412 | if (!$this->stmt = $this->mysqli->prepare($sql)) { 413 | throw new Exception("MySQL Prepare statement failed: " . $this->mysqli->error); 414 | } 415 | 416 | $this->stmt->bind_param("i", $userId); 417 | $this->stmt->execute(); 418 | 419 | $sql = "DELETE FROM users_information WHERE userId=?"; 420 | if (!$this->stmt = $this->mysqli->prepare($sql)) { 421 | throw new Exception("MySQL Prepare statement failed: " . $this->mysqli->error); 422 | } 423 | 424 | $this->stmt->bind_param("i", $userId); 425 | $this->stmt->execute(); 426 | } 427 | 428 | /** 429 | * Returns a hidden input field with a unique token value 430 | * for CSRF to be used with post data. 431 | * The token is saved in a session for later validation. 432 | * 433 | * @param bool $xhtml 434 | * @return string 435 | */ 436 | 437 | public function getToken($xhtml = true) 438 | { 439 | $token = uniqid($this->sessionName, true); 440 | $name = "token_" . md5($token); 441 | 442 | $_SESSION[$this->sessionName]["csrf_name"] = $name; 443 | $_SESSION[$this->sessionName]["csrf_token"] = $token; 444 | 445 | $string = "sessionName]["csrf_name"]; 464 | $token = $_SESSION[$this->sessionName]["csrf_token"]; 465 | unset($_SESSION[$this->sessionName]["csrf_token"]); 466 | unset($_SESSION[$this->sessionName]["csrf_name"]); 467 | 468 | if ($_POST[$name] == $token) 469 | return true; 470 | 471 | return false; 472 | } 473 | 474 | /** 475 | * This function updates the users last activity time 476 | * This is a void function. 477 | */ 478 | 479 | private function _updateActivity() 480 | { 481 | if (!$this->logged_in) { 482 | return; 483 | } 484 | 485 | $userId = $_SESSION[$this->sessionName]["userId"]; 486 | 487 | $sql = "UPDATE users SET uActivity=NOW() WHERE userId=? LIMIT 1"; 488 | if (!$this->stmt = $this->mysqli->prepare($sql)) { 489 | throw new Exception("MySQL Prepare statement failed: " . $this->mysqli->error); 490 | } 491 | 492 | $this->stmt->bind_param("i", $userId); 493 | $this->stmt->execute(); 494 | 495 | } 496 | 497 | /** 498 | * Validates if the user is logged in or not. 499 | * This is a void function. 500 | */ 501 | 502 | private function _validateUser() 503 | { 504 | if (!isset($_SESSION[$this->sessionName]["userId"])) { 505 | return; 506 | } 507 | 508 | if (!$this->_validateUserId()) { 509 | return; 510 | } 511 | 512 | $this->logged_in = true; 513 | } 514 | 515 | /** 516 | * Validates if the user id, in the session is still valid. 517 | * 518 | * @return bool 519 | * @throws Exception 520 | */ 521 | 522 | private function _validateUserId() 523 | { 524 | $userId = $_SESSION[$this->sessionName]["userId"]; 525 | 526 | $sql = "SELECT userId FROM users WHERE userId=? LIMIT 1"; 527 | if (!$this->stmt = $this->mysqli->prepare($sql)) { 528 | throw new Exception("MySQL Prepare statement failed: " . $this->mysqli->error); 529 | } 530 | 531 | $this->stmt->bind_param("i", $userId); 532 | $this->stmt->execute(); 533 | $this->stmt->store_result(); 534 | 535 | if ($this->stmt->num_rows == 1) { 536 | return true; 537 | } 538 | 539 | $this->logoutUser(); 540 | 541 | return false; 542 | } 543 | 544 | /** 545 | * Populates the current users data information for 546 | * quick access as an object. 547 | * 548 | * @return void 549 | * @throws Exception 550 | */ 551 | 552 | private function _populateUserdata() 553 | { 554 | $this->userdata = array(); 555 | 556 | if ($this->logged_in) { 557 | $data = $this->getInfoArray(); 558 | foreach ($data as $key => $value) { 559 | $this->userdata[$key] = $value; 560 | } 561 | } 562 | } 563 | 564 | } 565 | 566 | ?> -------------------------------------------------------------------------------- /reference.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 |71 | SimpleUsers::__construct() 72 |
73 |
74 | Object construct verifies that a session has been started and that a MySQL connection can be established.
75 | It takes no parameters.
76 |
78 |
85 | 86 | 87 |88 | SimpleUsers::createUser( string $username, string $password ) 89 |
90 |91 | Returns a (int)user id, if the user was created succesfully. 92 | If not, it returns (bool)false. 93 |
94 |95 |
112 | 113 | 114 | 115 |116 | SimpleUsers::loginUser( string $username, string $password ) 117 |
118 |
119 | Pairs up username and password as registrered in the database.
120 | If the username and password is correct, it will return (int)user id of
121 | the user which credentials has been passed and set the session, for
122 | use by the user validating.
123 |
125 |
148 | 149 | 150 |151 | SimpleUsers::setInfo( string $key, string $value [, int $userId] ) 152 |
153 |154 | Sets a custom information pair, consisting of a key name and that keys value for a user. 155 | Setting the third parameter sets the provided information for a given user, instead of the one calling it. 156 |
157 |158 |
171 | 172 | 173 |174 |
188 | 189 | 190 |191 | SimpleUsers::getInfo( string $key [, int $userId] ) 192 |
193 |
194 | Use this function to retrieve stored custom user information.
195 | Setting the third parameter gets the stored information for a given user, instead of the one calling it.
196 |
198 |
211 | 212 | 213 |214 |
228 | 229 | 230 | 231 |232 | SimpleUsers::getInfoArray( [int $userId] ) 233 |
234 |
235 | Returns an associative array with all the custom information stored about the current user.
236 | Passing a userId as a parameter will return an associative array with all the custom information for that specific user.
237 |
239 |
254 | 255 | 256 |257 |
273 | 274 | 275 |276 | SimpleUsers::logoutUser() 277 |
278 |279 | Logout the active user, unsetting the users session. 280 |
281 |282 |
294 | 295 | 296 |297 | SimpleUsers::setPassword( string $password [, int $userId] ) 298 |
299 |
300 | Update the users password with this function.
301 | It generates a new salt and a sets the users password provided by the first parameter.
302 | Providing a userId in the second paramater will set the password for that specific user.
303 |
305 |
318 | 319 | 320 |321 |
335 | 336 | 337 | 338 |339 | SimpleUsers::getUsers() 340 |
341 |342 | Gives you an array with all registered users and their basic information (last activity, creation date, username and userId) 343 |
344 |345 |
359 | 360 | 361 | 362 |363 | SimpleUsers::getSingleUser( [int $userId] ) 364 |
365 |
366 | Gives you an associative array, containing the basic information (last activity, creation date, username and userId) for the current active user.
367 | If a userId is provided as parameter, the returned associative array will contain the basic information for that specific user.
368 |
370 |
384 | 385 | 386 |387 |
403 | 404 | 405 |406 | SimpleUsers::deleteUser( int $userId ) 407 |
408 |409 | Deletes a given user and stored custom information with it. 410 |
411 |412 |
424 | 425 | 426 |427 | SimpleUsers::getToken( [bool] ) 428 |
429 |
430 | Returns a hidden input field with a unique token value for CSRF to be used with post data.
431 | The token is saved in a session for later validation.
432 | The boolean parameter is default set to true which returns the input field as XHTML - provide (bool) false for HTML 4 compliance.
433 |
435 |
448 | 449 | 450 |451 | SimpleUsers::validateToken() 452 |
453 |
454 | Use this method when you wish to validate the CSRF token from your post data.
455 | The method returns true upon validation, otherwise false.
456 |
458 |
479 | 480 | 481 |484 | SimpleUsers::logged_in 485 |
486 |
487 | This tells wether a user is logged in or not.
488 | Value is (bool)true or (bool)false.
489 |
491 |
506 | 507 | 508 | 509 |510 | SimpleUsers::userdata 511 |
512 |
513 | This is auto-populated array containing the information stored for the logged in user.
514 | If no user is logged in, this member consists of an empty array.
515 |
517 |
530 | 531 | 532 | 533 | 534 | --------------------------------------------------------------------------------