-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
129 lines (110 loc) · 4.64 KB
/
functions.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
<?php
// Used at the welcome page > welcome.php
function sayHello(){
global $user_profile;
if (isset($user_profile['first_name'])){
$name = $user_profile['first_name'];
}
elseif (isset($_SESSION['username'])){
$name = $_SESSION['username'];
}
else{
$name = 'Dear guest';
}
echo $name;
}
// Used at the Sign in page > index.php
function logIn($submit){
global $resultQuery;
if (isset($_POST[$submit]))
{
$connect = new DB();
$where = array (
'email' => mysql_real_escape_string($_POST['useremail']),
'pass' => mysql_real_escape_string(md5($_POST['userpass']))
);
$connect->selectWhere('users', $where);
// Check username and password match
if (mysql_num_rows($resultQuery) == 1) {
// Set user email session variable
$_SESSION['useremail'] = $_POST['useremail'];
// Call user's name and set it to session
$usname = mysql_fetch_array($resultQuery);
$_SESSION['username'] = $usname['namefirst'];
header('Location: welcome.php');
}
else {
echo "<div class=\"error\">The username or password you entered is incorrect</div>";
}
}
}
// Used at the welcome page > welcome.php
function logout(){
if (isset($_GET['logout']))
{
unset($_SESSION['useremail']);
unset($_SESSION['username']);
session_destroy();
header('Location: index.php');
}
}
// Used at the Register page > register.php
function createAccount(){
if (isset($_POST['crtAcc']))
{
$connect = new DB();
$email = strtolower(mysql_real_escape_string($_POST['useremail']));
$pass = mysql_real_escape_string(md5($_POST['userpass']));
$nameF = mysql_real_escape_string($_POST['userFirstName']);
$nameL = mysql_real_escape_string($_POST['userLastName']);
if ( $email !="" && $pass != "" && $nameF != "" && $nameL != "" )
{
$connect->select('users', 'email');
// Check email is exsit or not
$match = isMatch('email',$email);
switch ($match)
{
case true :
die('<div class="error">email address is aleady exist please try with another email address</div>');
break;
// Register new user at the database
case null :
$values = array ('email' => $email
, 'pass' => $pass
, 'namefirst' => $nameF
, 'namelast' => $nameL
);
$connect->insert('users', $values);
// Set username session variable
$_SESSION['username'] = $nameF;
// Set user email session variable
$_SESSION['useremail'] = $_POST['useremail'];
// Jump to secured page
header('Location: welcome.php');
break;
}
}
echo '<div class="error">Please fill the records completely</div>';
}
}
// Check is the field match with the selected column's row or not
function isMatch($column, $field){
global $resultQuery;
$match = null;
while ($row = mysql_fetch_array($resultQuery))
{
if ($row[$column] == $field){ $match = true; }
}
return $match;
}
// Page title
function pageTitle(&$subtitle){
echo $subtitle = 'My Site » ' . $subtitle;
}
// Get registering form values
function getValue($value){
global $user_profile;
(isset($user_profile[$value]))? $value = $user_profile[$value] : $value = '';
echo $value;
}
?>