Show sourcecode
The following files exists in this folder. Click to view.
webbsrvprg/exercises/projekt/classes/
card.php
collection.php
filtersort.php
lineup.php
user.php
user.php
111 lines UTF-8 Windows (CRLF)
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
<?php
include("./incl/default.php");
class User
{
private $db;
public function __construct($dbconn)
{
$this->db = $dbconn;
//gör så att man inte behöver ha db hela tiden, man skapar en user med db
}
public function login($email, $password)
{
try {
$sql = "SELECT email, password FROM projektusers";
$stmt = $this->db->prepare($sql);
$stmt->execute();
while ($res = $stmt->fetch(PDO::FETCH_ASSOC)) {
if ($email === $res['email'] && $password === $res['password']) {
$this->autologin($email, $password);
} else
echo "Fel uppgifter!<br>";
}
} catch (PDOException $e) {
echo $sql . "<br />" . $e->getMessage();
}
}
public function register($name, $email, $password)
{
$name = htmlspecialchars($_POST["name"], ENT_QUOTES, 'UTF-8');
$_SESSION["register-date"] = time();
$_SESSION["register-name"] = $name;
$_SESSION["register-email"] = $email;
$_SESSION["register-password"] = $password;
$meddelande = "Ditt namn är: " . $name . "\nDitt epostadress är: " . $email . "\nKlicka på länken nedan för att verifera detta \nhttps://labb.vgy.se/~danieleh/webbsrvprg/exercises/projekt/verify.php?email=" . $email;
//länk med get (epost i länken som då verifierar att du vill skapa nytt)
$mottagare = $email;
$rubrik = "Verifiera ditt konto";
$mejlhuvud = "From: " . $email;
mail($mottagare, $rubrik, $meddelande, $mejlhuvud);
echo "<p>Veriferingslänk skickad till " . $email . "</p>";
echo "<p>================================</p>";
}
public function logout()
{
unset($_SESSION["logIn"]);
unset($_SESSION["email"]);
unset($_SESSION["userid"]);
header("Location: index.php");
}
public function forgot($email, $password)
{
$email = htmlspecialchars($_POST["email"], ENT_QUOTES, 'UTF-8');
$password = htmlspecialchars($_POST["password"], ENT_QUOTES, 'UTF-8');
if (isset($_POST["forgot"])) {
$_SESSION["forgot-date"] = time();
$_SESSION["forgot-email"] = $email;
$_SESSION["forgot-password"] = crypt($password, '$1$somethin$');
$meddelande = "För att skapa ett nytt lösenord till kontot med epostadressen: \n" . $email . "\nKlicka på länken nedan för att välja ett nytt lösenord detta \nhttps://labb.vgy.se/~danieleh/webbsrvprg/exercises/projekt/newpass.php?email=" . $email;
$mottagare = $email;
$rubrik = "Verifiera ditt konto";
$mejlhuvud = "From: " . $email;
mail($mottagare, $rubrik, $meddelande, $mejlhuvud);
echo "<p>Veriferingslänk skickad till " . $email . "</p>";
echo "<p>================================</p>";
}
}
private function autologin($email, $password){
$sql = "SELECT id FROM projektusers
WHERE email = ? AND password = ?";
$stmt = $this->db->prepare($sql);
$data = array($email, $password);
$stmt->execute($data);
while ($res = $stmt->fetch(PDO::FETCH_ASSOC)) {
$_SESSION["logIn"] = true;
$_SESSION["userid"] = $res["id"];
header("Location: index.php");
}
}
public function newpass($email, $password)
{
try {
$email = htmlspecialchars($_SESSION["forgot-email"], ENT_QUOTES, 'UTF-8');
$password = htmlspecialchars($_SESSION["forgot-password"], ENT_QUOTES, 'UTF-8');
$sql = "UPDATE projektusers SET password = ?
WHERE email = ?";
$stmt = $this->db->prepare($sql);
$data = array($password, $email);
$stmt->execute($data);
$this->autologin($email, $password);
} catch (PDOException $e) {
echo $sql . "<br />" . $e->getMessage();
}
}
public function verify($name, $email, $password)
{
try {
$sql = "INSERT INTO projektusers (name, email, password)
VALUES (?, ?, ?)";
$stmt = $this->db->prepare($sql);
$data = array($name, $email, $password);
$stmt->execute($data);
$this->autologin($email, $password);
} catch (PDOException $e) {
echo $sql . "<br />" . $e->getMessage();
}
}
}