Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. <?php if(!(isset($_SESSION['username']))): ?>
  2. <P class="lead">No has iniciado sesión <a href="login.php">Inicia sesión</a>
  3. </P>
  4.  
  5. <?php else: ?>
  6. <p class="lead">Has iniciado sesión como <?php if(isset($_SESSION['username'])) echo $_SESSION['username']; ?>
  7.  
  8.  
  9. <?php endif ?>
  10.  
  11. function rememberMe($user_id){
  12. $encryptCookieData = base64_encode("UaQteh5i4y3dntstemYODEC{$user_id}");
  13. // Cookie set to expire in about 30 days
  14. setcookie("rememberUserCookie", $encryptCookieData, time()+60*60*24*100, "/");
  15. }
  16.  
  17. /**
  18. * checked if the cookie used is same with the encrypted cookie
  19. * @param $db, database connection link
  20. * @return bool, true if the user cookie is valid
  21. */
  22. function isCookieValid($db){
  23. $isValid = false;
  24. if (isset($_COOKIE['rememberUserCookie'])) {
  25.  
  26. /**
  27. * Decode cookies and extract user ID
  28. */
  29. $decryptCookieData = base64_decode($_COOKIE['rememberUserCookie']);
  30. $user_id = explode("UaQteh5i4y3dntstemYODEC", $decryptCookieData);
  31. $userID = $user_id[1];
  32.  
  33. /**
  34. * check if id retrieved from the cookie exist in the database
  35. * */
  36. $sqlQuery = "SELECT * FROM users WHERE id = :id";
  37. $statement = $db->prepare($sqlQuery);
  38. $statement->execute(array(':id' => $userID));
  39.  
  40. if($row = $statement->fetch()){
  41. $id = $row['id'];
  42. $username = $row['username'];
  43.  
  44. /**
  45. * Create the user session variable
  46. */
  47. $_SESSION['id'] = $id;
  48. $_SESSION['username'] = $username;
  49. $isValid = true;
  50. }else{
  51. /**
  52. * cookie ID is invalid destroy session and logout user
  53. */
  54. $isValid = false;
  55. signout();
  56. }
  57. }
  58. return $isValid;
  59. }
  60.  
  61. /**
  62. * kill all sessions, cookies and regenrate session ID
  63. * Redirect to index page after all
  64. */
  65. function signout(){
  66. unset($_SESSION['username']);
  67. unset($_SESSION['id']);
  68.  
  69. if(isset($_COOKIE['rememberUserCookie'])){
  70. unset($_COOKIE['rememberUserCookie']);
  71. setcookie('rememberUserCookie', null, -1, '/');
  72. }
  73. session_destroy();
  74. session_regenerate_id(true);
  75. redirectTo('index');
  76. }
  77.  
  78. /**
  79. *
  80. * @return bool, true if all good
  81. */
  82. function guard(){
  83.  
  84. $isValid = true;
  85. $inactive = 60 * 15; //15 mins
  86. $fingerprint = md5($_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']);
  87.  
  88. if((isset($_SESSION['fingerprint']) && $_SESSION['fingerprint'] != $fingerprint)){
  89. $isValid = false;
  90. signout();
  91. }else if((isset($_SESSION['last_active']) && (time() - $_SESSION['last_active']) > $inactive) && $_SESSION['username']){
  92. $isValid = false;
  93. signout();
  94. }else{
  95. $_SESSION['last_active'] = time();
  96. }
  97.  
  98. return $isValid;
  99. }
  100.  
  101. function isValidImage($file){
  102. $form_errors = array();
  103.  
  104. //split file name into an array using the dot (.)
  105. $part = explode(".", $file);
  106.  
  107. //target the last element in the array
  108. $extension = end($part);
  109.  
  110. switch(strtolower($extension)){
  111. case 'jpg':
  112. case 'gif':
  113. case 'bmp':
  114. case 'png':
  115.  
  116. return $form_errors;
  117. }
  118.  
  119. $form_errors[] = $extension . " is not a valid image extension";
  120. return $form_errors;
  121. }
  122.  
  123.  
  124. function _token(){
  125. $randonToken = base64_encode(openssl_random_pseudo_bytes(32));
  126. //$randonToken = md5(uniqid(rand(), true))." md5";
  127.  
  128. return $_SESSION['token'] = $randonToken;
  129. }
  130.  
  131. function validate_token($requestToken){
  132. if(isset($_SESSION['token']) && $requestToken === $_SESSION['token']){
  133. unset($_SESSION['token']);
  134.  
  135. return true;
  136. }
  137.  
  138. return false;
  139. }
  140.  
  141. function prepLogin ($id, $username, $remember){
  142. $_SESSION['id'] = $id;
  143. $_SESSION['username'] = $username;
  144.  
  145. $fingerprint = md5($_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']);
  146. $_SESSION['last_active'] = time();
  147. $_SESSION['fingerprint'] = $fingerprint;
  148.  
  149. if($remember === "yes"){
  150. rememberMe($id);
  151. }
  152. //call sweet alert
  153. echo $welcome = "<script type="text/javascript">
  154. swal({
  155. title: "Welcome back $username!",
  156. text: "You're being logged in.",
  157. type: 'success',
  158. timer: 3000,
  159. showConfirmButton: false });
  160. setTimeout(function(){
  161. window.location.href = 'index.php';
  162. }, 3000);
  163. </script>";
  164. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement