Advertisement
Guest User

Untitled

a guest
Dec 23rd, 2016
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. <?php
  2. session_start();
  3. error_reporting(0);
  4.  
  5. $db = mysqli_connect("127.0.0.1", "root", "", "mytest");
  6.  
  7. /*Fill in values if not set*/
  8. if( !( isset($_SESSION["logged_in"]) && isset($_SESSION["active_user"]) ) )
  9. {
  10. $_SESSION["logged_in"] = 0;
  11. $_SESSION["active_user"] = "None";
  12. }
  13.  
  14. /*Awesome shorthand logic*/
  15. $postUser = ((isset($_POST["user"]) && !empty(trim($_POST["user"]))) ? true : false);
  16. $postPass = ((isset($_POST["pass"]) && !empty(trim($_POST["pass"]))) ? true : false);
  17.  
  18. /*Make mysql query, and if both values return true, rows will equal 1*/
  19. if($postUser && $postPass)
  20. {
  21. $mysqlPostUser = mysqli_real_escape_string($db, $_POST["user"]);
  22. $mysqlPostPass = mysqli_real_escape_string($db, $_POST["pass"]);
  23. if((mysqli_num_rows(mysqli_query($db, "SELECT * FROM logins WHERE username='{$mysqlPostUser}' AND password='{$mysqlPostPass}'"))) > 0)
  24. {
  25. $_SESSION["logged_in"] = 1;
  26. $_SESSION["active_user"] = $_POST["user"];
  27. }
  28. else
  29. {
  30. $_SESSION["logged_in"] = 0;
  31. $_SESSION["active_user"] = "nope";
  32. }
  33. header("Location: /");
  34. }
  35.  
  36. /*Prevents browser asking for resubmit if only one value is submitted*/
  37. if( $postUser xor $postPass )
  38. header("Location: /");
  39. ?>
  40. <!DOCTYPE html>
  41. <html>
  42. <head>
  43. <title>Login Test</title>
  44. <meta name="viewport" content="width=device-width, initial-scale=1">
  45. <style type="text/css">
  46. html, body
  47. {
  48. margin:0px;
  49. padding:0px;
  50. }
  51.  
  52. html {overflow-y: scroll;}
  53.  
  54. body
  55. {
  56. padding:50px;
  57. }
  58.  
  59. .text
  60. {
  61. font-family: Verdana;
  62. font-size:16px;
  63. font-weight: normal;
  64. }
  65.  
  66. .box
  67. {
  68. box-sizing: border-box;
  69. display: block;
  70. }
  71.  
  72. .inline
  73. {
  74. box-sizing: border-box;
  75. display: inline-block;
  76. }
  77.  
  78. #wrapper
  79. {
  80. width:100%;
  81. max-width:280px;
  82. border:1px dashed gray;
  83. padding:10px;
  84. margin:auto;
  85. }
  86.  
  87. form
  88. {
  89. border-bottom:1px solid lightgray;
  90. padding-bottom:10px;
  91. }
  92.  
  93. .inputs
  94. {
  95. /*border:2px solid blue;*/
  96. }
  97.  
  98. .row
  99. {
  100. /*border:1px solid green;*/
  101. height:40px;
  102. line-height: 40px;
  103. }
  104.  
  105. .left {float:left;text-align: left;width:40%;}
  106. .right {float:right;text-align: right;width:60%;}
  107.  
  108. input[type="text"], input[type="password"]
  109. {
  110. width:100%;
  111. padding:4px;
  112. border-radius: 3px;
  113. border:1px solid #ccc;
  114. }
  115. </style>
  116. </head>
  117. <body>
  118. <div id="wrapper" class="box">
  119. <form class="box text" method="post" action="">
  120. <div class="box inputs">
  121. <div class="left row box text">Username: </div>
  122. <div class="right row box"><input class="inline text" type="text" name="user" autocomplete="off" placeholder="..." onfocus="this.removeAttribute('readonly');" readonly /></div>
  123. <br style="clear:both">
  124. </div>
  125. <div class="box inputs">
  126. <div class="left row box text">Password: </div>
  127. <div class="right row box"><input class="inline text" type="password" name="pass" autocomplete="off" placeholder="..." onfocus="this.removeAttribute('readonly');" readonly /></div>
  128. <br style="clear:both">
  129. </div>
  130. <input type="submit" value="Login" style="float:right;margin-top:5px" class="text">
  131. <br style="clear:both">
  132. </form>
  133. <div class="box text" style="padding-top:5px">
  134. <?php echo "Logged in:" . $_SESSION["logged_in"] . "<br>" . "Current User:" . $_SESSION["active_user"];?>
  135. </div>
  136. </div>
  137. </body>
  138. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement