0
早上好/晚上,PHP登录和会话
我被卡住了,我需要一些PHP帮助。
我想编码管理仪表板。我想检查用户是否已登录,如果没有,则重定向到登录页面。
我的index.php是这样的:
<?php
$pagename ="Index";
@require_once('inc/head.php');
?>
<body>
CONGRATS! Welcome to the Admin dashboard.
</body>
</html>
我的登录页面:
<?php
$pagename = "login";
$adminUser = "admin";
$adminPass = "admin";
@require_once('inc/head.php');
// If POST is submitted and IDs match the ones set
if($_SERVER["REQUEST_METHOD"] == "POST")
{
if($_POST["username"] == $adminUser && $_POST["password"] == $adminPass)
{
session_start();
$_SESSION["username"] = $adminUser;
$_SESSION["login"] = true;
echo '<script>alert("Congrats, you logged in");
window.location = "index.php"; </script>';
/* I skip the line underneath because for unknown reasons my code
Doesn't fully run through. So I redirected with the JS above instead.
header("Location: index.php");
exit(); */
}else{
echo '<script>alert("Incorrect username or password!'");</script>';
}
}
?>
<html>
<!-- login page here -->
</html>
而且在这里不用我head.php:
<?php
// If we AREN'T on the login page , check if session exist. If not send to login
if($pagename != "login")
{ if(!$_SESSION['login'])
{
header('location: login.php');
exit();
}
}
?>
有很多的东西错了与此,我知道,但截至目前我试图解决我的登录问题。每当我登录时,我都会弹出JS说我成功登录了,但我没有被重定向到索引。我想我得到发送到我的index.php(没有理由我的JS重定向到NOT功能),但我的索引发送我回到登录,我不明白为什么。
在会话中的所有页面开始使用他们?似乎不是head.php文件的情况。 –
'session_start();'在** head.php中缺少** –
但是我只想在用户登录后创建一个会话。或者我需要创建一个,无论如何,当用户登录时为其添加值在? – FrenchMajesty