Facebook
From Innocent Meerkat, 4 Years ago, written in Plain Text.
This paste is a reply to Re: slq kodu from Denim Bee - view diff
Embed
Download Paste or View Raw
Hits: 206
  1. CREATE DATABASE login;
  2.  
  3. USE login;
  4.  
  5. CREATE TABLE users (
  6.         id int auto_increment,
  7.         primary key(id),
  8.         username varchar(100) unique,
  9.         password varchar(300)
  10. );
  11.  
  12.  
  13.  
  14. <?php
  15. // Initialize the session
  16. session_start();
  17.  
  18. // Unset all of the session variables
  19. $_SESSION = array();
  20.  
  21. // Destroy the session.
  22. session_destroy();
  23.  
  24. // Redirect to login page
  25. header("location: login.php");
  26. exit;
  27. ?>
  28.  
  29.  
  30. <?php
  31. /* Database credentials. Assuming you are running MySQL
  32. server with default setting (user 'root' with no password) */
  33. define('DB_SERVER', 'localhost');
  34. define('DB_USERNAME', 'root');
  35. define('DB_PASSWORD', '');
  36. define('DB_NAME', 'login');
  37.  
  38. /* Attempt to connect to MySQL database */
  39. $link = mysqli_connect(DB_SERVER, DB_USERNAME, DB_PASSWORD, DB_NAME);
  40.  
  41. // Check connection
  42. if($link === false){
  43.     die("ERROR: Could not connect. " . mysqli_connect_error());
  44. }
  45. ?>
  46.  
  47.  
  48.  
  49.  
  50. <?php
  51. // Initialize the session
  52. session_start();
  53.  
  54. // Include config file
  55. require_once "config.php";
  56.  
  57. // Check if the user is logged in, if not then redirect him to login page
  58. if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
  59.     header("location: login.php");
  60.     exit;
  61. }
  62. $sql = "SELECT id, username FROM users ORDER BY id ASC";
  63. $result = $link->query($sql);
  64.  
  65. ?>
  66.  
  67. <!DOCTYPE html>
  68. <html lang="en">
  69. <head>
  70.     <meta charset="UTF-8">
  71.     <title>Welcome</title>
  72.     <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
  73.     <style type="text/css">
  74.         body{ font: 14px sans-serif; text-align: center; }
  75.     </style>
  76. </head>
  77. <body>
  78.     <div class="page-header">
  79.         <h1>Hi, <b><?php echo htmlspecialchars($_SESSION["username"]); ?></b>. Welcome to our site.</h1>
  80.     </div>
  81.     <p>
  82.         <a href="reset-password.php" class="btn btn-warning">Reset Your Password</a>
  83.         <a href="logout.php" class="btn btn-danger">Sign Out of Your Account</a>
  84.     </p>
  85.     <div class="container">
  86.         <table class="table">
  87.         <thead>
  88.             <tr>
  89.                <th>#</th>
  90.                 <th>Username</th>
  91.             </tr>
  92.             </thead>
  93.             <tbody>
  94.         <?php
  95.             foreach($result as $value) {
  96.                 echo "<tr>";
  97.                 echo    "<td>" . $value['id'] . "</td>";
  98.                 echo    "<td>" . $value['username'] . "</td>";
  99.                 echo "</tr>";
  100.             }
  101.         ?>
  102.             </tbody>
  103.         </table>
  104.     </div>
  105. </body>
  106. </html>
  107.  
  108.  
  109.  
  110.  
  111.  
  112.  
  113.  
  114. <?php
  115. // Initialize the session
  116. session_start();
  117.  
  118. // Check if the user is logged in, otherwise redirect to login page
  119. if(!isset($_SESSION["loggedin"]) || $_SESSION["loggedin"] !== true){
  120.     header("location: login.php");
  121.     exit;
  122. }
  123.  
  124. // Include config file
  125. require_once "config.php";
  126.  
  127. // Define variables and initialize with empty values
  128. $new_password = $confirm_password = "";
  129. $new_password_err = $confirm_password_err = "";
  130.  
  131. // Processing form data when form is submitted
  132. if($_SERVER["REQUEST_METHOD"] == "POST"){
  133.  
  134.     // Validate new password
  135.     if(empty(trim($_POST["new_password"]))){
  136.         $new_password_err = "Please enter the new password.";    
  137.     } elseif(strlen(trim($_POST["new_password"])) < 6){
  138.         $new_password_err = "Password must have atleast 6 characters.";
  139.     } else{
  140.         $new_password = trim($_POST["new_password"]);
  141.     }
  142.    
  143.     // Validate confirm password
  144.     if(empty(trim($_POST["confirm_password"]))){
  145.         $confirm_password_err = "Please confirm the password.";
  146.     } else{
  147.         $confirm_password = trim($_POST["confirm_password"]);
  148.         if(empty($new_password_err) && ($new_password != $confirm_password)){
  149.             $confirm_password_err = "Password did not match.";
  150.         }
  151.     }
  152.        
  153.     // Check input errors before updating the database
  154.     if(empty($new_password_err) && empty($confirm_password_err)){
  155.         // Prepare an update statement
  156.         $sql = "UPDATE users SET password = ? WHERE id = ?";
  157.        
  158.         if($stmt = mysqli_prepare($link, $sql)){
  159.             // Bind variables to the prepared statement as parameters
  160.             mysqli_stmt_bind_param($stmt, "si", $param_password, $param_id);
  161.            
  162.             // Set parameters
  163.             $param_password = password_hash($new_password, PASSWORD_DEFAULT);
  164.             $param_id = $_SESSION["id"];
  165.            
  166.             // Attempt to execute the prepared statement
  167.             if(mysqli_stmt_execute($stmt)){
  168.                 // Password updated successfully. Destroy the session, and redirect to login page
  169.                 session_destroy();
  170.                 header("location: login.php");
  171.                 exit();
  172.             } else{
  173.                 echo "Oops! Something went wrong. Please try again later.";
  174.             }
  175.  
  176.             // Close statement
  177.             mysqli_stmt_close($stmt);
  178.         }
  179.     }
  180.    
  181.     // Close connection
  182.     mysqli_close($link);
  183. }
  184. ?>
  185.  
  186. <!DOCTYPE html>
  187. <html lang="en">
  188. <head>
  189.     <meta charset="UTF-8">
  190.     <title>Reset Password</title>
  191.     <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
  192.     <style type="text/css">
  193.         body{ font: 14px sans-serif; }
  194.         .wrapper{ width: 350px; padding: 20px; }
  195.     </style>
  196. </head>
  197. <body>
  198.     <div class="wrapper">
  199.         <h2>Reset Password</h2>
  200.         <p>Please fill out this form to reset your password.</p>
  201.         <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  202.             <div class="form-group <?php echo (!empty($new_password_err)) ? 'has-error' : ''; ?>">
  203.                 <label>New Password</label>
  204.                 <input type="password" name="new_password" class="form-control" value="<?php echo $new_password; ?>">
  205.                 <span class="help-block"><?php echo $new_password_err; ?></span>
  206.             </div>
  207.             <div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
  208.                 <label>Confirm Password</label>
  209.                 <input type="password" name="confirm_password" class="form-control">
  210.                 <span class="help-block"><?php echo $confirm_password_err; ?></span>
  211.             </div>
  212.             <div class="form-group">
  213.                 <input type="submit" class="btn btn-primary" value="Submit">
  214.                 <a class="btn btn-link" href="welcome.php">Cancel</a>
  215.             </div>
  216.         </form>
  217.     </div>    
  218. </body>
  219. </html>
  220.  
  221.  
  222.  
  223.  
  224.  
  225.  
  226.  
  227.  
  228.  
  229. <?php
  230. // Initialize the session
  231. session_start();
  232.  
  233. // Check if the user is already logged in, if yes then redirect him to welcome page
  234. if(isset($_SESSION["loggedin"]) && $_SESSION["loggedin"] === true){
  235.   header("location: welcome.php");
  236.   exit;
  237. }
  238.  
  239. // Include config file
  240. require_once "config.php";
  241.  
  242. // Define variables and initialize with empty values
  243. $username = $password = "";
  244. $username_err = $password_err = "";
  245.  
  246. // Processing form data when form is submitted
  247. if($_SERVER["REQUEST_METHOD"] == "POST"){
  248.  
  249.     // Check if username is empty
  250.     if(empty(trim($_POST["username"]))){
  251.         $username_err = "Please enter username.";
  252.     } else{
  253.         $username = trim($_POST["username"]);
  254.     }
  255.    
  256.     // Check if password is empty
  257.     if(empty(trim($_POST["password"]))){
  258.         $password_err = "Please enter your password.";
  259.     } else{
  260.         $password = trim($_POST["password"]);
  261.     }
  262.    
  263.     // Validate credentials
  264.     if(empty($username_err) && empty($password_err)){
  265.         // Prepare a select statement
  266.         $sql = "SELECT id, username, password FROM users WHERE username = ?";
  267.        
  268.         if($stmt = mysqli_prepare($link, $sql)){
  269.             // Bind variables to the prepared statement as parameters
  270.             mysqli_stmt_bind_param($stmt, "s", $param_username);
  271.            
  272.             // Set parameters
  273.             $param_username = $username;
  274.            
  275.             // Attempt to execute the prepared statement
  276.             if(mysqli_stmt_execute($stmt)){
  277.                 // Store result
  278.                 mysqli_stmt_store_result($stmt);
  279.                
  280.                 // Check if username exists, if yes then verify password
  281.                 if(mysqli_stmt_num_rows($stmt) == 1){                    
  282.                     // Bind result variables
  283.                     mysqli_stmt_bind_result($stmt, $id, $username, $hashed_password);
  284.                     if(mysqli_stmt_fetch($stmt)){
  285.                         if(password_verify($password, $hashed_password)){
  286.                             // Password is correct, so start a new session
  287.                             session_start();
  288.                            
  289.                             // Store data in session variables
  290.                             $_SESSION["loggedin"] = true;
  291.                             $_SESSION["id"] = $id;
  292.                             $_SESSION["username"] = $username;                            
  293.                            
  294.                             // Redirect user to welcome page
  295.                             header("location: welcome.php");
  296.                         } else{
  297.                             // Display an error message if password is not valid
  298.                             $password_err = "The password you entered was not valid.";
  299.                         }
  300.                     }
  301.                 } else{
  302.                     // Display an error message if username doesn't exist
  303.                     $username_err = "No account found with that username.";
  304.                 }
  305.             } else{
  306.                 echo "Oops! Something went wrong. Please try again later.";
  307.             }
  308.  
  309.             // Close statement
  310.             mysqli_stmt_close($stmt);
  311.         }
  312.     }
  313.    
  314.     // Close connection
  315.     mysqli_close($link);
  316. }
  317. ?>
  318.  
  319. <!DOCTYPE html>
  320. <html lang="en">
  321. <head>
  322.     <meta charset="UTF-8">
  323.     <title>Login</title>
  324.     <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
  325.     <style type="text/css">
  326.         body{ font: 14px sans-serif; }
  327.         .wrapper{ width: 350px; padding: 20px; }
  328.     </style>
  329. </head>
  330. <body>
  331.     <div class="wrapper">
  332.         <h2>Login</h2>
  333.         <p>Please fill in your credentials to login.</p>
  334.         <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  335.             <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
  336.                 <label>Username</label>
  337.                 <input type="text" name="username" class="form-control" value="<?php echo $username; ?>">
  338.                 <span class="help-block"><?php echo $username_err; ?></span>
  339.             </div>    
  340.             <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
  341.                 <label>Password</label>
  342.                 <input type="password" name="password" class="form-control">
  343.                 <span class="help-block"><?php echo $password_err; ?></span>
  344.             </div>
  345.             <div class="form-group">
  346.                 <input type="submit" class="btn btn-primary" value="Login">
  347.             </div>
  348.             <p>Don't have an account? <a href="register.php">Sign up now</a>.</p>
  349.         </form>
  350.     </div>    
  351. </body>
  352. </html>
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361. <?php
  362. // Include config file
  363. require_once "config.php";
  364.  
  365. // Define variables and initialize with empty values
  366. $username = $password = $confirm_password = "";
  367. $username_err = $password_err = $confirm_password_err = "";
  368.  
  369. // Processing form data when form is submitted
  370. if($_SERVER["REQUEST_METHOD"] == "POST"){
  371.  
  372.     // Validate username
  373.     if(empty(trim($_POST["username"]))){
  374.         $username_err = "Please enter a username.";
  375.     } else{
  376.         // Prepare a select statement
  377.         $sql = "SELECT id FROM users WHERE username = ?";
  378.        
  379.         if($stmt = mysqli_prepare($link, $sql)){
  380.             // Bind variables to the prepared statement as parameters
  381.             mysqli_stmt_bind_param($stmt, "s", $param_username);
  382.            
  383.             // Set parameters
  384.             $param_username = trim($_POST["username"]);
  385.            
  386.             // Attempt to execute the prepared statement
  387.             if(mysqli_stmt_execute($stmt)){
  388.                 /* store result */
  389.                 mysqli_stmt_store_result($stmt);
  390.                
  391.                 if(mysqli_stmt_num_rows($stmt) == 1){
  392.                     $username_err = "This username is already taken.";
  393.                 } else{
  394.                     $username = trim($_POST["username"]);
  395.                 }
  396.             } else{
  397.                 echo "Oops! Something went wrong. Please try again later.";
  398.             }
  399.  
  400.             // Close statement
  401.             mysqli_stmt_close($stmt);
  402.         }
  403.     }
  404.    
  405.     // Validate password
  406.     if(empty(trim($_POST["password"]))){
  407.         $password_err = "Please enter a password.";    
  408.     } elseif(strlen(trim($_POST["password"])) < 6){
  409.         $password_err = "Password must have atleast 6 characters.";
  410.     } else{
  411.         $password = trim($_POST["password"]);
  412.     }
  413.    
  414.     // Validate confirm password
  415.     if(empty(trim($_POST["confirm_password"]))){
  416.         $confirm_password_err = "Please confirm password.";    
  417.     } else{
  418.         $confirm_password = trim($_POST["confirm_password"]);
  419.         if(empty($password_err) && ($password != $confirm_password)){
  420.             $confirm_password_err = "Password did not match.";
  421.         }
  422.     }
  423.    
  424.     // Check input errors before inserting in database
  425.     if(empty($username_err) && empty($password_err) && empty($confirm_password_err)){
  426.        
  427.         // Prepare an insert statement
  428.         $sql = "INSERT INTO users (username, password) VALUES (?, ?)";
  429.          
  430.         if($stmt = mysqli_prepare($link, $sql)){
  431.             // Bind variables to the prepared statement as parameters
  432.             mysqli_stmt_bind_param($stmt, "ss", $param_username, $param_password);
  433.            
  434.             // Set parameters
  435.             $param_username = $username;
  436.             $param_password = password_hash($password, PASSWORD_DEFAULT); // Creates a password hash
  437.            
  438.             // Attempt to execute the prepared statement
  439.             if(mysqli_stmt_execute($stmt)){
  440.                 // Redirect to login page
  441.                 header("location: login.php");
  442.             } else{
  443.                 echo "Something went wrong. Please try again later.";
  444.             }
  445.  
  446.             // Close statement
  447.             mysqli_stmt_close($stmt);
  448.         }
  449.     }
  450.    
  451.     // Close connection
  452.     mysqli_close($link);
  453. }
  454. ?>
  455.  
  456. <!DOCTYPE html>
  457. <html lang="en">
  458. <head>
  459.     <meta charset="UTF-8">
  460.     <title>Sign Up</title>
  461.     <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.4.1/css/bootstrap.min.css" integrity="sha384-Vkoo8x4CGsO3+Hhxv8T/Q5PaXtkKtu6ug5TOeNV6gBiFeWPGFN9MuhOf23Q9Ifjh" crossorigin="anonymous">
  462.     <style type="text/css">
  463.         body{ font: 14px sans-serif; }
  464.         .wrapper{ width: 350px; padding: 20px; }
  465.     </style>
  466. </head>
  467. <body>
  468.     <div class="wrapper">
  469.         <h2>Sign Up</h2>
  470.         <p>Please fill this form to create an account.</p>
  471.         <form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
  472.             <div class="form-group <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
  473.                 <label>Username</label>
  474.                 <input type="text" name="username" class="form-control" value="<?php echo $username; ?>">
  475.                 <span class="help-block"><?php echo $username_err; ?></span>
  476.             </div>    
  477.             <div class="form-group <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
  478.                 <label>Password</label>
  479.                 <input type="password" name="password" class="form-control" value="<?php echo $password; ?>">
  480.                 <span class="help-block"><?php echo $password_err; ?></span>
  481.             </div>
  482.             <div class="form-group <?php echo (!empty($confirm_password_err)) ? 'has-error' : ''; ?>">
  483.                 <label>Confirm Password</label>
  484.                 <input type="password" name="confirm_password" class="form-control" value="<?php echo $confirm_password; ?>">
  485.                 <span class="help-block"><?php echo $confirm_password_err; ?></span>
  486.             </div>
  487.             <div class="form-group">
  488.                 <input type="submit" class="btn btn-primary" value="Submit">
  489.                 <input type="reset" class="btn btn-default" value="Reset">
  490.             </div>
  491.             <p>Already have an account? <a href="login.php">Login here</a>.</p>
  492.         </form>
  493.     </div>    
  494. </body>
  495. </html>
  496.  
  497.  
  498.  
  499.  
  500.  
  501.  
  502.  
  503.  
  504.  
  505.  
  506.