-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauthentication.php
170 lines (164 loc) · 8.68 KB
/
authentication.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
ob_start();
session_start();
include "init.php";
if(isset($_SESSION['userLogedIn'])){
header('Location:index.php');
}
else
{
if($_SERVER['REQUEST_METHOD'] == 'POST')
{
if(isset($_POST['signIn']))
{
$username = $_POST['signInUsername'];
$password = $_POST['signInPassword'];
$hashedPassword = sha1($password);
$stmt = $cnx->prepare("SELECT * FROM users WHERE username = ? AND password = ?");
$stmt->execute(array($username,$hashedPassword));
$rows = $stmt->fetch();
if($stmt->rowCount() > 0)
{
$_SESSION['userLogedIn'] = $username;
$_SESSION['userId'] = $rows['userId'];
header('Location:index.php');
exit();
}
}
else
{
$sentUserName = $_POST['#Username'];
$sentPassword = $_POST['#Password'];
$sentConfirmPassword = $_POST['#ConfirmPassword'];
$sentEmail = $_POST['#Email'];
$requestRulesErrors = array();
if(isset($sentUserName)){
$filteredUserName = filter_var($sentUserName,FILTER_SANITIZE_STRING);
if(strlen($filteredUserName) < 4){
$requestRulesErrors[] = 'username should be more than 4 characters!';
}
}
if(isset($sentPassword) && isset($sentConfirmPassword)){
if(empty($sentPassword))
{
$requestRulesErrors[] = 'password should not be empty!';
}
if($sentPassword !== $sentConfirmPassword){
$requestRulesErrors[] = 'password and confirmation not match!';
}
}
if(isset($sentEmail)){
$filteredEmail = filter_var($sentEmail, FILTER_SANITIZE_EMAIL);
if(filter_var($filteredEmail,FILTER_VALIDATE_EMAIL) != true){
$requestRulesErrors[] = 'sorry , email is not valide';
}
}
if(empty($requestRulesErrors)){
$exists = checkItem('username','users',$filteredUserName);
if($exists > 0){
$requestRulesErrors[] = "Sorry this username already exists";
}
else
{
$stmt = $cnx->prepare("INSERT INTO users(username,password,email,registred_at) VALUES (:username,:password,:email,now())");
$stmt->execute(array(
'username' => $filteredUserName,
'password' => sha1($sentPassword),
'email' => $filteredEmail,
));
if($stmt->rowCount()>0){
$successMessage = "Congrats you has been registred successfully!";
}
else{
$requestRulesErrors = "Unfortunately your account has not been registered , try again";
}
}
}
}
}
}
?>
<div class='container authentication-page'>
<h1 class='text-center'><span class="selected" data-class="signIn">SignIn</span> | <span data-class="#" >#</span></h1>
<form class="signIn" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
<div class="input-container">
<input type="text"
class="form-control"
id="inputSignInUserName"
name="signInUsername"
placeholder="username"
autocomplete="off"
required="required">
</div>
<div class="input-container">
<input type="password"
class="form-control"
id="inputSignInPassword"
name="signInPassword"
placeholder="password"
autocomplete="new-password"
required="required">
</div>
<input type="submit" value="signIn" name="signIn" class="btn btn-primary btn-block">
</form>
<form class="#" action="<?php echo $_SERVER['PHP_SELF'] ?>" method="POST">
<div class="input-container">
<input type="text"
pattern='.{4,}'
title='username should be more than 4 chars'
class="form-control"
id="input#UserName"
name="#Username"
placeholder="username"
autocomplete="off"
required>
</div>
<div class="input-container">
<input type="password"
minlength="4"
class="form-control"
id="input#Password"
name="#Password"
placeholder="password"
autocomplete="new-password"
required>
</div>
<div class="input-container">
<input type="password"
class="form-control"
id="input#ConfirmPassword"
name="#ConfirmPassword"
placeholder="confirm password"
autocomplete="new-password"
required>
</div>
<div class="input-container">
<input type="email"
class="form-control"
id="input#Email"
name="#Email"
placeholder="email"
autocomplete="off"
required>
</div>
<input type="submit" value="#" name="#" class="btn btn-success btn-block">
</form>
<div class="messages text-center">
<?php
if(!empty($requestRulesErrors))
{
foreach ($requestRulesErrors as $error) {
echo "<div class='message error'>$error</div>";
}
}
if(isset($successMessage))
{
echo "<div class='message success'>$successMessage</div>";
}
?>
</div>
</div>
<?php
include $tpl . "_footer.php";
ob_end_flush();
?>