我正在尝试使用php和dhtmlxform构建登录。我的表单信息成功提交到我的login.php脚本以检查凭据。如果它们是正确的,则创建用户会话,并且脚本将响应发送回dhtmlxform中的javascript。如果会话已创建,我将$ session设置为true并将响应返回给javascript。如果响应等于1,则我将用户重定向到登录页面。如果响应不是1,我会提醒响应(包含失败错误)。我需要做的是将login.php脚本中的实际$ _SESSION信息与javascript中的重定向结合起来。我该怎么做呢?
感谢所有帮助!谢谢!
我的代码:
HTML /使用Javascript:
<body>
<div id="headerDiv">
<br/>
<br/>
<h1>Welcome to the Scheduler</h1>
<br/>
<br/>
</div>
<div id="bodyDiv">
<div id="login"></div>
<br/>
<br/>
</div>
<script>
loginStructure = [
{type:"settings", position:"label-top"},
{type:"fieldset", name: "loginFieldset", label: "Login", width: 200, list: [
{type: "input", name: "username", label: "Username: "},
{type:"password", name: "password", label: "Password: "},
{type:"button", name: "submit", width: 50, offsetTop: 20, offsetLeft: 55, value: "Submit"}
]}
];
var myForm = new dhtmlXForm("login",loginStructure);
myForm.attachEvent("onButtonClick",function(id){
if(id=="submit"){
myForm.send("login.php", "post", function(loader, response){
if (response == 1) {
window.location.href="https://ltiws.com/scheduler/production_sched.php";
} else {
alert(response);
};
});
}
});
</script>
</body>
我的PHP脚本:
<?php
include("inc.php");
if ((isset($_POST['username'])) and (isset($_POST['password']))) {
$login = $_POST['username'];
$pass = $_POST['password'];
} else {
echo "Credentials did not save. Please try again.";
}
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_errno) {
echo "There was a problem connecting to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error;
}
$sql = "SELECT * FROM sched_users WHERE login = ? ORDER BY login LIMIT 1";
if (!($sql = $conn->prepare("SELECT * FROM sched_users WHERE login = ? ORDER BY login LIMIT 1"))) {
echo "Prepare failed: (" . $conn->errno . ") " . $conn->error;
}
if (!($sql->bind_param("s", $login))) {
echo "Binding parameters failed: (" . $sql->errno . ") " . $sql->error;
}
if (!$sql->execute()) {
echo "Execute failed: (" . $sql->errno . ") " . $sql->error;
}
$res = $sql->get_result();
if ($res->num_rows === 0) {
echo "No user record found.";
} else if (!(($res->num_rows === 1) or ($res->num_rows === 0))) {
echo "Too many results returned.";
} else {
$row = $res->fetch_array();
$hash = $row['password'];
if (password_verify($pass, $hash)) {
session_start();
$_SESSION['id'] = $row['id'];
$_SESSION['firstname'] = $row['firstname'];
$_SESSION['lastname'] = $row['lastname'];
$_SESSION['login'] = $row['login'];
$_SESSION['email'] = $row['email'];
$_SESSION['phone'] = $row['phone'];
$_SESSION['company'] = $row['company'];
$_SESSION['department'] = $row['department'];
$_SESSION['admin'] = $row['admin'];
$_SESSION['statusflag'] = $row['statusflag'];
$_SESSION['revoked'] = $row['revoked'];
if (isset($_SESSION['id'])) {
$session = true;
echo $session;
} else {
$session = false;
echo $session;
}
} else {
echo "The username or password does not match. Please try again.";
}
}
$sql->close();
$conn->close();
?>