Guest User

Untitled

a guest
Mar 5th, 2018
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.40 KB | None | 0 0
  1. <?php
  2. session_start();
  3. include 'class.php';
  4. $con1=new Connect('root','','localhost','ablashimov');
  5.  
  6.  
  7. if(isset($_POST['enter'])){
  8. $data = array_map('trim', $_POST);
  9. $data = array_map('strip_tags', $data);
  10. $data['password'] = md5($data['password']);
  11. $user = $con1->selectLoginPassword('users', $data);
  12. if($user){
  13. $_SESSION = $user;
  14. }
  15. else{
  16. ?>
  17. <p style='color:red'>Неверный логин и/или пароль</p>
  18. <?php }
  19.  
  20. }
  21. if(!isset($_SESSION['login'])):
  22. ?>
  23. <form method="POST">
  24. <input type='text' name='login' required />
  25. <input type='password' name='password' required />
  26.  
  27. <input type="submit" name='enter' value='Войти'>
  28. </form>
  29. <?php else:?>
  30. ДОбро пожаловать на наш сайт,
  31. <?php
  32. echo $_SESSION['login'].'<br>';
  33. ?>
  34. <a href='profile.php'>ваш профиль</a><br>
  35. <a href='logout.php'>Выйти</a>
  36.  
  37. <?php endif; ?>
  38.  
  39. <?php
  40.  
  41. class Connect
  42. {
  43.  
  44. private $user;
  45. private $pass;
  46. private $host;
  47. private $db;
  48. private $dsn;
  49. private $pdo;
  50.  
  51. public function __construct($userName, $userPassword, $host, $db)
  52. {
  53. $opt=array(
  54. PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION,
  55. PDO::ATTR_DEFAULT_FETCH_MODE=>PDO::FETCH_ASSOC
  56. );
  57. $this->user = $userName;
  58. $this->pass = $userPassword;
  59. $this->host = $host;
  60. $this->db = $db;
  61. $this->pdo = new PDO($this->settings(), $userName, $userPassword,$opt);
  62. }
  63.  
  64. public function getUserName()
  65. {
  66. return $this->user;
  67. }
  68.  
  69. public function getPass()
  70. {
  71. return $this->pass;
  72. }
  73.  
  74. public function getHost()
  75. {
  76. return $this->host;
  77. }
  78.  
  79. public function getDataBase()
  80. {
  81. return $this->db;
  82. }
  83.  
  84. public function settings()
  85. {
  86. $this->dsn = "mysql:host={$this->host};dbname={$this->db}";
  87.  
  88. return $this->dsn;
  89. }
  90.  
  91. public function update($table, $column, $value,$param,$param2)
  92. {
  93. $sql="UPDATE $table SET $column='$value' WHERE $param=$param2";
  94. return $this->pdo->prepare($sql)->execute([':value' => $value]);
  95. }
  96.  
  97. public function insert($table, $column, $value)
  98. {
  99. $sql = "INSERT INTO $table ($column) VALUES(:value);";
  100.  
  101. return $this->pdo->prepare($sql)->execute([':value' => $value]);
  102. }
  103.  
  104. public function delet($table,$param=null,$param2=null)
  105. {
  106. $sql="DELETE FROM $table WHERE $param=$param2";
  107. return $this->pdo->query($sql);
  108. }
  109.  
  110. public function select($arr)
  111. {
  112. $sql = 'SELECT * From users WHERE `password` = "'.$arr['password'].'"';
  113. $stm=$this->pdo->query($sql);
  114. $value = $stm->fetch();
  115. return $value;
  116. }
  117. public function getAll($table) {
  118. $sql = "SELECT * From $table";
  119. $stm=$this->pdo->query($sql);
  120. return $users = $stm->fetchAll();
  121. }
  122. public function multipleSelect($table, $idArr) {
  123. $in=str_repeat('?,',count($idArr)-1).'?';
  124. $sql='SELECT * FROM $table WHERE id IN ($in)';
  125. $stm=$this->pdo->prepare($sql);
  126. $stm->execute($idArr);
  127. $data=$stm->fetchall();
  128. return $data;
  129. }
  130. public function selectLoginPassword($table, $arr) {
  131. $sql = 'SELECT * From `'.$table.'` WHERE `login` = "'.$arr['login'].'" AND `password` = "'.$arr['password'].'"';
  132. $stm=$this->pdo->query($sql);
  133. $value = $stm->fetch();
  134. return $value;
  135. }
  136.  
  137. }
  138. ?>
  139. <?php
  140. session_start();
  141. session_destroy();
  142. header('Location: auth.php'); //переадресовываем пользователя на страницу авторизации
  143. ?>
  144. <?php
  145. session_start();
  146.  
  147. if(!isset($_SESSION['login'])):
  148. echo "Доступ закрыт";
  149. else:
  150. ?>
  151. <h2>профиль пользователя</h2>
  152. <?php
  153. foreach($_SESSION as $key=> $val)
  154. echo $_SESSION[$key].'<br>';
  155.  
  156. endif?>
Add Comment
Please, Sign In to add comment