Advertisement
Guest User

Untitled

a guest
May 5th, 2017
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.84 KB | None | 0 0
  1. <?php
  2.  
  3.     /*
  4.     *   Hey! This file processes the given
  5.     *   credentials to see if they're correct!
  6.     *   If something goes wrong, an error code
  7.     *   is returned to the login page.
  8.     */
  9.    
  10. function got_error($id = 0) {
  11.     // This function returns the error...
  12.     Header('HTTP/1.1 500 Access Denied');
  13.     Header('Location: ./?err='.(is_numeric($id) ? $id : 0));
  14.     return;
  15. }
  16.  
  17.  
  18. // Checking the basic stuff here.
  19. if(empty($_POST)) return got_error();
  20. if(empty($_POST['user'])&&empty($_POST['pass'])) return got_error(1);
  21. if(empty($_POST['user'])||empty($_POST['pass'])) return got_error((empty($_POST['user'])?2:3));
  22.  
  23. // Attempt to connect to the MySQL database!.
  24. $conn = @mysql_connect('localhost:3306', 'photofroggy', '-');
  25. if(!$conn) {
  26.     echo mysql_error();return;
  27.     got_error(4);
  28. }
  29. if(!@mysql_select_db('photofroggy_main', $conn)) got_error(4);
  30. // Query the database with the given login details.
  31. $query = @mysql_query(
  32.     'SELECT * FROM user WHERE Username=\''.$_POST['user'].'\' AND Password=\''.sha1($_POST['pass']).'\''
  33. );
  34. if(!$query) got_error(4);
  35. // Get the data from the user table!
  36. $rows = array();
  37. while($row = mysql_fetch_assoc($query)) { $rows[] = $row; }
  38. // Now we know if the login was right or not.
  39. if(empty($rows)) return got_error(5);
  40. // If all is good, we can move on! Here we create an authtoken and get a timestamp.
  41. $token = sha1(time()+microtime());
  42. $ts = time();
  43. // Now we put the token and timestamp in the user table.
  44. $query = mysql_query("UPDATE user SET Authtoken='$token',Timestamp='$ts' WHERE Username='".$row['Username'].'\'');
  45. // An error could occur here...
  46. if(!$query) return got_error(4);
  47. // But we set a cookie if all is well!
  48. setcookie('blogg:user', array('username' => $row['Username'], 'authtoken' => $token), $ts+3600);
  49. // And then we return to the homepage!
  50. Header('HTTP/1.1 200 SUCCESS');
  51. Header('Location: ../');
  52.  
  53. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement