-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.php
More file actions
74 lines (68 loc) · 2.42 KB
/
Copy pathauth.php
File metadata and controls
74 lines (68 loc) · 2.42 KB
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
<?php
class Auth{
public static $db;
private $host = "mysql:host=localhost;dbname=login";
private $user = "root";
private $pass = "";
private static $query;
private static $statement;
private static $user_data;
function __construct(){
session_start();
try{
self::$db = new PDO($this->host, $this->user, $this->pass);
}catch (PDOException $e){
echo $e->getMessage();
}
}
public static function Login($array){
self::$query = "SELECT * FROM users WHERE email = '".$array['email']."' LIMIT 1";
self::$statement = self::$db->prepare(self::$query);
self::$statement->execute();
self::$user_data = self::$statement->fetch(PDO::FETCH_OBJ);
if(self::$user_data != null){
if(self::$user_data->password == self::Hashing($array['pass'])){
$_SESSION["Logged"] = true;
$_SESSION['type'] = "success";
}else{
$_SESSION["Wrong_password"] = true;
$_SESSION['type'] = "fail";
}
}else{
$_SESSION["User_exist_false"] = true;
$_SESSION['type'] = "fail";
}
return header('Location: http://localhost/login');
}
public static function Register($array){
self::$query = "SELECT * FROM users WHERE email = '".$array['email']."' LIMIT 1";
self::$statement = self::$db->prepare(self::$query);
self::$statement->execute();
self::$user_data = self::$statement->fetch(PDO::FETCH_OBJ);
if(self::$user_data == null){
$array['pass'] = self::Hashing($array['pass']);
self::$query = "INSERT
INTO users (name, email, password)
VALUES ('".$array['name']."', '".$array['email']."', '".$array['pass']."')";
self::$statement = self::$db->prepare(self::$query);
self::$statement->execute();
$_SESSION["Register"] = true;
$_SESSION['type'] = "success";
}else{
$_SESSION["User_exist"] = true;
$_SESSION['type'] = "fail";
}
return header('Location: http://localhost/login');
}
private static function Hashing($pass): string{
$hash = md5($pass);
return $hash;
}
}
$login = new Auth();
$type = $_GET['type'];
if($type == 'login'){
Auth::Login($_POST);
}else if($type='register'){
Auth::Register($_POST);
}