所以我班上的一个项目是创建一个包含数据库的网站。我无法使登录功能正常工作。我在php中编写了登录和登录脚本。我登录时发生的所有事情都是页面刷新到“localhost / login.php”?而不是“index.php”任何帮助表示赞赏,提前谢谢。
我的login.php
<?php
include 'nav.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<title>UCF Events</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="css/login.css">
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="css/navstuff.css">
<?php
include 'setUniversityColors.php';
setColors();
?>
<script>
function call() {
//we will POST to this php file on the server
//it will process what we send and can return back JSON information
var request = $.post("login-script.php",
{
//these are defined in the inputs within our form
//each input is defined by their id attribute in the HTML
email: $("#inputEmail").val(),
password: $("#inputPassword").val()
}
//this function is called when we get a response back from the server
function(json){
//write back what we get in the "message" field to the response div defined in this HTML
//the response div is located right below the "Register" button
$("div.response").html(json.message);
//on success redirect to the index page
if(json.success === "success")
self.location="index.php";
}
//defines that we are expecting JSON back from the server
"json");
}
</script>
</head>
<body>
<?php
displayNav('login.php' ,'Login');
?>
<div class="container">
<form class="form-signin">
<label for="inputEmail" class="sr-only">Email address</label>
<input type="email" id="inputEmail" class="form-control" placeholder="Email address" required autofocus>
<label for="inputPassword" class="sr-only">Password</label>
<input type="password" id="inputPassword" class="form-control" placeholder="Password" required>
<button class="btn btn-lg btn-primary btn-block" onClick="call();">Sign in</button>
</form>
</div>
</body>
</html>
我的login-script.php
<?php
//sets the following variables:
$mysqluser = 'root';
$mysqlpassword = '';
$mysqldbname = 'db';
//now connect to the database
$mysqli = new mysqli("localhost", $mysqluser, $mysqlpassword, $mysqldbname);
//here we can extract information from the client's POST request
// this was submitted by the jQuery function
//always use mysql_real_escape_string when taking in user input
// this prevents SQL injection
$email = $mysqli->real_escape_string($_POST['UserEmail']);
$password = $mysqli->real_escape_string($_POST['Password']);
$success = " ";
$message = " ";
$sql = "SELECT Password, isAdmin, isSuperAdmin, UserID FROM user WHERE UserEmail='$email'";
//run the query and check if it was successful
$result = $mysqli->query( $sql );
if($result){
//get an associative array from the result
//retrieve specific attribute values by $row['tableAttribute']
$row = $result->fetch_assoc();
//check entered password against the hash
if (password_verify($password, $row['Password'])) {
$message = "Login Successful, redirecting...";
$success = "success";
//start the session and set some identifying variables
//these are save across pages
//we will end the session when the user logs out
session_start();
$_SESSION['UserEmail'] = $email;
$_SESSION['isAdmin'] = $row['isAdmin'];
$_SESSION['isSuperAdmin'] = $row['isSuperAdmin'];
$_SESSION['id'] = $row['UserID'];
} else {
$message = "There was a problem with your user name or password.";
$success = "fail";
}
} else {
$message = "Error accessing database: " . $mysqli->error;
$success = "fail";
}
$return = array('message' => $message, 'success' => $success);
echo json_encode($return);
?>
答案 0 :(得分:0)
你的代码非常奇怪。 我看到你使用jquery,这是一个例子。
$.ajax({
url: "your.php",
method: "POST",
data: { email : $('#email').val(), password: $('#pass').val() }
});
现在,在你的php中
$_POST['email'];
$_POST['password'];
现在使用JSON。
//Prepare your JSON
var myJson = {};
myJson['email'] = $('#email').val();
myJson['password'] = $('#pass').val();
$.ajax({
url: "your.php",
method: "POST",
data: { email : $('#email').val(), password: $('#pass').val() },
dataType: "json"
});
在你的PHP中
echo "The array ".print_r($_POST['paramet'],1);
$data = json_decode($_POST['paramet']);
echo $data['email'];
echo $data['password'];