-
Notifications
You must be signed in to change notification settings - Fork 0
/
user_auth_fns.php
91 lines (76 loc) · 1.69 KB
/
user_auth_fns.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
<?php
function register($email, $password)
{
//register new person with db
//return true or error message
//connect to db
$conn = db_connect();
//check if username is unique
$result = $conn->query(
"select * from user where email='" .
$email . "'");
if( !$result )
{
throw new Exception('Could not execute query: ' . $conn->error);
}
if($result->num_rows > 0 )
{
throw new Exception(
'That username is taken - go back and choose another one.');
}
//if ok, put in db
$shapwd = sha1($password);
$result = $conn->query(
"insert into user " .
"(email,password)" .
" values ('$email','$shapwd')");
if( !$result )
{
throw new Exception(
'Could not register you in the database. ' .
'Please try again.');
}
$nUserId = get_user_id($email);
createUserAccount($nUserId);
return TRUE;
}
function login($email, $password)
{
//check email and password with db
//if yes, return true
//else throw exception
//connect to db
$conn = db_connect();
//check if email is unique
$result = $conn->query(
"select * from user
where email='$email'" .
" and password= sha1('$password')");
if(!$result)
{
throw new Exception('Could not log you in. Bad password or email?');
}
if($result->num_rows > 0 )
{
return true;
}
else
{
throw new Exception('Could not log you in. Email or password not found.');
}
}
function check_valid_user()
{
$userEmail = NULL;
// see if somebody is logged in and notify them if not
if(isset($_SESSION['valid_user']))
{
$userEmail = $_SESSION['valid_user'];
}
return $userEmail;
}
function logout()
{
session_start();
session_destroy();
}