-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth_process.php
70 lines (53 loc) · 2.18 KB
/
auth_process.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
<?php
session_start();
require_once "config.php";
if(isset($_POST['register'])){
$name = htmlspecialchars($_POST['name']);
$email = htmlspecialchars($_POST['email']);
$phone = htmlspecialchars($_POST['phone']);
$password = htmlspecialchars($_POST['password']);
$check_sql = "SELECT email FROM users WHERE email='$email'";
$check_result = $connection->query($check_sql);
foreach($check_result as $email){}
if(isset($email['email'])){
// email already exists
$email = $email['email'];
$_SESSION['msg'] = " $email is already registered";
header('location: register.php');
}else{
$sql = "INSERT INTO users (name, phone, email, password)
VALUES ('$name', '$phone', '$email', '$password')";
$result = $connection->query($sql);
if($result){
$_SESSION['msg'] = "Account created successfully, please login to your account";
header('location: login.php');
}else{
$_SESSION['msg'] = "Register failed";
header('location: register.php');
}
}
}
// user login
if(isset($_POST['login'])){
$email = htmlspecialchars($_POST['email']);
$password = htmlspecialchars($_POST['password']);
$check_sql = "SELECT * FROM users WHERE email='$email' AND password='$password' limit 1";
$check_result = $connection->query($check_sql);
foreach($check_result as $user){}
if(isset($user)){
$_SESSION['user_id'] = $user['user_id'];
$_SESSION['role'] = $user['role'];
// message
$_SESSION['msg'] = "Logged in successfully";
if(isset($_SESSION['redirect_url'])){
$redirect_url = $_SESSION['redirect_url'];
unset($_SESSION['redirect_url']);
header("location: $redirect_url");
}else{
header('location: index.php');
}
}else{
$_SESSION['msg'] = "Your email & password doesn't match";
header('location: login.php');
}
}