Guest User

Untitled

a guest
Nov 3rd, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.93 KB | None | 0 0
  1. What we will be going over:
  2. - Secret Token
  3. - Ajax
  4. - PHP
  5. - Unique IDs
  6.  
  7. Create a new database.
  8. [code]
  9. CREATE TABLE `users` (
  10. `uuid` VARCHAR(30) UNIQUE,
  11. `username` VARCHAR(40) UNIQUE,
  12. `password` VARCHAR(40),
  13. `email` VARCHAR(255) UNIQUE
  14. );
  15. [/code]
  16.  
  17. Explination:
  18. [spoiler]
  19. UUID = Unique Userd IDs (30 Possible Chars Long), Unique so there can't be a duplicate
  20. username = Username (40 Possible Chars Long) Unique so there can't be a duplicate
  21. password = Hashed PW, (40 Possible Chars long because thats the size of the hashed string)
  22. email = Email, (255 Possible Chars) Unique so ther can't be a duplicate
  23. [/spoiler]
  24.  
  25. Now create a new folder called cfg in your websites root directory. Inside of that create a php file called db.php.
  26.  
  27. db.php
  28. [php]
  29. <?php
  30. define('DB_HOST', 'IP OF DB');
  31. define('DB_NAME', 'DB NAME');
  32. define('DB_USERNAME', 'DB USERNAME');
  33. define('DB_PASSWORD', 'DB PASSWORD');
  34.  
  35. $odb = new PDO('mysql:host=' . DB_HOST . ';dbname=' . DB_NAME, DB_USERNAME, DB_PASSWORD);
  36. ?>
  37. [/php]
  38.  
  39. Explination:
  40. [spoiler]
  41. define = well, define the info with a string.
  42. $odb = how we access the database object from now on.
  43. [/spoiler]
  44.  
  45. Now either create or goto your login page and add the follow code to the top
  46.  
  47. login.php
  48. [php]
  49. <?php
  50. session_start();
  51. include 'cfg/db.php';
  52. if(isset($_SESSION['uuid'])) {
  53. echo'
  54. <script language="javascript">
  55. window.location.href="dashboard.php"
  56. </script>
  57. ';
  58. }
  59. $token = $_SESSION['token'] = sha1(uniqid(rand(), true));
  60. ?>
  61. [/php]
  62.  
  63. Explination:
  64. [spoiler]
  65. session_start = Starting a PHP Session
  66. include = We are including the DB code so we can access the PDO object.
  67. if statment = if there is a SESSION vairable for uuid this means the user is already logged so we forward them to the dashboard.
  68. token = applies the value of sha1(uniqid(rand(), true)) to the local variable for token and a session variable
  69. [/spoiler]
  70.  
  71. Now create the form and call to the login page.
  72.  
  73. login.php
  74. [php]
  75. <form id="dankForm">
  76. <input name="username" type="text" placeholder="Username">
  77. <input name="password" type="password" placeholder="Password">
  78. <input name="token" type="hidden" value="<?php echo $token; ?>">
  79. <input name="submit" type="submit" value="submit">
  80. </form>
  81.  
  82. <script type="text/javascript">
  83. $("#dankForm").submit(function(e) {
  84. var url = "cfg/login.php"; // the script where you handle the form input. we'll get to this in a second
  85.  
  86. $.ajax({
  87. type: "POST",
  88. url: url,
  89. data: $("#dankForm").serialize(), // serializes the form's elements.
  90. success: function(data)
  91. {
  92. var obj = JSON.parse(data); //parse the json response
  93. if(obj.status == 'success') {
  94. window.location.href = "dashboard.php" //redirect to the panel
  95. } else {
  96. window.location.href = "login.php?e=" + obj.status; // redirect the user to an error handle
  97. }
  98. }
  99. });
  100.  
  101. e.preventDefault(); // avoid to execute the actual submit of the form.
  102. });
  103. [/php]
  104.  
  105. Explination:
  106. [spoiler]
  107. form = REMEMBER THESE NAMES
  108. script = Ajax call to our login page which validates everything.
  109. [/spoiler]
  110.  
  111. As you probably guessed our next step is to create a login.php in cfg folder.
  112.  
  113. cfg/login.php
  114. [php]
  115. <?php
  116. session_start();
  117. include "db.php";
  118. $username = $_POST['username'];
  119. $pass = sha1($_POST['password']);
  120. $result = $odb -> prepare("SELECT * FROM users WHERE username = :username AND password = :password");
  121. if($_POST['token'] == $_SESSION['token']) {
  122. if($result -> execute(array(":username" => $username, ":password" => $pass))) {
  123. if($result -> rowCount() > 0) {
  124. $info = $result -> fetch(PDO::FETCH_ASSOC);
  125. $_SESSION['uuid'] = $info['uuid'];
  126. $_SESSION['username'] = $info['username'];
  127. echo json_encode(array("status" => "success"));
  128. } else {
  129. echo json_encode(array("status" => "Incorrect Credentials"));
  130. }
  131. } else {
  132. echo json_encode(array("status" => "Database Error"));
  133. }
  134. } else {
  135. echo json_encode(array("status" => "Something weird happened!"));
  136. }
  137. ?>
  138. [/php]
  139.  
  140. Explination:
  141. [spoiler]
  142. start the session
  143. include the db object
  144. get the username from the post array
  145. get the password from the post array
  146. check the database for the username and password
  147. if the token in the post fields and session are the same continue else error out
  148. if the execute of the query was good continue else error out
  149. if the number of fields matching are greater than 0 should be 1 contune else error out
  150. fetch the row into a variable
  151. create a session var with the name uuid and store it with the info['uuid']
  152. create a session var with the name username and store it witht he info['username']
  153. echo a serialized string telling us its all good.
  154. [/spoiler]
  155.  
  156. SWEET! That is all you need for a login, now for the register part!
  157.  
  158. Create a file called register.php
  159.  
  160.  
  161. register.php
  162. [php]
  163. <?php
  164. session_start();
  165. include 'cfg/db.php';
  166. if(isset($_SESSION['uuid'])) {
  167. echo'
  168. <script language="javascript">
  169. window.location.href="dashboard.php"
  170. </script>
  171. ';
  172. }
  173. $registerToken = $_SESSION['registerToken'] = sha1(uniqid(rand(), true));
  174. ?>
  175.  
  176. <form id="dankForm">
  177. <input name="username" type="text" placeholder="Username">
  178. <input name="password" type="password" placeholder="Password">
  179. <input name="email" type="email" placeholder="Email">
  180. <input name="registerToken" value="<?php echo $registerToken; ?>" type="hidden">
  181. <input name="submit" type="submit" value="submit">
  182. </form>
  183.  
  184. <script type="text/javascript">
  185. $("#dankForm").submit(function(e) {
  186. var url = "cfg/register.php"; // the script where you handle the form input. we'll get to this in a second
  187.  
  188. $.ajax({
  189. type: "POST",
  190. url: url,
  191. data: $("#dankForm").serialize(), // serializes the form's elements.
  192. success: function(data)
  193. {
  194. var obj = JSON.parse(data); //parse the json response
  195. if(obj.status == 'success') {
  196. window.location.href = "login.php" //redirect to the login so he can login.
  197. } else {
  198. window.location.href = "register.php?e=" + obj.status; // redirect the user to an error handle
  199. }
  200. }
  201. });
  202.  
  203. e.preventDefault(); // avoid to execute the actual submit of the form.
  204. });
  205. [/php]
  206.  
  207. Explination:
  208. [spoiler]
  209. form = REMEMBER THESE NAMES
  210. script = Ajax call to our register page which validates everything.
  211. [/spoiler]
  212.  
  213. As you probably guessed our next step is to create a register.php in cfg folder.
  214.  
  215. cfg/register.php
  216. [php]
  217. <?php
  218. session_start();
  219. include "db.php";
  220. $username = $_POST['username'];
  221. $pass = sha1($_POST['password']);
  222. $email = $_POST['email'];
  223. $result = $odb -> prepare("INSERT INTO users VALUES(:uuid, :user, :password, :email)");
  224. if($_POST['token'] == $_SESSION['token']) {
  225. if($result -> execute(array(":uuid" => generateUUID($username), ":username" => $username, ":password" => $pass, ":email" => $email))) {
  226. if($result -> rowCount() > 0) {
  227. echo json_encode(array("status" => "success"));
  228. } else {
  229. echo json_encode(array("status" => "Incorrect Credentials"));
  230. }
  231. } else {
  232. echo json_encode(array("status" => "Database Error"));
  233. }
  234. } else {
  235. echo json_encode(array("status" => "Something weird happened!"));
  236. }
  237.  
  238. private function generateUUID($username) {
  239. $randomString = "";
  240. $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  241. $charactersLength = strlen($characters);
  242. for ($i = 0; $i < (strlen($username) - 40); $i++) {
  243. $randomString .= $characters[rand(0, $charactersLength - 1)];
  244. }
  245. return $username . $randomString;
  246. }
  247. ?>
  248.  
  249. Explination:
  250. [spoiler]
  251. start the session
  252. include the db object
  253. get the username from the post array
  254. get the password from the post array
  255. prepare to insert into the database
  256. if the token in the post fields and session are the same continue else error out
  257. if the execute of the query was good continue else error out
  258. echo a serialized string telling us its all good.
  259. Generate UUID Function:
  260. Initalize a new empty variable
  261. Field of chars
  262. Get length of chars string
  263. while i is less than 40(uuid max size) - username length
  264. append
  265. return
  266. [/spoiler]
Add Comment
Please, Sign In to add comment