2017-01-01 259 views
-1

我正在尝试创建一个登录系统,根据登录类型,将显示不同的页面。 (即以admin身份登录,或各种其他角色)登录/注销会话的问题

这包括三个文件:

的login.php - 各种形式在这里提交,并根据表格值名称,设置一个会话变量到正确的级别(管理员等)

Logout.php - 取消前面提到的变量。

Dashboard.php - ,看看检查如果变量设置如果是这样,加载培训相​​关的信息,如果没有,发回的index.php

请找到下面的代码:

的login.php

<?php 
session_start(); 
if (isset($_POST['uname_driver'])) 
{ 
    $username = $_POST['uname_driver']; 
    $hpassword = password_hash($_POST['hpass_driver'], PASSWORD_DEFAULT); 
    // Check here for login details within server 
    $_SESSION['loggedIn'] = "driver"; 
    header("Location: dashboard.php"); 
} 
if (isset($_POST['uname_restaurant'])) 
{ 
    $username = $_POST['uname_restaurant']; 
    $hpassword = password_hash($_POST['hpass_restaurant'], PASSWORD_DEFAULT); 
    // Check here for login details within server 
    $_SESSION['loggedIn'] = "restaurant"; 
    header("Location: dashboard.php"); 
} 
if (isset($_POST['uname_admin'])) 
{ 
    $username = $_POST['uname_admin']; 
    $hpassword = password_hash($_POST['hpass_admin'], PASSWORD_DEFAULT); 
    // Check here for login details within server 
    $_SESSION['loggedIn'] = "admin"; 
    header("Location: dashboard.php"); 
} 

Logout.php- 编辑,以反映Juned的回答日在这个问题

<?php 
// Initialize the session. 
// If you are using session_name("something"), don't forget it now! 
session_start(); 

// Unset all of the session variables. 
$_SESSION = array(); 

// If it's desired to kill the session, also delete the session cookie. 
// Note: This will destroy the session, and not just the session data! 
if (ini_get("session.use_cookies")) { 
    $params = session_get_cookie_params(); 
    setcookie(session_name(), '', time() - 42000, 
     $params["path"], $params["domain"], 
     $params["secure"], $params["httponly"] 
    ); 
} 

// Finally, destroy the session. 
session_destroy(); 


header("Location: index.php"); 

Dashboard.php

<?php 
session_start(); 
include("header.php"); 
if (isset($_SESSION['loggedIn'])) 
{ 
    switch ($_SESSION['loggedIn']) 
    { 
     case "admin": 
      include("admin_dashboard.php"); 
      break; 
     case "driver": 
      include("driver_dashboard.php"); 
      break; 
     case "restaurant": 
      include("restaurant_dashboard.php"); 
      break; 
    } 
} 
else 
{ 
    header("Location: index.php"); 
} 
?> 

登录显得做工精绝,直到我试图再次登录的解决了一部分,注销与实现仪表板上的某个按钮和一个Jquery帖子的onclick事件,如下所示:

$('#logoutOfDashboard').click(function(e) 
{ 
    e.preventDefault(); 
    var reallyLogout=confirm("Do you really want to log out?"); 
    if(reallyLogout) 
    { 
     $.post('logout.php', {}) 
     .done(function(data) 
     { 
      window.location.replace("/"); 
     }) 
    } 
}); 

再次,这似乎工作,但是,如果我手动输入/dashboard.php到URL栏,它会踢我回到index.php按预期。 Now无论何时提交登录表单,并且应该重新创建会话变量,dashboard.php会不断返回索引,就好像它不存在一样,并且在此会话期间我无法再登录。这是几乎一样,如果会话变量是没有设置缓存,无法再重新设置

我已经尝试添加各种无缓存的头信息,如:

header("Cache-Control: no-store, no-cache, must-revalidate, max-age=0"); 
header("Cache-Control: post-check=0, pre-check=0", false); 
header("Pragma: no-cache"); 

但是这似乎没有任何效果。任何援助或在这方面的见解将不胜感激。

EDIT

的index.php

<?php 
include("header.php"); 
?> 
<body> 
    <div class="container"> 
     <div class="jumbotron"> 
      <h1>Website Coming Soon!</h1> 
     </div> 

     <div class="row marketing"> 
      <div class="col-lg-6 center-block"> 
       <a class="btn btn-lg btn-success btn-space center-block" href="/restaurant_login.php" role="button">Log In As Restaurant</a> 
      </div> 

      <div class="col-lg-6 center-block"> 
       <a class="btn btn-lg btn-primary btn-space center-block" href="/driver_login.php" role="button">Log In As Driver</a> 
      </div> 
     </div> 


     <footer class="footer"> 
      <p>&copy; 2016</p> 
     </footer> 
    </div> 
</body> 

的header.php

<?php 
date_default_timezone_set('Europe/London'); 
require_once('config.php'); 
require_once('functions.php'); 
function autoloader($class) 
{ 
    require_once(PUBLIC_BASE_PATH_PHP . "classes/$class.php"); 
} 

spl_autoload_register("autoloader"); 

global $dbConn; 
$dbConn = null; 

if(!Database::connect()) 
{ 
    die("Unable to connect to the database"); 
} 
?> 

<head> 
    <!-- Footer these scripts at end --> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script> 

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> 

    <!-- Merge these together and minify at end --> 
    <link rel="stylesheet" href="css/jumbotron-narrow.css"> 
    <link rel="stylesheet" href="css/signin.css"> 
    <link rel="stylesheet" href="css/style.css"> 
</head> 

RestarauntLogin。PHP

<?php Header("Cache-Control: max-age=3000, must-revalidate"); 
include("header.php"); 
?> 

<div class="container"> 
    <form class="form-signin" action="/login.php" method="post"> 

    <h2 class="form-signin-heading">Please sign in</h2> 

    <label for="inputEmail" class="sr-only">Email</label> 
    <input type="email" name = "uname_restaurant" id="inputEmail" class="form-control" placeholder="Email" required autofocus> 

    <label for="inputPassword" class="sr-only">Password</label> 
    <input type="password" name = "hpass_restaurant" id="inputPassword" class="form-control" placeholder="Password" required> 

    <button class="btn btn-lg btn-success btn-block" type="submit">Sign in</button> 
    <a href = "/" class="btn btn-lg btn-primary btn-block" role="button">Back</a> 

    </form> 

</div> 
+0

我相信问题是,会议没有设置页面重新加载后,由于PHP是后端系统,它不是动态的它会导致_SESSION $仍然设置,直到窗口被重新加载,这意味着如果你通过jQuery动态加载窗口,它仍然会说你已经登录 – Imphusius

+0

应该了解会话和会话cookie的工作方式,并且看不出为什么你甚至使用ajax注销,因为无论如何你都要重定向将更好地处理服务器 – charlietfl

+0

我已经尝试删除所有jquery的注销,并只是有一个href将用户发送到logout.php(在logout.php结尾处带有额外的标题(Location:index.php)),具有相同的效果。 – Aphire

回答

3

你需要摧毁的会话,因此而不是仅仅取消设置$ _SESSION [“的loggedIn”],你需要吃会话cookie彻底摧毁了会议。 PHP有一个内置的函数来为你做这件事:session_destroy();

请参见:http://php.net/manual/en/function.session-destroy.php

<?php 
// Initialize the session. 
// If you are using session_name("something"), don't forget it now! 
session_start(); 

// Unset all of the session variables. 
$_SESSION = array(); 

// If it's desired to kill the session, also delete the session cookie. 
// Note: This will destroy the session, and not just the session data! 
if (ini_get("session.use_cookies")) { 
    $params = session_get_cookie_params(); 
    setcookie(session_name(), '', time() - 42000, 
     $params["path"], $params["domain"], 
     $params["secure"], $params["httponly"] 
    ); 
} 

// Finally, destroy the session. 
session_destroy(); 
?> 
+0

我已经尝试过使用这个(将未设置更改为session_destroy)以及尝试这两个,但具有完全相同的结果。 – Aphire

+0

感谢编辑,这似乎解决了其中一个问题! :)现在注销后,如果用户再次尝试访问/dashboard.php,它将正确地将其踢回索引。但是我仍然遇到无法重新登录的问题.. – Aphire

+0

张贴index.php的源代码 – Juned

0

而不是使用window.location.replace()的,请尝试使用window.location.href='/'

+0

同样的问题,我很害怕,谢谢你的尝试! – Aphire

1

按我的理解,这里发生的主要问题是如何保持整个系统的会话。

我写了一个简单的登录系统代码,我相信这有助于理解这一点。在研究Aphire的代码之前,我想提一下在代码中使用Ajax是没有意义的,因为最后会刷新页面,所以如果您直接将用户重定向到logout.php,那将是一件好事。

无论如何,请参阅下面给出的代码,它肯定有助于理解会话在登录系统中的使用。

的login.php

<?php 
$name = $_GET['name']; 
session_start(); 

if(isset($_SESSION['loggedIn'])) 
{ 
    header("Location: dashboard.php"); 
} else { 
    if (isset($name)) 
    { 
      $_SESSION['loggedIn'] = $name; 
      header("Location: dashboard.php"); 
    } else { 
     echo "Please provide correct input"; 
    } 
} 
?> 

logout.php

<?php 
// Initialize the session. 
session_start(); 

// Finally, destroy the session. 
session_destroy(); 


header("Location: login.php"); 
?> 

dashboard.php

<?php 
session_start(); 

if (isset($_SESSION['loggedIn'])) 
{ 
    switch ($_SESSION['loggedIn']) 
    { 
     case "admin": 
      echo "admin"; 
      break; 
     case "driver": 
      echo "driver"; 
      break; 
     case "restaurant": 
      echo "restaurant"; 
      break; 
    default: 
     header("Location: login.php"); 
     session_destroy(); 
    } 
} 
else 
{ 
    header("Location: login.php"); 
} 
?> 

<a href="logout.php">Logout</a> 
+0

嗨苛刻,非常感谢代码片段,我实际上已从注销中删除了AJAX,并从片段中取出了一些代码并将其集成到了我的代码中,但是我遇到了同样的问题:( – Aphire

+0

嗨,Aphire,Can你请分享你已更新的代码,以便我可以查看它吗? – Harsh

+0

嗨哈什,我现在还在开发中,我会尽快编辑我的问题,我可以让我有一些稳定的东西不会去改变一段时间,非常感谢你坚持使用它 – Aphire

4

我试图CREA使用您的代码从我的电脑中查看本地测试页。它似乎工作正常。如果你正在处理会话和cookie,并且你做了很多测试或调试,有时会弄乱浏览器,我可以提出什么建议。尝试清除缓存/ Cookie。我的意思是把它放在注释部分,但我没有足够的声望:)但如果这没有帮助,请告诉我,我会删除它。

+0

嗨Roljhon,这绝对有帮助,很高兴知道我不会生气,这是一个奇怪的问题,而不是我只是愚蠢的。谢谢! – Aphire

+0

很高兴听到这个问题解决了哥们儿。高兴地帮助:)它确实让我发疯,然后才想到真的到底发生了什么:D但无论如何,好运的伙伴! – Roljhon

+0

不幸的是我仍然有这个问题,但是你在本地测试它并且它似乎工作的事实让我感觉好多了:P,也许转移到另一台服务器可能会解决我的问题。 +1从我:) – Aphire

0

你是否试图保留会话ID,但只是删除会话数据?我想没有必要删除会话ID。

我说的是从logout.php删除这段代码:

// If it's desired to kill the session, also delete the session cookie. 
// Note: This will destroy the session, and not just the session data! 
if (ini_get("session.use_cookies")) { 
    $params = session_get_cookie_params(); 
    setcookie(session_name(), '', time() - 42000, 
     $params["path"], $params["domain"], 
     $params["secure"], $params["httponly"] 
    ); 
} 

// Finally, destroy the session. 
session_destroy(); 

所以,logout.php现在看起来像:

<?php 
// Initialize the session. 
// If you are using session_name("something"), don't forget it now! 
session_start(); 

// Unset all of the session variables. 
$_SESSION = array(); 

header("Location: index.php"); 

这应该是足够的 “注销” 的用户。

+0

这是我最初的样子,而且我刚刚测试过它。不幸的是,这样做允许用户在注销后仍然可以访问/dashboard.php,直到他们控制+ F5(重置缓存)。在此之后,无法登录并重新创建会话的问题仍然会像正常情况一样发生:( – Aphire

+0

因此看起来您有缓存问题,并且会话没有问题。 可能在此之后,当您再次登录时它看起来会失败,如果你还按CTRL + F5,你又进入仪表板 – peiiion

+0

另外,你提到你已经尝试在你的header.php文件上发送缓存相关的头文件,但该文件没有被使用login.php和logout.php,所以浏览器可能会缓存这些响应的重定向。我会创建一个cache.php文件(仅用于测试)并在其中放置缓存标头: 'Cache-Control:no-cache ,no-store,must-revalidate' 包含你所有其他php文件中的那个文件。 – peiiion

0

我从你的片段&试过在我的本地与Roljhon

一切都发生创造了一个小的版本似乎是工作的罚款。我认为其他代码或服务器配置可能存在问题。我不能说。

如果它可以帮助,这里是我测试的代码:

loginForm.php

<?php 
Header("Cache-Control: max-age=3000, must-revalidate"); 
//include("header.php"); 
?> 
<div class="container"> 
    <form class="form-signin" action="login.php" method="post"> 
    <input type="email" name = "uname_restaurant" id="inputEmail" class="form-control" placeholder="Email" required autofocus> 
    <input type="password" name = "hpass_restaurant" id="inputPassword" class="form-control" placeholder="Password" required> 
    <button class="btn btn-lg btn-success btn-block" type="submit">Sign in</button> 
    </form> 
</div> 

的login.php

<?php 
session_start(); 
if (isset($_POST['uname_restaurant'])) 
{ 
    $username = $_POST['uname_restaurant']; 
    $hpassword = password_hash($_POST['hpass_restaurant'], PASSWORD_DEFAULT); 
    $_SESSION['loggedIn'] = "restaurant"; 
} 
var_dump($_SESSION); 
die("Location: dashboard.php"); 

仪表板。 php

<?php 
session_start(); 
//include("header.php"); 
if (isset($_SESSION['loggedIn'])) 
{ 
    switch ($_SESSION['loggedIn']) 
    { 
     case "admin": 
      die("admin_dashboard.php"); 
      break; 
     case "driver": 
      die("driver_dashboard.php"); 
      break; 
     case "restaurant": 
      die("restaurant_dashboard.php"); 
      break; 
    } 
} 
else 
{ 
    die("Location: index.php"); 
} 

logout.php

<?php 
// Initialize the session. 
// If you are using session_name("something"), don't forget it now! 
session_start(); 

// Unset all of the session variables. 
$_SESSION = array(); 

// If it's desired to kill the session, also delete the session cookie. 
// Note: This will destroy the session, and not just the session data! 
if (ini_get("session.use_cookies")) { 
    $params = session_get_cookie_params(); 
    setcookie(session_name(), '', time() - 42000, 
     $params["path"], $params["domain"], 
     $params["secure"], $params["httponly"] 
    ); 
} 

// Finally, destroy the session. 
session_destroy(); 

die("Location: index.php");