Advertisement
Guest User

Untitled

a guest
Nov 29th, 2018
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.04 KB | None | 0 0
  1. <?php
  2. // Initialize the session
  3. session_start();
  4.  
  5. // Check if the user is logged in, if not then redirect him to login page
  6. if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
  7. header("location: login.php");
  8. exit;
  9. }
  10. ?>
  11.  
  12. <!DOCTYPE html>
  13. <html lang="en">
  14. <head>
  15. <meta charset="UTF-8">
  16. <title>Welcome</title>
  17. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
  18. <style type="text/css">
  19. body{ font: 14px sans-serif; text-align: center; }
  20. </style>
  21. </head>
  22. <body>
  23. <div class="page-header">
  24. <h1>Hi, <b><?php echo htmlspecialchars($_SESSION["username"]); ?></b>. Welcome to our site.</h1>
  25. </div>
  26. <p>
  27. <a href="reset-password.php" class="btn btn-warning">Reset Your Password</a>
  28. <a href="logout.php" class="btn btn-danger">Sign Out of Your Account</a>
  29. </p>
  30. </body>
  31. </html>
  32.  
  33. <?php
  34.  
  35. error_reporting(E_ALL); ini_set('display_errors', 1);
  36.  
  37.  
  38. // Initialize the secure session
  39.  
  40. session_start();
  41.  
  42. // Check if the user is already logged in, if yes then redirect him to welcome page
  43. if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
  44. header("location: welcome.php");
  45. exit;
  46. }
  47.  
  48. // Include config file
  49. require_once "config.php";
  50.  
  51. // Define variables and initialize with empty values
  52. $username = $password = "";
  53. $username_err = $password_err = "";
  54.  
  55. // Processing form data when form is submitted
  56. if($_SERVER["REQUEST_METHOD"] == "POST"){
  57.  
  58. // Check if username is empty
  59. if(empty(trim($_POST["username"]))){
  60. $username_err = "Please enter username.";
  61. } else{
  62. $username = trim($_POST["username"]);
  63. }
  64.  
  65. // Check if password is empty
  66. if(empty(trim($_POST["password"]))){
  67. $password_err = "Please enter your password.";
  68. } else{
  69. $password = trim($_POST["password"]);
  70. }
  71.  
  72. // Validate credentials
  73. if(empty($username_err) && empty($password_err)){
  74. // Prepare a select statement
  75. $sql = "SELECT id, username, password FROM users WHERE username = :username";
  76.  
  77. if($stmt = $pdo->prepare($sql)){
  78. // Bind variables to the prepared statement as parameters
  79. $stmt->bindParam(":username", $param_username, PDO::PARAM_STR);
  80.  
  81. // Set parameters
  82. $param_username = trim($_POST["username"]);
  83.  
  84. // Attempt to execute the prepared statement
  85. if($stmt->execute()){
  86. // Check if username exists, if yes then verify password
  87. if($stmt->rowCount() == 1){
  88. if($row = $stmt->fetch()){
  89. $id = $row["id"];
  90. $username = $row["username"];
  91. $hashed_password = $row["password"];
  92. if(password_verify($password, $hashed_password)){
  93. // Password is correct, so start a new session
  94. session_start();
  95.  
  96. // Store data in session variables
  97. $_SESSION["loggedin"] = true;
  98. $_SESSION["id"] = $id;
  99. $_SESSION["username"] = $username;
  100.  
  101. // Redirect user to welcome page
  102. header("location: welcome.php");
  103. } else{
  104. // Display an error message if password is not valid
  105. $password_err = "The password you entered was not valid.";
  106. }
  107. }
  108. } else{
  109. // Display an error message if username doesn't exist
  110. $username_err = "No account found with that username.";
  111. }
  112. } else{
  113. echo "Oops! Something went wrong. Please try again later.";
  114. }
  115. }
  116.  
  117. // Close statement
  118. unset($stmt);
  119. }
  120.  
  121. // Close connection
  122. unset($pdo);
  123. }
  124. ?>
  125.  
  126. <!DOCTYPE html>
  127. <html lang="en">
  128. <head>
  129. <meta charset="UTF-8">
  130. <title>Login</title>
  131. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
  132. <style type="text/css">
  133. body{ font: 14px sans-serif; }
  134. .wrapper{ width: 350px; padding: 20px; }
  135. </style>
  136. </head>
  137. <body>
  138. <div class="wrapper">
  139. <h2>Login</h2>
  140. <p>Please fill in your credentials to login.</p>
  141. <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  142. <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
  143. <label>Username</label>
  144. <input type="text" name="username" class="form-control" value="<?php echo $username; ?>">
  145. <span class="help-block"><?php echo $username_err; ?></span>
  146. </div>
  147. <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
  148. <label>Password</label>
  149. <input type="password" name="password" class="form-control">
  150. <span class="help-block"><?php echo $password_err; ?></span>
  151. </div>
  152. <div class="form-group">
  153. <input type="submit" class="btn btn-primary" value="Login">
  154. </div>
  155. <p>Don't have an account? <a href="register.php">Sign up now</a>.</p>
  156. </form>
  157. </div>
  158. </body>
  159. </html>
  160.  
  161. <?php
  162. // Include config file
  163. require_once "config.php";
  164.  
  165. // Define variables and initialize with empty values
  166. $username = $password = $confirm_password = "";
  167. $username_err = $password_err = $confirm_password_err = "";
  168.  
  169. // Processing form data when form is submitted
  170. if($_SERVER["REQUEST_METHOD"] == "POST"){
  171.  
  172. // Validate username
  173. if(empty(trim($_POST["username"]))){
  174. $username_err = "Please enter a username.";
  175. } else{
  176. // Prepare a select statement
  177. $sql = "SELECT id FROM users WHERE username = :username";
  178.  
  179. if($stmt = $pdo->prepare($sql)){
  180. // Bind variables to the prepared statement as parameters
  181. $stmt->bindParam(":username", $param_username, PDO::PARAM_STR);
  182.  
  183. // Set parameters
  184. $param_username = trim($_POST["username"]);
  185.  
  186. // Attempt to execute the prepared statement
  187. if($stmt->execute()){
  188. if($stmt->rowCount() == 1){
  189. $username_err = "This username is already taken.";
  190. } else{
  191. $username = trim($_POST["username"]);
  192. }
  193. } else{
  194. echo "Oops! Something went wrong. Please try again later.";
  195. }
  196. }
  197.  
  198. // Close statement
  199. unset($stmt);
  200. }
  201.  
  202. // Validate password
  203. if(empty(trim($_POST["password"]))){
  204. $password_err = "Please enter a password.";
  205. } elseif(strlen(trim($_POST["password"])) < 6){
  206. $password_err = "Password must have atleast 6 characters.";
  207. } else{
  208. $password = trim($_POST["password"]);
  209. }
  210.  
  211. // Validate confirm password
  212. if(empty(trim($_POST["confirm_password"]))){
  213. $confirm_password_err = "Please confirm password.";
  214. } else{
  215. $confirm_password = trim($_POST["confirm_password"]);
  216. if(empty($password_err) && ($password != $confirm_password)){
  217. $confirm_password_err = "Password did not match.";
  218. }
  219. }
  220.  
  221. // Check input errors before inserting in database
  222. if(empty($username_err) && empty($password_err) && empty($confirm_password_err)){
  223.  
  224. // Prepare an insert statement
  225. $sql = "INSERT INTO users (username, password) VALUES (:username, :password)";
  226.  
  227. if($stmt = $pdo->prepare($sql)){
  228. // Bind variables to the prepared statement as parameters
  229. $stmt->bindParam(":username", $param_username, PDO::PARAM_STR);
  230. $stmt->bindParam(":password", $param_password, PDO::PARAM_STR);
  231.  
  232. // Set parameters
  233. $param_username = $username;
  234. $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
  235.  
  236. // Attempt to execute the prepared statement
  237. if($stmt->execute()){
  238. // Redirect to login page
  239. header("location: login.php");
  240. } else{
  241. echo "Something went wrong. Please try again later.";
  242. }
  243. }
  244.  
  245. // Close statement
  246. unset($stmt);
  247. }
  248.  
  249. // Close connection
  250. unset($pdo);
  251. }
  252. ?>
  253.  
  254. <!DOCTYPE html>
  255. <html lang="en">
  256. <head>
  257. <meta charset="UTF-8">
  258. <title>Sign Up</title>
  259. <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
  260. <style type="text/css">
  261. body{ font: 14px sans-serif; }
  262. .wrapper{ width: 350px; padding: 20px; }
  263. </style>
  264. </head>
  265. <body>
  266. <div class="wrapper">
  267. <h2>Sign Up</h2>
  268. <p>Please fill this form to create an account.</p>
  269. <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  270. <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
  271. <label>Username</label>
  272. <input type="text" name="username" class="form-control" value="<?php echo $username; ?>">
  273. <span class="help-block"><?php echo $username_err; ?></span>
  274. </div>
  275. <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
  276. <label>Password</label>
  277. <input type="password" name="password" class="form-control" value="<?php echo $password; ?>">
  278. <span class="help-block"><?php echo $password_err; ?></span>
  279. </div>
  280. <div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
  281. <label>Confirm Password</label>
  282. <input type="password" name="confirm_password" class="form-control" value="<?php echo $confirm_password; ?>">
  283. <span class="help-block"><?php echo $confirm_password_err; ?></span>
  284. </div>
  285. <div class="form-group">
  286. <input type="submit" class="btn btn-primary" value="Submit">
  287. <input type="reset" class="btn btn-default" value="Reset">
  288. </div>
  289. <p>Already have an account? <a href="login.php">Login here</a>.</p>
  290. </form>
  291. </div>
  292. </body>
  293. </html>
  294.  
  295. <?php
  296. /* Database credentials. Assuming you are running MySQL
  297. server with default setting (user 'root' with no password) */
  298. define('DB_SERVER', 'localhost');
  299. define('DB_USERNAME', 'root');
  300. define('DB_PASSWORD', 'SECRETE');
  301. define('DB_NAME', 'mySite');
  302.  
  303. /* Attempt to connect to MySQL database */
  304. try{
  305. $pdo = new PDO("mysql:host=" . DB_SERVER . ";dbname=" . DB_NAME, DB_USERNAME, DB_PASSWORD);
  306. // Set the PDO error mode to exception
  307. $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
  308. } catch(PDOException $e){
  309. die("ERROR: Could not connect. " . $e->getMessage());
  310. }
  311. ?>
  312.  
  313. <?php
  314. // Initialize the session
  315. session_start();
  316.  
  317. // Unset all of the session variables
  318. $_SESSION = array();
  319.  
  320. // Destroy the session.
  321. session_destroy();
  322.  
  323. // Redirect to login page
  324. header("location: login.php");
  325. exit;
  326. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement