-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.php
160 lines (125 loc) · 4.66 KB
/
login.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
<?php
// kõik funktsioonid, kus tegeleme AB'iga
require_once("functions.php");
//kui kasutaja on sisseloginud,
//siis suuunan data.php lehele
if(isset($_SESSION["logged_in_user_id"])){
header("Location: data.php");
}
// muuutujad errorite jaoks
$email_error = "";
$password_error = "";
$create_email_error = "";
$create_password_error = "";
// muutujad väärtuste jaoks
$email = "";
$password = "";
$create_email = "";
$create_password = "";
if($_SERVER["REQUEST_METHOD"] == "POST") {
// *********************
// **** LOGI SISSE *****
// *********************
if(isset($_POST["login"])){
if ( empty($_POST["email"]) ) {
$email_error = "See väli on kohustuslik";
}else{
// puhastame muutuja võimalikest üleliigsetest sümbolitest
$email = cleanInput($_POST["email"]);
}
if ( empty($_POST["password"]) ) {
$password_error = "See väli on kohustuslik";
}else{
$password = cleanInput($_POST["password"]);
}
// Kui oleme siia jõudnud, võime kasutaja sisse logida
if($password_error == "" && $email_error == ""){
echo "Võib sisse logida! Kasutajanimi on ".$email." ja parool on ".$password;
$hash = hash("sha512", $password);
// kasutaja sisselogimise fn, failist functions.php
$login_response = $User->loginUser($email, $hash);
//kasutaja logis edukalt sisse
if(isset($login_response->success)){
// id, emaili
$_SESSION["logged_in_user_id"] = $login_response->user->id;
$_SESSION["logged_in_user_email"] = $login_response->user->email;
$_SESSION["login_success_message"] = $login_response->success->message;
header("Location: data.php");
}
}
} // login if end
// *********************
// ** LOO KASUTAJA *****
// *********************
if(isset($_POST["create"])){
if ( empty($_POST["create_email"]) ) {
$create_email_error = "See väli on kohustuslik";
}else{
$create_email = cleanInput($_POST["create_email"]);
}
if ( empty($_POST["create_password"]) ) {
$create_password_error = "See väli on kohustuslik";
} else {
if(strlen($_POST["create_password"]) < 8) {
$create_password_error = "Peab olema vähemalt 8 tähemärki pikk!";
}else{
$create_password = cleanInput($_POST["create_password"]);
}
}
if( $create_email_error == "" && $create_password_error == ""){
// räsi paroolist, mille salvestame ab'i
$hash = hash("sha512", $create_password);
echo "Võib kasutajat luua! Kasutajanimi on ".$create_email." ja parool on ".$create_password." ja räsi on ".$hash;
// kasutaja loomise fn, failist functions.php,
// saadame kaasa muutujad
// fn User klassist
$create_response = $User->createUser($create_email, $hash);
}
} // create if end
}
// funktsioon, mis eemaldab kõikvõimaliku üleliigse tekstist
function cleanInput($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<!DOCTYPE html>
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Log in</h2>
<?php if(isset($login_response->error)): ?>
<p style="color:red;">
<?=$login_response->error->message;?>
</p>
<?php elseif(isset($login_response->success)): ?>
<p style="color:green;" >
<?=$login_response->success->message;?>
</p>
<?php endif; ?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" >
<input name="email" type="email" placeholder="E-post" value="<?php echo $email; ?>"> <?php echo $email_error; ?><br><br>
<input name="password" type="password" placeholder="Parool" value="<?php echo $password; ?>"> <?php echo $password_error; ?><br><br>
<input type="submit" name="login" value="Log in">
</form>
<h2>Create user</h2>
<?php if(isset($create_response->error)): ?>
<p style="color:red;">
<?=$create_response->error->message;?>
</p>
<?php elseif(isset($create_response->success)): ?>
<p style="color:green;" >
<?=$create_response->success->message;?>
</p>
<?php endif; ?>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="post" >
<input name="create_email" type="email" placeholder="E-post" value="<?php echo $create_email; ?>"> <?php echo $create_email_error; ?><br><br>
<input name="create_password" type="password" placeholder="Parool"> <?php echo $create_password_error; ?> <br><br>
<input type="submit" name="create" value="Create user">
</form>
<body>
<html>