doukanhua0752 2018-02-07 20:27
浏览 77

使用CSRF令牌PHP / Javascript将登录页面重定向到主应用程序页面

I have a login page (login.php) that redirects to my main page (dashboard.php). When the user POSTs their email and password to login.php I validate credentials, create a CSRF token, store it in the session, and then redirect to dashboard.php. Upon redirection, however, dashboard.php is doing it's own validation of the session and CSRF token. Since it's a redirect in php, I cannot attach any custom headers to contain the CSRF and dashboard.php is redirecting back to login.php. I thought I may be able to handle this with JavaScript but haven't found any examples of redirecting to another page in JavaScript and including the CSRF token in a header. I know I could redirect with a GET variable using the CSRF token but that seems insecure.

Login.php

<?php 
$user = new AuthenticatedUser($dbh);
$loggedIn = $user->isLoggedIn();
if ($user->isLoggedIn())
{
    header("Location: dashboard.php");
    die("Redirecting to dashboard...");
}

if (isset($_POST["email"]) && isset($_POST["password"]))
{
    $user->login($_POST["email"], $_POST["password"]);

    if ($user->isLoggedIn())
    {
        header("Location: dashboard.php");
    }
}

dashboard.php

<?php
$dbh = new Database();
$authUser = new AuthenticatedUser($dbh);

// When constructing the AuthenticatedUser class object in dashboard.php the
// constructor function checks to see if the user has a session and if so then
// checks for the CSRF is set. If it's not the class flips the flag for
// isLoggedIn which in turn tells dashboard.php to redirect back to login.php
if (!$authUser->isLoggedIn())
{
    header("Location: login.php");
    die("Redirecting to login...");
}
?>
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何在scanpy上做差异基因和通路富集?
    • ¥20 关于#硬件工程#的问题,请各位专家解答!
    • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
    • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
    • ¥30 截图中的mathematics程序转换成matlab
    • ¥15 动力学代码报错,维度不匹配
    • ¥15 Power query添加列问题
    • ¥50 Kubernetes&Fission&Eleasticsearch
    • ¥15 報錯:Person is not mapped,如何解決?
    • ¥15 c++头文件不能识别CDialog