如何解决由于 Javascript,无法登录托管网站
我尝试将我的网站托管给提供商,但它似乎不想在那里登录。执行登录操作...我可以成功注册,更改密码,所以基本上我有数据库连接,但我无法登录网站...我应该修改什么?这是我的代码:
如果我在网站链接中输入例如https://example.com/login.php?enterID=123&password=123,我可以获得很好的响应,但它似乎不允许我登录网站..
登录.php:
<?php
include "mysql-connect.php";
//get Info from login.html
$ID = $_GET['enterID'];
$PW = $_GET['password'];
$stmt = $connect->prepare("SELECT PW,userType,nickName FROM users WHERE ID = ?");
$stmt->bind_param("s",$ID);
$valid = $stmt->execute();
if (!$valid){
die("Could not successfully run query.". $connect->connect_error);
}
$result = $stmt->get_result();
if ($result->num_rows==0){
//display message of no such student/teacher/admin
echo "Failed to find an account with the input ID.";
} else {
$row = $result->fetch_assoc();
if ($PW == $row['PW']) {
$type = $row['userType'];
$nick = $row['nickName'];
//save data,record cookie for 6hours
setcookie("type",$type,time() + 21600,'/');
setcookie("userID",$ID,'/');
setcookie("nickName",$nick,'/');
//login success - Request.responseText to checklogin.js
echo $type;
} else {
//display message of password error
echo "The input password does not match the account password.";
}
}
$connect->close();
?>
checklogin.js:
function login() {
var enterID = document.getElementById("enterID").value;
var password = document.getElementById("password").value;
if ((password != "") && (enterID != "")) {
var Request = new XMLHttpRequest();
var info = "?enterID=" + enterID + "&password=" + password;
Request.open("GET","php/login.php" + info,true);
Request.send();
Request.onload = function() {
var respond = Request.responseText;
if (respond == "admin") {
window.location.href = "page/admin-system-management.php";
} else if (respond == "student"){
window.location.href = "page/student-dashboard.php";
} else if (respond == "teacher"){
window.location.href = "page/teacher-dashboard.php";
} else{
document.getElementById("errorMessage").innerText = respond;
}
}
} else {
document.getElementById("errorMessage").innerText = "Please fill in all the fields.";
}
}
function redirect() {
var Request = new XMLHttpRequest();
Request.open("GET","php/redirect.php",true);
Request.send();
Request.onload = function() {
var respond = Request.responseText;
if (respond != "not logged.") {
if (respond == "admin") {
window.location.href = "page/admin-system-management.php";
} else if (respond == "student"){
window.location.href = "page/student-dashboard.php";
} else if (respond == "teacher"){
window.location.href = "page/teacher-dashboard.php";
}
}
}
}
重定向.php:
<?php
if (isset($_COOKIE["type"])){
setcookie("type",$_COOKIE["type"],"/");
setcookie("userID",$_COOKIE["userID"],"/");
setcookie("nickName",$_COOKIE["nickName"],"/");
echo $_COOKIE["type"];
} else {
echo "not logged.";
}
?>
TImeoutAndRedirect 函数:
function TimeoutAndRedirect(Type) {
var Request = new XMLHttpRequest();
Request.open("GET","../php/redirect.php",true);
Request.send();
Request.onload = function() {
var respond = Request.responseText;
if (respond == "not logged.") {
alert("Your login period has expired! Please login again!");
window.location.href = "../login.html";
} else if (respond != Type) {
alert("You cannot access this page using your account!");
if (respond == "admin") {
window.location.href = "../page/admin-system-management.php";
} else if (respond == "student"){
window.location.href = "../page/student-dashboard.php";
} else if (respond == "teacher"){
window.location.href = "../page/teacher-dashboard.php";
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。