Skip to content

Commit

Permalink
Merge pull request #2 from UMaine-ASAP/Login-Controller
Browse files Browse the repository at this point in the history
Login controller seems good. Like the pull request description said, we may have to change how navigating to urls work.
  • Loading branch information
dyllandry committed Mar 25, 2016
2 parents 49a1cae + c29a873 commit d2a0e06
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 0 deletions.
31 changes: 31 additions & 0 deletions controller/login.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
//begin session
session_start();

//get user file
require_once dirname(__FILE__) . "/../models/user.php";

//get the html page ready to be displayed
$page = file_get_contents(dirname(__FILE__) . '/../views/login.html');
echo $page;
if(isset($_POST['submitLogin'])){ //change submitLogin to the equivalent login.html file

$user = new User(-1); //User with no user id to give
$user->User_Login($_POST['postName'], $_POST['postPassword']); //check for right credentials

//if correct credentials, set SESSION variables and go to correct home page
if($user->userID != -1){
$_SESSION['user'] = $user;
$_SESSION['sessionCheck'] = 'true';
if ($_SESSION['user']->userType == 'Student'){
header("location:student_home.php");
}
else{
header("location:instructor_home.php");
}
}
else {
echo "Wrong Username/Password</br>Please try again.</br>";
}
}
?>
26 changes: 26 additions & 0 deletions models/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class User {
public $password;

public function User($user_id){
//check to see if valid user_id
if($user_id <= -1){
return;
}

$this->userID = $user_id;

$db = GetDB();
Expand Down Expand Up @@ -49,6 +54,27 @@ public function User($user_id){
}
}

public function User_Login($email, $password){
$db = GetDB();

//query for the user in the database using credentials
$query = "SELECT * FROM `user` WHERE `email` = '" . $email . "' AND `password` = '" . $password . "';";
$result = $db->query($query);

//if the result isn't empty
if($result->num_rows != 0){
$user = $result->fetch_array(MYSQLI_BOTH);

$this->userID = $user['userID'];
$this->firstName = $user['firstName'];
$this->lastName = $user['lastName'];
$this->middleInitial = $user['middleInitial'];
$this->userType = $user['userType'];
$this->email = $user['email'];
$this->password = $user['password'];
}
}

public function Save(){
if($this->userID != -1){
$query = "UPDATE `user` SET ";
Expand Down
37 changes: 37 additions & 0 deletions views/login.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!DOCTYPE html>
<html>
<head>
<title>Haggis - Sign In</title>
<link rel="stylesheet" type="text/css" href="stylesheet.css"/>
</head>
<body>
<div id="container">
<div id="headder">
<button type="button">Register</button>
</div><!-- end of headder -->

<div id="login">
<h2>Login</h2>
<form method='POST' name='login form'>
<input type="text" name='postName' placeholder="Username"/>
<p><input type="password" name='postPassword' placeholder="Password"/></p>
<input type="submit" name='submitLogin' value="Login"></input>
</form>
<p><a href="passwordreset.php">forgot your password?</a></p>
</div> <!-- end of form div-->

<div id="footer">


</div><!-- end of footer -->
</div><!-- end of container div -->


</body>






</html>
3 changes: 3 additions & 0 deletions views/passwordreset.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#login{
background:red;
}
10 changes: 10 additions & 0 deletions views/stylesheet.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/*haggis stylesheet*/
body{
border-style:solid;
border-width:1px;
border-color:black;
}

#login{
align:center;
}

0 comments on commit d2a0e06

Please sign in to comment.