fix User.class.php and update example
This commit is contained in:
1 parent
e261a78f06
commit
c3db2b32b9
3 files changed
+98
-55
No files matched your search
+3
-2
@@ -5,8 +5,9 @@
|
||||
rsgrinko@gmail.com
|
||||
https://it-stories.ru
|
||||
*/
|
||||
require_once $_SERVER['DOCUMENT_ROOT'].'/core/session.php';
|
||||
require_once $_SERVER['DOCUMENT_ROOT'].'/core/lib/DataBase.class.php';
|
||||
require_once $_SERVER['DOCUMENT_ROOT'].'/core/lib/User.class.php';
|
||||
|
||||
$DataBase = new DataBase('localhost', 'db_user', 'password', 'db_name');
|
||||
$User = new User();
|
||||
$DataBase = new DataBase('localhost', 'rsgrinko_iot', '2670135', 'rsgrinko_iot');
|
||||
$User = new User();
|
||||
+64
-20
@@ -1,10 +1,10 @@
|
||||
<?php
|
||||
/*
|
||||
Класс для работы с пользователями
|
||||
Роман Сергеевич Гринько
|
||||
rsgrinko@gmail.com
|
||||
https://it-stories.ru
|
||||
*/
|
||||
/*
|
||||
Класс для работы с пользователями
|
||||
Роман Сергеевич Гринько
|
||||
rsgrinko@gmail.com
|
||||
https://it-stories.ru
|
||||
*/
|
||||
|
||||
class User {
|
||||
public $id;
|
||||
@@ -12,13 +12,21 @@ class User {
|
||||
|
||||
public function __construct($users_table = 'users') {
|
||||
$this->users_table = $users_table;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
// Получение всех полей пользователя
|
||||
public function getFields($id){
|
||||
public function getFields($id = ''){
|
||||
global $DataBase;
|
||||
$result = $DataBase->getItem($this->users_table, array('id'=>$id));
|
||||
if(empty($id) or $id == ''){
|
||||
$id = $this->id;
|
||||
}
|
||||
|
||||
if(is_array($id)){
|
||||
$where = $id;
|
||||
} else {
|
||||
$where = array('id'=>$id);
|
||||
}
|
||||
$result = $DataBase->getItem($this->users_table, $where);
|
||||
if($result) {
|
||||
return $result;
|
||||
} else {
|
||||
@@ -26,27 +34,63 @@ class User {
|
||||
}
|
||||
}
|
||||
|
||||
// Метод выполняет авторизацию пользователя в системе по ID
|
||||
public function Authorize($id){
|
||||
global $DataBase;
|
||||
$result = $DataBase->getItem($this->users_table, array('id'=>$id));
|
||||
|
||||
if($result) {
|
||||
setcookie('authorize', 'Y', time()+3600*24);
|
||||
setcookie('login', $result['login'], time()+3600*24);
|
||||
setcookie('password', md5($result['password']), time()+3600*24);
|
||||
|
||||
$this->id = $id;
|
||||
$_SESSION['authorize'] = 'Y';
|
||||
$_SESSION['login'] = $result['login'];
|
||||
$_SESSION['password'] = $result['password'];
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Метод выполняет авторизацию пользователя в системе по логину и паролю
|
||||
public function SecurityAuthorize($login, $password){
|
||||
global $DataBase;
|
||||
|
||||
$result = $DataBase->getItem($this->users_table, array('login'=>$login, 'password' => $password));
|
||||
if($result) {
|
||||
$this->id = $result['id'];
|
||||
$_SESSION['authorize'] = 'Y';
|
||||
$_SESSION['login'] = $result['login'];
|
||||
$_SESSION['password'] = $result['password'];
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Проверка на пользователя
|
||||
public function is_user(){
|
||||
global $DataBase;
|
||||
|
||||
$result = $DataBase->getItem($this->users_table, array('login'=>$_SESSION['login']));
|
||||
if($result) {
|
||||
if($result['password']==$_SESSION['password']){
|
||||
$DataBase->update($this->users_table, array('id'=>$result['id']), array('last_active' => time()));
|
||||
$this->id = $result['id'];
|
||||
return true;
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
// Метод выхода из системы
|
||||
public function Logout(){
|
||||
|
||||
setcookie('authorize', 'N', time()-3600);
|
||||
setcookie('login', '', time()-3600);
|
||||
setcookie('password', '', time()-3600);
|
||||
|
||||
$_SESSION['authorize'] = '';
|
||||
$_SESSION['login'] = '';
|
||||
$_SESSION['password'] = '';
|
||||
session_unset();
|
||||
session_destroy();
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
+31
-33
@@ -1,41 +1,39 @@
|
||||
<?php
|
||||
/*
|
||||
Пример использования класса
|
||||
Роман Сергеевич Гринько
|
||||
rsgrinko@gmail.com
|
||||
https://it-stories.ru
|
||||
*/
|
||||
require_once $_SERVER['DOCUMENT_ROOT'].'/core/bootstrap.php';
|
||||
|
||||
// DataBase class
|
||||
if($_REQUEST['act']=='login'){
|
||||
if(!$User->SecurityAuthorize($_REQUEST['login'], $_REQUEST['password'])) {
|
||||
echo 'login fail!<br>';
|
||||
}
|
||||
}
|
||||
|
||||
// Query
|
||||
$result = $DataBase->query('SELECT * FROM iot_sensors WHERE ID=3924538');
|
||||
echo '<pre>'.print_r($result, true).'</pre>';
|
||||
|
||||
// Update
|
||||
$result = $DataBase->update('iot_sensors',
|
||||
array( //WHERE ID=3924538
|
||||
'ID' => '3924538'
|
||||
),
|
||||
array( //SET VALUE=601, SENSOR=test_sensor2
|
||||
'VALUE' => '601',
|
||||
'SENSOR' => 'test_sensor2'
|
||||
)
|
||||
);
|
||||
echo '<pre>'.print_r($result, true).'</pre>';
|
||||
if($_GET['act']=='logout') {
|
||||
$User->Logout();
|
||||
}
|
||||
|
||||
|
||||
// Get items
|
||||
$result = $DataBase->getItems('iot_sensors',
|
||||
array(
|
||||
'VALUE'=>'>32' // WHERE VALUE>32
|
||||
),
|
||||
array(
|
||||
'VALUE'=>'DESC' // ORDER BY VALUE DESC
|
||||
));
|
||||
echo '<pre>'.print_r($result, true).'</pre>';
|
||||
if($User->is_user()):
|
||||
$user_fields = $User->getFields();
|
||||
?>
|
||||
<p>Добро пожаловать, <?=$user_fields['name'];?>! <a href="example.php?act=logout">Выход</a></p>
|
||||
<pre><?=print_r($user_fields, true);?></pre>
|
||||
<?php
|
||||
else:
|
||||
?>
|
||||
<form action="example.php" method="POST">
|
||||
<input type="hidden" name="act" value="login">
|
||||
<input type="text" name="login" placeholder="Enter your login"><br>
|
||||
<input type="password" name="password" placeholder="Enter your password"><br>
|
||||
<input type="submit" value="Login"><br>
|
||||
</form>
|
||||
<?php
|
||||
endif;
|
||||
|
||||
|
||||
// users class
|
||||
|
||||
$User->Authorize(1); //set cookies
|
||||
|
||||
$result = $User->getFields(1); //Get fields for user id=1
|
||||
echo '<pre>'.print_r($result, true).'</pre>';
|
||||
|
||||
$User->Logout(); //remove cookies
|
||||
echo '<pre>'.session_id().'</pre>';
|
||||
Reference in new issue
Block a user