-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprocess.php
More file actions
140 lines (127 loc) · 3.44 KB
/
Copy pathprocess.php
File metadata and controls
140 lines (127 loc) · 3.44 KB
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
<?php
session_start();
require_once("include/connection.php");
if(isset($_POST['action']) and $_POST['action'] == "login")
{
loginAction();
}
else if(isset($_POST['action']) and $_POST['action'] == "register")
{
registerAction();
}
else
{
session_destroy();
header("location: index.php");
}
function loginAction()
{
$errors = NULL;
if(!(isset($_POST['email']) and filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)))
{
$errors[] = "Email is not valid.";
}
if(!(isset($_POST['password']) and strlen($_POST['password']) > 6))
{
$errors[] = "Please double check your password (length must be greater than 6).";
}
if($errors != NULL)
{
$_SESSION['error_messages'] = $errors;
header('location: index.php');
}
else
{
$query = "SELECT * FROM users WHERE email = '{$_POST['email']}' AND password = '" . md5($_POST['password']) . "'";
$users = fetch_all($query);
if(count($users)>0)
{
$_SESSION['logged_in'] = true;
$_SESSION['user']['first_name'] = $users[0]['first_name'];
$_SESSION['user']['last_name'] = $users[0]['last_name'];
$_SESSION['user']['email'] = $users[0]['email'];
header('location: wall.php');
}
else
{
$errors[] = "Invalid login information.";
$_SESSION['error_messages'] = $errors;
header('location: index.php');
}
}
}
function registerAction()
{
$errors = NULL;
//email validation
if(empty($_POST['email']))
{
$errors[] = "Email address cannot be blank.";
}
else if(!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL))
{
$errors[] = "Email address must be valid.";
}
// first namme
if(empty($_POST['first_name']))
{
$errors[] = "First name cannot be blank.";
}
else if (is_numeric($_POST['first_name']))
{
$errors[] = "First name cannot contain numbers.";
}
//last name validation
if(empty($_POST['last_name']))
{
$errors[] = "Last name cannot be blank.";
}
else if (is_numeric($_POST['last_name']))
{
$errors[] = "Last name cannot contain numbers.";
}
//password validation
if (empty($_POST['password']))
{
$errors[] = "Password cannot be blank.";
}
else if (strlen($_POST['password']) < 6)
{
$errors[] = "Password should be at least 6 characters.";
}
//confirm password validation
if(empty($_POST['confirm_password']))
{
$errors[] = "Please confirm your password.";
}
else if ($_POST['password'] != $_POST['confirm_password'])
{
$errors[] = "Passwords do not match.";
}
if(!($errors == NULL))
{
$_SESSION['error_messages'] = $errors;
header("location: index.php");
}
else
{
//see if the email address is already taken
$query = "SELECT * FROM users WHERE email = '".mysql_real_escape_string($_POST['email'])."'";
$users = fetch_all($query);
//see if someone already registered with that email address
if(count($users)>0)
{
$errors[] = "Someone has already registered with that email address. Do you want to log in?";
$_SESSION['error_messages'] = $errors;
header("location: index.php");
}
else
{
$query = "INSERT INTO users (first_name, last_name, email, password, created_at) VALUES ('".mysql_real_escape_string($_POST['first_name'])."', '".mysql_real_escape_string($_POST['last_name'])."', '". mysql_real_escape_string($_POST['email']). "', '" . md5($_POST['password']) . "', NOW())";
mysql_query($query);
$_SESSION['success_message'] = "User was succcessfully created!";
header("location: index.php");
}
}
}
?>