Advertisement
Guest User

Untitled

a guest
Feb 9th, 2017
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.43 KB | None | 0 0
  1. <?php
  2. session_start();
  3. if(isset($_POST['login']))
  4. include('/class.login.php');
  5. //$login = new Login();
  6. if(Login()->isLoggedIn()){
  7. echo "Success!";}
  8. else{
  9. Login()->showErrors();}
  10. $token = $_SESSION['token'] = md5(uniqid(mt_rand(),true));
  11. //<--THIS IS LINE 18 ?>
  12. <form method =POST" action="<?php=$_SERVER['PHP_SELF']; ?>">
  13. <table>
  14. <tr><td>Username:</td><td><input type="text" name="username" /></td></tr>
  15. <tr><td>Password:</td><td><input type="password" name="password" /></td></tr>
  16. </table>
  17. <input type="hidden" name="token" value="<?php=$token; ?>" />
  18. <input type="submit name="login" value="Log In" />
  19. </form>
  20.  
  21. <?php
  22. class Login
  23.  
  24. {
  25. private $_id;
  26. private $_username;
  27. private $_password;
  28. private $_passmd5;
  29.  
  30.  
  31. private $_errors;
  32. private $_access;
  33. private $_login;
  34. private $_token;
  35. public function __contruct()
  36. {
  37. $this->_errors = array();
  38. $this->_login = isset($_POST['login'])? 1 : 0;
  39. $this->_access = 0;
  40. $this->_token = $_POST['token'];
  41.  
  42. $this->_id = 0;
  43. $this->_username = ($this->_login)? $this->filter($_POST['username']) : $_SESSION['username'];
  44. $this->_password = ($this->_login)? $this->filter($_POST['password']) : '';
  45. $this->_passmd5 = ($this->_login)? md5($this->_password) : $_SESSION['password'];
  46. }
  47.  
  48. public function isLoggedIn()
  49. {
  50. ($this->_login)? $this->verifyPost() : $this->verifySession();
  51. return $this->_access;
  52. }
  53.  
  54. public function filter($var)
  55. {
  56. return preg_replace('/[^a-zA-Z0-9','',$var);
  57. }
  58.  
  59. public function verifyPost()
  60. {
  61. try{
  62. if(!$this->isTokenValid())
  63. throw new Exception('Invalid Form Submission');
  64. if(!$this->isDataValid())
  65. throw new Exception('Invalid Form Data');
  66. if(!$this->verifyDatabase())
  67. throw new Exception('Invalid Username/Password');
  68.  
  69. $this->_access = 1;
  70. $this->registerSession();
  71. }
  72. catch (Exception $e)
  73. {
  74. $this->_errors[] = $e->getMessage();
  75. }
  76. }
  77.  
  78. public function verifySession()
  79. {
  80. if($this->_sessionExist() && $this->verifyDatabase())
  81. $this->_access = 1;
  82. }
  83.  
  84. public function verifyDatabase()
  85. {
  86. //Database Connection Data
  87. $db_username = "";
  88. $db_password = "";
  89. $db_host = "";
  90. $db_dbname = "";
  91.  
  92. mysql_connect($db_host, $db_username, $db_password) or die(mysql_error());
  93. mysql_select_db($db_dbname) or die(mysql_errorCode());
  94.  
  95. $data = mysql_query("SELECT ID FROM admin_tbl WHERE username = '($this->_username)' AND password = '($this->_passmd5)'");
  96.  
  97. if(mysql_num_rows($data))
  98. {
  99. list($this->_id ) = @array_values(mysql_fetch_assoc($data));
  100. return true;
  101. }
  102. else
  103. {
  104. return false;
  105. }
  106. }
  107.  
  108. public function isDataValid()
  109. {
  110. return (preg_match('/^[a-zA-Z0-9](5,12)$/',$this->_username) && preg_match('/^[a-zA-Z0-9](5,12)$/',$this->_password))? 1 : 0;
  111. }
  112.  
  113. public function isTokenValid()
  114. {
  115. return (!isset($_SESSION['token']) || $this->_token != $SESSION['token'])? 0 : 1;
  116. }
  117.  
  118. public function registerSession()
  119. {
  120. $_SESSION['id'] = $this->_id;
  121. $_SESSION['username'] = $this->_username;
  122. $_SESSION['password'] = $this->_passmd5;
  123. }
  124.  
  125. public function sessionExist()
  126. {
  127. return (isset($_SESSION['username']) && isset($_SESSION['password']))? 1 : 0;
  128. }
  129.  
  130. public function showErrors()
  131. {
  132. echo"<h3>Error</h3>";
  133. foreach($this->_errors as $key=>$value)
  134. echo $value."<br>";
  135. }
  136. }
  137. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement