-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.php
125 lines (107 loc) · 4.12 KB
/
index.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
<?php
require_once('connectvars.php');
// Start the session
session_start();
// Clear the error message
$error_msg = "";
//for Sign up button
if(isset($_POST['signup']))
{
header('Location: signup.php');
}
// If the user isn't logged in, try to log them in
if (!isset($_SESSION['username'])) {
if (isset($_POST['submit'])) {
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
// Grab the user-entered log-in data
$user_username = mysqli_real_escape_string($dbc, trim($_POST['username']));
$user_password = mysqli_real_escape_string($dbc, trim($_POST['password']));
if (!empty($user_username) && !empty($user_password)) {
// Look up the username and password in the database
$query = "SELECT username FROM user WHERE username = '$user_username' AND password = SHA('$user_password')";
$data = mysqli_query($dbc, $query) or die("error");
if (mysqli_num_rows($data) == 1) {
// The log-in is OK so set the user ID and username session vars (and cookies), and redirect to the home page
$row = mysqli_fetch_array($data);
$_SESSION['username'] = $row['username'];
setcookie('username', $row['username'], time() + (60 * 60 * 24 * 30)); // expires in 30 days
$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/view.html';
header('Location: ' . $home_url);
}
else {
// The username/password are incorrect so set an error message
$error_msg = 'Sorry, you must enter a valid username and password to log in.';
}
}
else {
// The username/password weren't entered so set an error message
$error_msg = 'Sorry, you must enter your username and password to log in.';
}
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>TravelEasy</title>
<link rel="stylesheet" type="text/css" href="style.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css">
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/jquery.backstretch.min.js"></script>
</head>
<body>
<style>
.jumbotron{
background-color:rgba(238, 238, 238, 0.56);
margin-top:10%;
}
</style>
<?php
// If the session var is empty, show any error message and the log-in form; otherwise confirm the log-in
if (empty($_SESSION['username'])) {
echo '<p class="error">' . $error_msg . '</p>';
?>
<form method="post" role="form" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<fieldset>
<div class="col-md-4 jumbotron col-xs-8 col-xs-offset-2 col-md-offset-4 row">
<div class="row">
<div class="col-md-offset-3 col-md-6 form-group">
<label for="username">Username:</label>
<input type="text" class="form-control" name="username" value="<?php if (!empty($user_username)) echo $user_username; ?>" /><br />
</div>
</div>
<div class="row">
<div class="col-md-offset-3 col-md-6 form-group">
<label for="password">Password:</label>
<input class="form-control" type="password" name="password" />
</div>
</div>
<input class="col-md-offset-3 btn btn-primary" type="submit" value="Log In" name="submit" />
<input class="col-md-offset-3 btn btn-primary" type="submit" value="Sign Up" name="signup" />
</fieldset>
</div>
</form>
<?php
}
else {
// Confirm the successful log-in
echo('<p class="login">You are logged in as ' . $_SESSION['username'] . '.</p>');
$home_url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . '/view.php';
header('Location: ' . $home_url);
}
?>
</body>
<script type="text/javascript">
$.backstretch([
"t1.jpg",
"t2.jpg",
"t3.jpg"
], {
fade: 2050,
duration: 1000
});
</script>
</html>