我已经创建了一个登录脚本并且它工作正常,但是,我想实现会话..我目前遇到一些麻烦,因为我的会话脚本只是部分执行。下面是我的登录脚本和我希望它重定向到的测试页面,如果用户已登录..我希望它显示测试页面,如果没有,那么我希望它重定向回登录页面(或者在这种情况下,index.php文件)并要求用户登录...请参阅下面的代码:
loginconfig.php:
var data_array = '';
$(document).on('ready', function(event, data) {
setInterval(function () {
$.ajax({
url:"./phponline.php",
async:false,
success:function(res)
{
data_array = res;
updateWithData();
},
error:function(errorMsg)
{
}
});
}, 5000);
updateWithData();
});
function updateWithData(){
//use data_array to make changes on each call.
}
test.php的:
<?php
// Create a connection
include("dbconfig.php");
if (isset($_POST['submit'])) {
if (empty($_POST['username']) or empty($_POST['password'])) {
header("location:index.php?msg0=Please complete the required fields.");
}
elseif (!empty($_POST['username']) && !empty($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];
$sql = mysqli_query($conn, "SELECT * FROM logininformation WHERE username = '$username' and password = '$password'") or die(mysqli_error($conn));
$login = ($sql) ? mysqli_fetch_assoc($sql) : false;
if (($login) == 0) {
header("location:index.php?msg1=Invalid username or password, please try again.");
}
elseif (($login) > 0) {
session_start();
$_SESSION['login'] = $_POST['username'];
//header("location:index.php?bid=$username&msg2=You are unable to log in at this time. Website is under construction.");
header("location:test.php?bid=$sessionwork");
}
}
}
?>
logout.php
<?php
session_start();
include("dbconfig.php");
$username = $_GET['bid'];
var_dump($_SESSION['login'];
// If user is logged in:
if(!empty($_SESSION['login'])){
echo "Welcome $username";
}
// If user is not logged in:
elseif(!isset($_SESSION['login'])){
header("location:index.php?msg4=You need to be logged in!");
}
?>
<html>
<head>
<title> user page </title>
</head>
<body>
<form name="logout" method="post" action="logout.php">
<input type="submit" name="logout" value="logout">
</form>
</body>
</html>
现在,如果您查看test.php文件..我有点告诉它检查用户是否已登录。但不幸的是,脚本只设法执行脚本,如果用户未登录in ..重定向到index.php ...即使用户输入正确的登录凭据并实际登录。可能是什么问题?
非常感谢任何帮助。
答案 0 :(得分:0)
在test.php中应该是这样的:
if(isset($_SESSION['login'])){
echo "Welcome $_SESSION['login']";
}
else{
header("location:index.php?msg4=You need to be logged in!");
}
在loginconfig.php中重复相同的错误。
答案 1 :(得分:0)
最初,我没有logout.php文件..因此我犯了不破坏我的会话的错误。我必须对我的初始脚本进行的更改是创建一个logout.php文件。但是当我这样做时,问题仍然存在..为了使它工作..我对logout.php文件做了以下更改..见下文:
<强> BEFORE:强>
<?php
session_start();
if(!empty($_SESSION['login'])){
session_destroy();
?>
<html>
<a href="index.php"> Homepage </a>
</html>
<强> AFTER:强>
<?php
session_start();
session_destroy();
header("location:index.php");
exit();
?>
感谢那些帮助过的人,尤其是@Epodax的GREAT支持。