refactor index.php and dependencies

This commit is contained in:
printempw 2016-01-17 15:03:58 +08:00
parent 681d3af79d
commit 35dc181e87
3 changed files with 101 additions and 87 deletions

View File

@ -1,45 +1,33 @@
function checkToken(token, handler) {
$.ajax({
type: "POST",
url: "check.php?action=token",
dataType: "json",
data: {"token":token},
success: function(json) {
handler(json);
}
});
}
function checkForm(type, uname, passwd, passwd2) {
if (type == "login") {
if (uname === "") {
if (uname === "") {
showMsg("alert-warning", "Empty Username!");
$("#uname").focus();
return false;
} else if (passwd === ""){
$("#uname").focus();
return false;
} else if (passwd === ""){
showMsg("alert-warning", "Empty Password!");
$("#passwd").focus();
return false;
$("#passwd").focus();
return false;
} else {
return true;
}
} else if (type == "register") {
if (uname === "") {
if (uname === "") {
showMsg("alert-warning", "Empty Username!");
$("#uname").focus();
return false;
} else if (passwd === ""){
$("#uname").focus();
return false;
} else if (passwd === ""){
showMsg("alert-warning", "Empty Password!");
$("#passwd").focus();
return false;
} else if (passwd2 === ""){
$("#passwd").focus();
return false;
} else if (passwd2 === ""){
showMsg("alert-warning", "Empty Comfirming Password!");
$("#cpasswd").focus();
return false;
} else if (passwd != passwd2){
$("#cpasswd").focus();
return false;
} else if (passwd != passwd2){
showMsg("alert-warning", "Non-equal password comfirming!");
$("#cpasswd").focus();
return false;
$("#cpasswd").focus();
return false;
} else {
return true;
}
@ -47,31 +35,31 @@ function checkForm(type, uname, passwd, passwd2) {
}
// Login Button Click Event
$("body").on("click", "#login", function(){
var uname = $("#uname").val();
var passwd = $("#passwd").val();
var uname = $("#uname").val();
var passwd = $("#passwd").val();
if (checkForm("login", uname, passwd)) {
$.ajax({
type: "POST",
url: "check.php?action=login",
dataType: "json",
data: {"uname":uname,"passwd":passwd},
beforeSend: function() {
showMsg("alert-info", "Logging in...");
$.ajax({
type: "POST",
url: "ajax.php?action=login",
dataType: "json",
data: {"uname":uname,"passwd":passwd},
beforeSend: function() {
showMsg("alert-info", "Logging in...");
},
success: function(json) {
if (json.success == 1) {
docCookies.setItem("uname", uname);
docCookies.setItem("token", json.token);
success: function(json) {
if (json.errno == 0) {
docCookies.setItem("uname", uname, '/');
docCookies.setItem("token", json.token, '/');
if ($("#keep").prop("checked")) {
docCookies.setItem("uname", uname, 604800);
docCookies.setItem("uname", uname, 604800, '/');
// 设置长效 token 7天
docCookies.setItem("token", json.token, 604800);
docCookies.setItem("token", json.token, 604800, '/');
}
showMsg("alert-success", "Logging succeed!");
window.setTimeout("window.location = './user.php'", 1000);
} else {
// window.setTimeout("window.location = './user.php'", 1000);
} else {
showMsg("alert-danger", json.msg);
}
}
}
});
}
@ -79,25 +67,25 @@ $("body").on("click", "#login", function(){
// Register Button Click Event
$("body").on("click", "#register", function(){
var uname = $("#uname").val();
var passwd = $("#passwd").val();
var uname = $("#uname").val();
var passwd = $("#passwd").val();
if (checkForm("register", uname, passwd, $("#cpasswd").val())) {
$.ajax({
type: "POST",
url: "check.php?action=register",
dataType: "json",
data: {"uname":uname,"passwd":passwd},
beforeSend: function() {
showMsg("alert-info", "Registering...");
$.ajax({
type: "POST",
url: "ajax.php?action=register",
dataType: "json",
data: {"uname":uname, "passwd":passwd},
beforeSend: function() {
showMsg("alert-info", "Registering...");
},
success: function(json) {
if (json.success == 1) {
success: function(json) {
if (json.errno == 0) {
showMsg("alert-success", json.msg);
window.setTimeout("window.location = './index.php?action=login&msg=Successfully Registered, please log in.'", 1000);
} else {
} else {
showMsg("alert-danger", json.msg);
}
}
}
});

View File

@ -1,16 +1,5 @@
// Auto login
$(document).ready(function(){
if (docCookies.hasItem("uname") && docCookies.hasItem("token") && $("#login-reg").html() == 'Register') {
checkToken(docCookies.getItem("token"),function(json) {
if (json.success == 1) {
showMsg("alert-success", json.msg);
window.location = "./user.php";
} else {
showMsg("alert-danger", json.msg);
}
});
}
});
// null
$('body').css('height', document.documentElement.clientHeight);

View File

@ -1,5 +1,35 @@
<?php
require "./connect.php";
/**
* @Author: printempw
* @Date: 2016-01-09 21:11:53
* @Last Modified by: prpr
* @Last Modified time: 2016-01-17 14:26:29
*/
session_start();
function __autoload($classname) {
$dir = dirname(__FILE__);
$filename = "$dir/includes/". $classname .".class.php";
include_once($filename);
}
if (getValue('uname', $_COOKIE) && getValue('token', $_COOKIE)) {
$user = new user($_COOKIE['uname']);
if ($_COOKIE['token'] == $user -> getToken()) {
$_SESSION['uname'] = $_COOKIE['uname'];
$_SESSION['token'] = $user -> getToken();
}
}
function getValue($key, $array) {
if (array_key_exists($key, $array)) {
return $array[$key];
}
return false;
}
function echoScript($script) {
echo "<script>".$script."</script>";
}
?>
<!DOCTYPE HTML>
<html>
@ -21,13 +51,13 @@
<li><a href="#">Link</a></li>
</ul> -->
<ul class="nav navbar-nav navbar-right">
<li><a id="login-reg" href="javascript:;"><?php
if ($_GET["action"] == "register") {
echo "Login";
} else {
echo "Register";
}
?></a></li>
<li><a id="login-reg" href="javascript:;"><?php
if (getValue('action', $_GET) == "register") {
echo "Login";
} else {
echo "Register";
}
?></a></li>
</ul>
</div><!-- /.navbar-collapse -->
</div><!-- /.container-fluid -->
@ -71,11 +101,18 @@
<script type="text/javascript" src="./libs/cookie.js"></script>
<script type="text/javascript" src="./assets/js/login_utils.js"></script>
<?php
if ($_GET["action"] == "register") {
echo "<script>changeForm(1);</script>";
if (getValue('action', $_GET) == "register") {
echoScript("changeForm(1);");
}
if ($_GET["msg"]) {
echo "<script>showMsg('alert-warning','".$_GET['msg']."');</script>";
}?>
if ($msg = getValue('msg', $_GET)) {
echoScript("showMsg('alert-warning','".$msg."');");
}
if (getValue('uname', $_SESSION)) {
echoScript("$('.login-title').html('Welcome');");
echoScript("$('#login-form').html('<a href=\"./user/index.php\">User Center</a>');");
echoScript("$('.navbar-right').html('<li><a href=\"javascript:;\">Welcome,".$_SESSION['uname']."!</a><li>');");
}
?>
<script type="text/javascript" src="./assets/js/ajax.js"></script>
</html>