Skip to content

Commit

Permalink
Đã thêm chức năng đăng nhập
Browse files Browse the repository at this point in the history
  • Loading branch information
tutv committed May 9, 2015
1 parent d85d073 commit 24b5956
Show file tree
Hide file tree
Showing 12 changed files with 272 additions and 16 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
2 changes: 1 addition & 1 deletion api/getmonhoc.php → api/getData/getmonhoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
require "MonHoc.php";
require "LopMH.php";
require "BuoiHoc.php";
require "../config.php";
require "../../config.php";

$ds_monhoc = array();

Expand Down
14 changes: 14 additions & 0 deletions api/sign/getStatus.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
session_start();
$user = $_SESSION['user'];
if (!isset($user)) {
echoJson(false);
} else {
echoJson(true, $user);
}
function echoJson($status, $user = null) {
header('Access-Control-Allow-Origin: *');
header('Content-Type:application/json; charset=UTF-8');
echo json_encode(["status" => $status, "user" => $user]);
}
?>
55 changes: 55 additions & 0 deletions api/sign/signin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
session_start();
include "../../config.php";
// lay password voi username nhap vao
function getPassword($username) {
global $conn;
$sql = "select pass from nguoidung where email = '$username'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result ) > 0) {
$row = mysqli_fetch_array($result, MYSQL_ASSOC);
$pass = $row['pass'];
return $pass;
}

return false;
}

function getName($username) {
global $conn;
$sql = "select name from nguoidung where email = '$username'";
$result = mysqli_query($conn, $sql);

if (mysqli_num_rows($result ) > 0) {
$row = mysqli_fetch_array($result, MYSQL_ASSOC);
$name = $row['name'];
if ($name == "") {
return $username;
}
return $name;
}

return "Bạn";
}

function echoJson($result) {
echo json_encode(array("result" => $result));
}

if (isset($_POST['user']) and isset($_POST['pass'])) {
$pass = getPassword($_POST['user']);
if ($pass == false) {
echoJson(false);
} else {
if (md5( $_POST['pass']) == $pass) {
$_SESSION['user'] = getName($_POST['user']);
echoJson(true);
} else {
echoJson(false);
}
}
}
else {
echoJson(false);
}
5 changes: 5 additions & 0 deletions api/sign/signout.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?php
session_start();
session_destroy();

echo "Success";
47 changes: 47 additions & 0 deletions css/main.css
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,51 @@ li.head-lesson {
#content-left-listSubject {
overflow-y: scroll;
height: 450px;
}


/* Sign in */
body {
padding-top: 40px;
padding-bottom: 40px;
background-color: #eee;
}

.form-signin {
max-width: 500px;
padding: 15px;
margin: 0 auto;
}
.form-signin .checkbox {
margin-bottom: 10px;
}
.form-signin .checkbox {
font-weight: normal;
}
.form-signin .form-control {
position: relative;
height: auto;
-webkit-box-sizing: border-box;
-moz-box-sizing: border-box;
box-sizing: border-box;
padding: 10px;
font-size: 16px;
}
.form-signin .form-control:focus {
z-index: 2;
}
.form-signin input[type="email"] {
margin-bottom: -1px;
border-bottom-right-radius: 0;
border-bottom-left-radius: 0;
}
.form-signin input[type="password"] {
margin-bottom: 10px;
border-top-left-radius: 0;
border-top-right-radius: 0;
}
/* Sign up */

#hello-user {
font-weight: bold;
}
19 changes: 19 additions & 0 deletions includes/form-signin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<div class="modal fade" id="form-signin" tabindex="-1" role="dialog" aria-labelledby="mySmallModalLabel" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<form class="form-signin" role="form">
<label for="inputEmail" class="sr-only">Email</label>
<input type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
<label for="inputPassword" class="sr-only">Mật khẩu</label>
<input type="password" id="inputPassword" class="form-control" placeholder="Password" required>
<div class="checkbox">
<label>
<input type="checkbox" value="remember-me"> Nhớ tôi
</label>
</div>
<button class="btn btn-lg btn-primary btn-block" type="button" id="btn-signin-submit">Đăng nhập</button>
<span id="result-signin"></span>
</form>
</div>
</div>
</div>
30 changes: 19 additions & 11 deletions includes/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,29 @@
</div>
<div>
<ul class="nav navbar-nav">
<!-- <li class="dropdown"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Admin <span class="caret"></span></a>-->
<!-- <ul class="dropdown-menu">-->
<!-- <li><a href="./admin/formInput.php">Nhập môn học</a></li>-->
<!-- <li><a href="#">Page 1-2</a></li>-->
<!-- <li><a href="#">Page 1-3</a></li>-->
<!-- </ul>-->
<!-- </li>-->
<li><a href="#">Dashboard</a></li>
<!-- <li><a href="#">Page 3</a></li>-->
<!-- <li><a href="#">Dashboard</a></li>-->
<li class="dropdown" id="tool-user"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Công cụ <span class="caret"></span></a>
<ul class="dropdown-menu">
<li id="btn-save-table"><a href="javascript:void();" >Lưu lại</a></li>
<li id="btn-share-table"><a href="javascript:void();" >Chia sẻ</a></li>
<li id="btn-export-table"><a href="javascript:void();" >Xuất file</a></li>
</ul>
</li>
</ul>

<ul class="nav navbar-nav navbar-right">
<li><a href="#"><span class="glyphicon glyphicon-user"></span> Đăng kí</a></li>
<li><a href="#"><span class="glyphicon glyphicon-log-in"></span> Đăng nhập</a></li>
<li class="dropdown" id="signined" style="display: none;" data-target="out"><a class="dropdown-toggle" data-toggle="dropdown" href="#">Xin chào <span id="hello-user"></span><span class="caret"></span></a>
<ul class="dropdown-menu">
<li><a href="#">Chỉnh sửa</a></li>
<li id="btn-signout"><a href="javascript:void();" >Đăng xuất</a></li>
</ul>
</li>
<li id="btn-signup" style="display: none;"><a href="#"><span class="glyphicon glyphicon-user"></span> Đăng kí</a></li>
<li id="btn-signin" style="display: none;"><a href="#" data-target="#form-signin" data-toggle="modal"><span class="glyphicon glyphicon-log-in"></span> Đăng nhập</a></li>
<li><a href="#" title="Xem hướng dẫn sử dụng ứng dụng"><span class="glyphicon glyphicon-bullhorn"></span> Trợ giúp</a></li>
</ul>
</div>
</div>
</nav>

<?php include "form-signin.php"; ?>
4 changes: 2 additions & 2 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
<script src="js/jquery.noty.packaged.min.js"></script>
</head>
<body>
<div id="page" style="padding-top: 55px">
<div id="page" style="padding-top: 13px">

<!--Lấy header-->
<?php include("includes/header.php"); ?>
<?php include "includes/header.php"; ?>

<!--Phần thân của Thời khóa biểu-->
<div id="content">
Expand Down
112 changes: 110 additions & 2 deletions js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ function infoLesson() {
// Get dữ liệu từ phía server
$(document).ready(function () {
$.ajax({
url : "./api/getmonhoc.php",
url : "./api/getData/getmonhoc.php",
method : "GET",
dataType: "json",
success : function(data) {
Expand Down Expand Up @@ -160,6 +160,21 @@ $(document).ready(function () {
},
cache: true
});


//Kiểm tra trạng thái đăng nhập?
$.ajax({
url: "api/sign/getStatus.php",
method: "GET",
dataType: "json",
success: function(data) {
if (data.status == true) {
signin();
} else {
signout();
}
}
});
});

//Thêm lớp môn học vào lịch tuần
Expand Down Expand Up @@ -371,4 +386,97 @@ function thongBao(text, type) {
speed: 500 // unavailable - no need
}
});
}
}

$("#btn-signin-submit").click(function () {
var user = $("#inputEmail").val();
var pass = $("#inputPassword").val();

$.ajax({
url : "api/sign/signin.php",
method : "POST",
dataType: "json",
data : {user: user, pass: pass},
success : function(data) {
var resultDOM = $("#result-signin");
if (data.result == true) {
$("#form-signin").modal("hide");
signin();
} else {
resultDOM.text("Đăng nhập thất bại");
resultDOM.addClass("bg-danger");
resultDOM.show();

}
}
});
});

$("#btn-signout").click(function() {
$.get("api/sign/signout.php");
signout();
});

function signout() {
var signinDOM = $("#signined");
signinDOM.hide();
signinDOM.attr("data-target", "out");
$("#btn-signup").show();
$("#btn-signin").show();
}

function signin() {
$.ajax({
url: "api/sign/getStatus.php",
method: "GET",
dataType: "json",
success: function(data) {
if (data.status == true) {
$("#hello-user").text(data.user);
var signinDOM = $("#signined");
signinDOM.show();
signinDOM.attr("data-target", "in");
$("#btn-signup").hide();
$("#btn-signin").hide();
}
}
});
}

function getStatusSign() {
var status = $("#signined").attr("data-target");
return (status == "in");
}

$("#btn-save-table").click(function () {
var sign = getStatusSign();
if (!sign) {
$("#form-signin").modal("show");
return;
}

//Nếu đã đăng nhập thì xử lý theo tool
alert("Chức năng đang hoàn thiện");
});

$("#btn-share-table").click(function () {
var sign = getStatusSign();
if (!sign) {
$("#form-signin").modal("show");
return;
}

//Nếu đã đăng nhập thì xử lý theo tool
alert("Chức năng đang hoàn thiện");
});

$("#btn-export-table").click(function () {
var sign = getStatusSign();
if (!sign) {
$("#form-signin").modal("show");
return;
}

//Nếu đã đăng nhập thì xử lý theo tool
alert("Chức năng đang hoàn thiện");
});

0 comments on commit 24b5956

Please sign in to comment.