Advertisement
Guest User

Untitled

a guest
Dec 4th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.63 KB | None | 0 0
  1. <?php
  2. // Start Session
  3. session_start();
  4. // Turn on all error reporting
  5. ERROR_REPORTING(E_ALL);
  6. ini_set('display_errors', 1);
  7.  
  8. require_once('classes/database.php');
  9. $link = new DATABASE;
  10.  
  11. // Include User info
  12. require_once('classes/user.php');
  13.  
  14. // Create instance for user class
  15. $activeUser = new USER($link);
  16. ?>
  17.  
  18. <?php
  19. require('config.php');
  20. // Check if user is already logged in
  21. if($activeUser->isLoggedIn()) {
  22. $activeUser->redirect('home.php');
  23. }
  24.  
  25. // Logging user into system
  26. if(isset($_POST['login'])) {
  27. $username = $_POST['user'];
  28. $password = $_POST['pass'];
  29.  
  30. if($activeUser->login($username, $password)) {
  31. $activeUser->redirect('home.php');
  32. }
  33.  
  34. else {
  35. $activeUser->error = "true";
  36. $activeUser->errorMessage = "Username or password is incorrect";
  37. }
  38. }
  39.  
  40. print_r($_SESSION);
  41. ?>
  42. <!doctype html>
  43. <html>
  44. <head>
  45. <meta charset="UTF-8">
  46. <title>IMD 2000 - Term Project (Will And Tyson)</title>
  47. </head>
  48.  
  49. <body>
  50. <form id = "registrationForm" method = "POST">
  51. <section id = "loginBox">
  52. <div id = "loginItems" name = "userBox">
  53. Username: <input type = "text" name = "user" required placeholder = "Username" /> <!-- Username input -->
  54. </div>
  55.  
  56. <div id = "loginItems" name = "passwordBox">
  57. Password: <input type = "password" name = "pass" required placeholder = "Password" /> <!-- Password input -->
  58. </div>
  59.  
  60. <div id = "loginItems" name = "loginBox">
  61. <input type = "submit" value = "Log In" name = "login" /> <!-- Log in to site -->
  62. </div>
  63. </section>
  64. </form>
  65.  
  66. <section id = "loginBox" name = "create">
  67. <a href = "newAccount.php">
  68. <input type = "button" value = "Create New Account" name = "createNew" />
  69. </a>
  70. </section>
  71.  
  72. <section id = "errorBox">
  73. <?php
  74. if($activeUser->error == "true") {
  75. echo $activeUser->errorMessage;
  76. }
  77. ?>
  78. </section>
  79.  
  80. </body>
  81. </html>
  82.  
  83. <?php
  84. require_once('config.php');
  85.  
  86. echo $_SESSION['username'];
  87. if(!$activeUser->isLoggedIn()) {
  88. header("Location: index.php");
  89. }
  90. print_r($_SESSION);
  91. ?>
  92. <!doctype html>
  93. <html>
  94. <head>
  95. <meta charset="UTF-8">
  96. <title>Home</title>
  97. <link rel = "stylesheet" href = "styles/homestylesheet.css">
  98. </head>
  99.  
  100. <body>
  101. <header class="site-header">
  102. <nav>
  103. <ul>
  104. <li><a href = "home.php">Home</a></li>
  105. <li><a href = "userInfo.php">Your Profile</a></li>
  106. <li><a href="">Name</a></li>
  107. <li><a href = "<?php $activeUser->logout();?>">Log Out</a></li>
  108. </ul>
  109. </nav>
  110. </header>
  111.  
  112.  
  113.  
  114. <h1>Home</h1>
  115.  
  116. <form id = "registrationForm" method = "POST">
  117. <section id = "loginBox">
  118. <div id = loginItems name = "userBox">
  119. <input type = "text" name = "user post" placeholder = "post" required />
  120. <input type = "submit" name = "submit" value = "post" />
  121. </div>
  122.  
  123. <div>
  124. <a><img src="Friendface.png" alt="Friendface"/>PosterName</a>
  125. <div>
  126. <post>
  127. tex here
  128. </post>
  129. </div>
  130. </div>
  131.  
  132. <div>
  133. <a><img src="Friendface.png" alt="Friendface"/>PosterName</a>
  134. <div>
  135. <post>
  136. tex here
  137. </post>
  138. </div>
  139. </div>
  140. </section>
  141. </form>
  142.  
  143. <section id = "errorBox">
  144. <?php if ($activeUser->error = "true") {echo $activeUser->errorMessage;}?>
  145. </section>
  146. </body>
  147. </html>
  148.  
  149. <?php
  150. class USER
  151. {
  152. // Set error to false, and blank error message
  153. public $error = "false";
  154. public $errorMessage = "";
  155.  
  156. private $conn;
  157.  
  158. // All the variables needed for the user profile.
  159. public $username;
  160. public $userID;
  161. public $password;
  162. public $firstName;
  163. public $lastName;
  164. public $emailAddress;
  165. public $address;
  166. public $city;
  167. public $province;
  168. public $country;
  169.  
  170. // OOP variable setting
  171. function __construct($conn){
  172. $this->conn = $conn;
  173. }
  174.  
  175. // Create a new user
  176. function createNewUser($username, $password) {
  177. // Clean inputs
  178. $username = trim($username);
  179. $password = trim($password);
  180.  
  181. // Encrypt password
  182. $password = password_hash($password, PASSWORD_DEFAULT);
  183.  
  184. // Check if username already exists
  185. $checkSQL = "SELECT * FROM users WHERE username = '$username'";
  186. $checkResult = $this->conn->queryDB($checkSQL);
  187. if(mysqli_num_rows($checkResult) > 0) {
  188. $this->error = "true";
  189. $this->errorMessage = "This username has already been taken. Please try again";
  190. return false;
  191. }
  192.  
  193. // Username does not exist, insert into database
  194. else {
  195. $insertSQL = "INSERT INTO users(username, password) VALUES('$username', '$password')";
  196. $insertResult = $this->conn->queryDB($insertSQL);
  197.  
  198. // Get the USER ID that is inserted into the function, to be used in the next phase of registration
  199. $userID = mysqli_insert_id($this->conn->getConnected());
  200. // Set the SESSION globals
  201. $_SESSION['username'] = $username;
  202. $_SESSION['userID'] = $userID;
  203. return true;
  204. }
  205. }
  206.  
  207. // Add or Edit User Info
  208. function userInfo($firstName, $lastName, $address, $city, $province, $country) {
  209. // Clean Inputs
  210. $firstName = trim($firstName);
  211. $lastName = trim($lastName);
  212. $emailAddress = "fakeyfakefake@fakeemail.com";
  213. $address = trim($address);
  214. $city = trim($city);
  215. $province = trim($province);
  216. $country = trim($country);
  217. $userID = $_SESSION['userID'];
  218.  
  219. // Validate first and last name, as they are the only required identifiers.
  220. if(empty($firstName) || empty($lastName)){
  221. $this->error = "true";
  222. $this->errorMessage = "Please enter a value for First AND Last Name";
  223. }
  224.  
  225. // Important values are valid, insert into database.
  226. else {
  227. // Check if user information is already set for User. If it is, we will use the UPDATE SQL query. If not, we will use the INSERT query
  228. $userInfoCheckSQL = "SELECT userID FROM userInfo WHERE userID = '$userID'";
  229. $userInfoCheckResult = $this->conn->queryDB($userInfoCheckSQL);
  230. $count = mysqli_num_rows($userInfoCheckResult);
  231. if ($count == 1) {
  232. $updateUserInfoSQL = "UPDATE userInfo
  233. SET firstName = '$firstName'
  234. lastName = '$lastName'
  235. address = '$address'
  236. city = '$city'
  237. province = '$province'
  238. country = '$country'
  239. WHERE userID = '$userID'
  240. ";
  241. $updateUserInfoResult = $this->conn->queryDB($updateUserInfoSQL);
  242.  
  243. return true;
  244. }
  245.  
  246. // User Info Does not exist for this user
  247. else {
  248. $addUserInfoSQL = "INSERT INTO userInfo(userID, firstName, lastName, emailAddress, address, city, province, country) VALUES('$userID','$firstName','$lastName','$emailAddress','$address','$city','$province','$country')";
  249. $addUserInfoResult = $this->conn->queryDB($addUserInfoSQL);
  250. return true;
  251. }
  252. }
  253. }
  254.  
  255. // Gather User Info From Database
  256. function fetchUserInfo() {
  257. $userID = $_SESSION['userID'];
  258. $fetchInfoQuery = "SELECT users.username, userInfo.* FROM users JOIN userInfo ON users.userID = userInfo.userID WHERE userInfo.userID = '$userID'";
  259. $fetchInfoResult = $this->conn->queryDB($fetchInfoQuery);
  260. $row = mysqli_fetch_array($fetchInfoResult, MYSQLI_ASSOC);
  261. $count = mysqli_num_rows($fetchInfoResult);
  262.  
  263. if($count == 1) {
  264.  
  265. $username = $row['username'];
  266. $firstName = $row['firstName'];
  267. $lastName = $row['lastName'];
  268. $emailAddress = $row['emailAddress'];
  269. $address = $row['address'];
  270. $city = $row['city'];
  271. $province = $row['province'];
  272. $country = $row['country'];
  273.  
  274. /*
  275. // Create a Table to display the information
  276. echo "<table id = 'userInfoTable'>";
  277.  
  278. // Create Rows and columns to store all the info
  279. echo "<tr><td>Username:</td><td>$username</td></tr>";
  280. echo "<tr><td>First Name:</td><td>$firstName</td></tr>";
  281. echo "<tr><td>Last Name:</td><td>$lastName</td></tr>";
  282. echo "<tr><td>E-Mail Address:</td><td>$emailAddress</td></tr>";
  283. echo "<tr><td>Address:</td><td>$address</td></tr>";
  284. echo "<tr><td>City:</td><td>$city</td></tr>";
  285. echo "<tr><td>Province:</td><td>$province</td></tr>";
  286. echo "<tr><td>Country:</td><td>$country</td></tr>";
  287.  
  288. // Close the table
  289. echo "</table>";
  290. */
  291. return true;
  292. }
  293. else {
  294. return false;
  295. }
  296. }
  297.  
  298. // Log in function
  299. function login($username, $password) {
  300. $sql = "SELECT * FROM users WHERE username = '$username'";
  301. $result = $this->conn->queryDB($sql);
  302. $row = mysqli_fetch_array($result, MYSQLI_ASSOC);
  303. // Validate the hash of the password
  304. $valid = password_verify($password, $row['password']);
  305. if ($valid) {
  306. // Set Session Variables
  307. $_SESSION['username'] = $username;
  308. $_SESSION['userID'] = $row['userID'];
  309.  
  310. return true;
  311. }
  312. }
  313.  
  314. // Check if user is already logged in function
  315. function isLoggedIn() {
  316. if(isset($_SESSION['username'])) {
  317. return true;
  318. }
  319. }
  320.  
  321. // Redirect to different section of site function
  322. function redirect($url) {
  323. session_write_close();
  324. header("Location: $url");
  325. exit;
  326. }
  327.  
  328. // Log out function
  329. function logout() {
  330. $_SESSION = array();
  331.  
  332. // Delete the cookies!
  333. if(ini_get("session.use_cookies")) {
  334. $params = session_get_cookie_params();
  335. setcookie(session_name(), '', time()-42000,
  336. $params["path"], $params["domain"],
  337. $params["secure"], $params["httponly"]
  338. );
  339. }
  340.  
  341. // Destroy the session
  342. session_destroy();
  343. }
  344.  
  345. /*
  346. // Delete User Account
  347. function deleteAccount() {
  348. global $conn;
  349. checkLoginStatus();
  350.  
  351. // Delete user info first
  352. $sqlDeleteInfo = "DELETE FROM userInfo WHERE userID = '$userID'";
  353. $deleteInfoResult = $conn->query($sqlDeleteInfo);
  354. if($deleteInfoResult) {
  355. echo "User info deleted successfully<br>";
  356. $sqlDeleteAccount = "DELETE FROM users WHERE userID = '$userID'";
  357. $deleteAccountResult = $conn->query($sqlDeleteAccount);
  358.  
  359. if ($deleteAccountResult){
  360. echo "Account has been deleted successfully.<br>";
  361. echo "Please click <a href = 'index.php'>here</a> to return to the index page.";
  362. session_destroy();
  363. }
  364.  
  365. else {
  366. "Error while deleting account <br>";
  367. }
  368. }
  369.  
  370. else {
  371. echo "Error while deleting user info<br>";
  372. }
  373. }*/
  374. // End of class
  375. }
  376. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement