Advertisement
Guest User

Untitled

a guest
Jan 27th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.97 KB | None | 0 0
  1. <?php
  2. // Initialize the session
  3. session_start();
  4. if (isset($_GET["brand"])) {
  5. $brand = $_GET["brand"];
  6. } else {
  7. $brand = $_SESSION["login"];
  8. }
  9. if (!isset($brand)) {
  10. header("location: ./");
  11. exit;
  12. }
  13.  
  14.  
  15. // Check if the user is already logged in, if yes then redirect him to welcome page
  16. if(isset($_SESSION[$brand]) && $_SESSION[$brand] === true){
  17. header("location: ./generate/" .$brand. ".php");
  18. exit;
  19. }
  20.  
  21. // Include config file
  22. require_once "config.php";
  23.  
  24. // Define variables and initialize with empty values
  25. $username = $password = "";
  26. $username_err = $password_err = "";
  27.  
  28. // Processing form data when form is submitted
  29. if($_SERVER["REQUEST_METHOD"] == "POST"){
  30.  
  31. // Check if username is empty
  32. if(empty(trim($_POST["username"]))){
  33. $username_err = "Please enter username.";
  34. } else{
  35. $username = trim($_POST["username"]);
  36. }
  37.  
  38. // Check if password is empty
  39. if(empty(trim($_POST["password"]))){
  40. $password_err = "Please enter your password.";
  41. } else{
  42. $password = trim($_POST["password"]);
  43. }
  44.  
  45. // Validate credentials
  46. if(empty($username_err) && empty($password_err)){
  47. // Prepare a select statement
  48. $sql = "SELECT id, username, password, expires_at FROM $brand WHERE username = ?";
  49.  
  50. if($stmt = mysqli_prepare($link, $sql)){
  51. // Bind variables to the prepared statement as parameters
  52. mysqli_stmt_bind_param($stmt, "s", $param_username);
  53.  
  54. // Set parameters
  55. $param_username = $username;
  56.  
  57. // Attempt to execute the prepared statement
  58. if(mysqli_stmt_execute($stmt)){
  59. // Store result
  60. mysqli_stmt_store_result($stmt);
  61.  
  62. // Check if username exists, if yes then verify password
  63. if(mysqli_stmt_num_rows($stmt) == 1){
  64. // Bind result variables
  65. mysqli_stmt_bind_result($stmt, $id, $username, $hashed_password, $expires_at);
  66. if(mysqli_stmt_fetch($stmt)){
  67. if(password_verify($password, $hashed_password)){
  68. // Password is correct, so start a new session
  69. session_start();
  70.  
  71. // Store data in session variables
  72. $_SESSION[$brand] = true;
  73. $_SESSION["id"] = $id;
  74. $_SESSION["username"] = $username;
  75. $_SESSION[$brand."_expires_at"] = $expires_at;
  76.  
  77. // Redirect user to welcome page
  78. header("location: ./generate/" .$brand. ".php?kind=unlimited&brand=" .$brand);
  79. } else{
  80. // Display an error message if password is not valid
  81. $password_err = "The password you entered was not valid.";
  82. }
  83. }
  84. } else{
  85. // Display an error message if username doesn't exist
  86. $username_err = "No account found with that username.";
  87. }
  88. } else{
  89. echo "Oops! Something went wrong. Please try again later.";
  90. }
  91. }
  92.  
  93. // Close statement
  94. mysqli_stmt_close($stmt);
  95. }
  96.  
  97. // Close connection
  98. mysqli_close($link);
  99. }
  100. require_once __DIR__.'/../includes/header.php';
  101. ?>
  102. <style>
  103. .card {
  104. max-width: 400px;
  105. margin: 35px auto;
  106. }
  107. </style>
  108. <div class="card">
  109. <!--Card content-->
  110. <div class="card-body px-lg-5 pt-0">
  111. <center><div class="avatar mx-auto" style="text-allign:center;">
  112. <img src="../../../images/logo/<?php echo $brand?>.jpg" class="rounded-circle z-depth-1"
  113. alt="<?php echo $brand?>">
  114. </div></center>
  115. <!-- Form -->
  116. <form class="text-center" method="POST">
  117.  
  118. <!-- Email -->
  119. <div class="md-form <?php echo (!empty($username_err)) ? 'has-error' : ''; ?>">
  120. <input type="text" id="username" class="form-control" name="username" placeholder="Username" autofocus="" value="<?php echo $username; ?>">
  121. <label for="username">Username</label>
  122. <span class="help-block"><?php echo $username_err; ?></span>
  123. </div>
  124.  
  125. <!-- Password -->
  126. <div class="md-form <?php echo (!empty($password_err)) ? 'has-error' : ''; ?>">
  127. <input type="password" id="password" name="password" class="form-control">
  128. <label for="password">Password</label>
  129. <span class="help-block"><?php echo $password_err; ?></span>
  130. </div>
  131.  
  132.  
  133.  
  134. <!-- Sign in button -->
  135. <button class="btn btn-outline-info btn-rounded btn-block my-4 waves-effect z-depth-0" type="submit">Sign in</button>
  136.  
  137. <!-- Register -->
  138. <p>Not a member?
  139. <a href="register.php">Register</a>
  140. </p>
  141.  
  142. </form>
  143. <!-- Form -->
  144.  
  145. </div>
  146.  
  147. </div>
  148. <?php require_once __DIR__.'/../includes/footer.php'; ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement