-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocess.php
More file actions
132 lines (123 loc) · 3.13 KB
/
process.php
File metadata and controls
132 lines (123 loc) · 3.13 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
<?php
include('new-connection.php');
session_start();
if(isset($_POST['action']) && ($_POST['action']) == 'register')
{
$errors = array();
if(empty($_POST['first_name']))
{
$errors[] = 'You need to enter your first name!';
}
if(empty($_POST['last_name']))
{
$errors[] = 'You need to enter your last name!';
}
if(empty($_POST['email']) || (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)))
{
$errors[] = 'You need to enter a valid email address!';
}
if(strlen($_POST['password']) < 6)
{
$errors[] = 'Your password must be at least 6 characters!';
}
if($_POST['confirm'] != $_POST['password'])
{
$errors[] = 'Your password needs to match!';
}
if(count($errors) > 0)
{
$_SESSION['errors'] = $errors;
header('location:index.php');
die();
}
else
{
$_SESSION['success'] = 'Success';
adduser();
header('location:index.php');
die();
}
}
elseif(isset($_POST['action']) && ($_POST['action']) == 'login')
{
login_user();
}
elseif(isset($_POST['action']) && ($_POST['action']) == 'message')
{
if(empty($_POST['message']))
{
$_SESSION['errors'][] = "You can't post an empty message!";
header('location:main.php');
}
else
{
add_message();
header('location:main.php');
}
}
elseif(isset($_POST['action']) && ($_POST['action']) == 'comment')
{
if(empty($_POST['comment']))
{
$_SESSION['errors'][] = "You can't post an empty comment!";
header('location:main.php');
}
else
{
add_comment();
header('location:main.php');
}
}
else
{
session_destroy();
header('location:index.php');
die();
}
function adduser()
{
$fname = escape_this_string($_POST['first_name']);
$lname = escape_this_string($_POST['last_name']);
$email = escape_this_string($_POST['email']);
$password = md5($_POST['password']);
$query = "INSERT INTO users (first_name, last_name, email, password, created_at, updated_at) VALUES ('{$fname}', '{$lname}', '{$email}', '{$password}', NOW(), NOW())";
run_mysql_query($query);
}
function login_user()
{
$email = escape_this_string($_POST['email']);
$password = md5($_POST['password']);
$query = "SELECT * FROM users WHERE email = '{$email}' AND password = '{$password}'";
$user = fetch_all($query);
if(count($user) > 0)
{
$_SESSION['user_id'] = $user[0]['id'];
$_SESSION['first_name'] = $user[0]['first_name'];
$_SESSION['logged_in'] = TRUE;
header('location:main.php');
}
else
{
$_SESSION['errors'][] = "No such user with these credentials!";
header('location:index.php');
die();
}
}
function add_message()
{
$user_id = escape_this_string($_SESSION['user_id']);
$message = escape_this_string($_POST['message']);
$query = "INSERT INTO messages (message, created_at, updated_at, users_id) VALUES ('{$message}', NOW(), NOW(), {$user_id})";
run_mysql_query($query);
}
function add_comment()
{
// $query1 = "SELECT messages.id FROM messages";
// $messages = fetch_all($query1);
$user_id = $_SESSION['user_id'];
$message_id = $_POST['messages_id'];
$comment = escape_this_string($_POST['comment']);
$query2 = "INSERT INTO comments (comment, created_at, updated_at, users_id, messages_id) VALUES ('{$comment}', NOW(), NOW(), {$user_id}, {$message_id})";
run_mysql_query($query2);
}
?>