-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.php
80 lines (67 loc) · 1.99 KB
/
auth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* Created by PhpStorm.
* User: Rzkan
* Date: 06/11/2018
* Time: 20:56
*/
require_once('config.php');
class auth{
private $conn;
function __construct(){
$conf = new config();
$this->conn = $conf->getConnection();
}
public function login(){
if (isset($_POST['login'])) {
if (empty($_POST['email']) || empty($_POST['pass'])) {
header("location: login");
}else{
$username=$_POST['email'];
$password=$_POST['pass'];
$db = $this->conn;
$sql = "SELECT * FROM user WHERE email='$username' AND pass='$password'";
$res = $db->prepare($sql);
$res->execute();
$row = $res->rowCount();
if($row == 1){
$_SESSION['login_user']=$username;
$this->sessionKeeper();
header("location: index");
}else{
header("location: login");
}
}
}
}
public function register(){
if(isset( $_POST['register'])){
$email = $_POST['email'];
$name = $_POST['name'];
$pass = $_POST['pass'];
$db = $this->conn;
$sql = "INSERT INTO user(name, email, pass) VALUES('$name', '$email', '$pass')";
$db->exec($sql);
header("location: index");
}
}
public function logout(){
if(session_destroy()){
header("Location: index");
}
}
function sessionKeeper(){
$userCheck = $_SESSION['login_user'];
$db = $this->conn;
$sql = "SELECT * FROM user WHERE email='$userCheck'";
$res = $db->prepare($sql);
$res->execute();
$row = $res->fetchAll();
foreach ($row as $a){
$id = $a['id'];
$name = $a['name'];
}
$_SESSION['name_user'] = $name;
$_SESSION['id_user'] = $id;
}
}