页面重新加载后丢失会话变量

时间:2019-04-05 13:02:35

标签: php apache vagrant

登录后,我用登录的用户名存储会话变量,然后重定向到/dashboard/index.php。稍后,我在应用程序中单击菜单链接,该菜单链接重定向到/dashboard/kanboard/index.php,而$_SESSION['user']仍在此处,但是如果我在浏览器(或kanboard菜单链接)中按F5键,则该方法将检查{{1} } exist返回false并重定向到登录页面。这种奇怪的行为不会在其他页面上发生,例如$_SESSION['user'],在这里我可以重新加载页面而不会丢失会话变量。显然这些文件非常相似。

dashboard / index.php

/dashboard/index.php

dashboard / kanboard / index.php

<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$titulo = "Index";
include $path.'/dashboard/includes/userData.php';
?> 
<!DOCTYPE html>
<html lang="es">
  <head>
   <?php
    include $path.'/dashboard/includes/header.php';
    ?>
  </head>

  <body class="nav-md">
    <div class="container body">
      <div class="main_container">
            <!-- sidebar menu -->
            <?php
            include $path.'/dashboard/includes/menu.php';
            include $path.'/dashboard/includes/top-bar.php';
            ?>
        <!-- page content -->
        <div class="right_col" role="main">

        </div>
        <!-- /page content -->

        <?php
            include $path.'/dashboard/includes/footer.php';
        ?>  
    <script src="./js/index.js"></script>

    <script>
      $(document).ready(function() {
        var index = new Index(JSON.parse('<?php echo getJSONUser()?>'));
      });
    </script>

  </body>
</html>

检查登录用户的方法

<?php
$path = $_SERVER['DOCUMENT_ROOT'];
$migas = "../";
$titulo = "Resumen Imputaciones";
include $path . '/dashboard/includes/userData.php';
?>
<!DOCTYPE html>
<html lang="es">
<head>
    <?php
    include $path . '/dashboard/includes/header.php';
    ?>
    <!-- MONGO Chart-->
    <script src="/dashboard/vendors/Chart.js/dist/Chart.min.js"></script>

</head>

<body class="nav-md">

<!-- MODAL INFORMACION DETALLADA -->
<div class="modal" tabindex="-1" role="dialog" id="modalinfo">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <h3 class="modal-title" id="mtitulo"></h3>
                <button type="button" class="close" data-dismiss="modal" aria-label="Close" style="float: right;">
                    <span aria-hidden="true">&times;</span>
                </button>
            </div>
            <div class="modal-body" id="mbody">

            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-secondary" data-dismiss="modal">Cerrar</button>
            </div>
        </div>
    </div>
</div>
<!-- FIN MODAL INFORMACION DETALLADA -->

<div class="container body">
    <div class="main_container">
        <!-- sidebar menu -->
        <?php
        include $path . '/dashboard/includes/menu.php';
        include $path . '/dashboard/includes/top-bar.php';
        ?>
        <!-- page content -->
        <div class="right_col" role="main">
            <div class="">
                <div class="clearfix"></div>
                <div class="row">
                    <div class="col-sm-12">
                        <div class="x_panel form-panel">
                            <div class="x_title">
                                <h2>Resumen Imputaciones</h2>
                                <div class="clearfix"></div>
                            </div>
                            <div class="x_content">
                                <?php
                                if (isAdministrator()) {
                                    ?>
                                    <div id="tableImputaciones-loader" class="div-loader">
                                        <i class="fa fa-spinner fa-pulse spinner"></i>
                                    </div>
                                    <div class="row" id="imputaciones">
                                        <div id="tablaImputacionesContainer"
                                             class="col-lg-12 col-md-12 col-sm-12 col-xs-12">
                                            <table id="tableImputaciones" class="table table-striped table-bordered"
                                                   style="width:100%">
                                                <thead id="tableImputacionesHead">
                                                </thead>
                                                <tbody id="tableImputacionesBody">
                                                </tbody>
                                            </table>
                                        </div>
                                        <div class="col-lg-1 col-md-1 col-sm-1 col-xs-1 more-results">
                                            <button type="button" title="Mostrar más"
                                                    class="btn btn-lg button-more-results" data-total-paginas="1"
                                                    data-tabla-ref="tableImputaciones"><i class="fa fa-angle-right"
                                                                                          aria-hidden="true"></i>
                                            </button>
                                        </div>
                                        <input type="hidden" id="last-index"/>
                                    </div>
                                    <?php
                                }
                                ?>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="clearfix"></div>
            </div>
        </div>
        <?php
        include $path . '/dashboard/includes/modals/showUrl.php';
        include $path . '/dashboard/includes/footer.php';
        ?>
        <!-- ECharts -->
        <script type="text/javascript" src="/dashboard/vendors/echarts/dist/echarts.js"></script>
        <script type="text/javascript" src="/dashboard/kanboard/js/utils.js"></script>
        <script type="text/javascript" src="/dashboard/kanboard/js/index.js"></script>
        <script>
            $(document).ready(function () {
                var index = new Index('<?php echo getUserName()?>');
            });
        </script>
</body>
</html>

编辑:在我将整个项目移至仪表板文件夹之前,这两个页面都工作正常。在它们直接出现在www / html上之前,并包括相对路径,而不是绝对路径。

编辑2 :我所做的另一个更改是在Centos 7中使用PHP 7.3.3创建Vagrant机器,并将整个项目移入其中。在此之前,整个项目可以在另一台装有Debian,相同版本的Apache和PHP 7.3.1的服务器上正常运行。

1 个答案:

答案 0 :(得分:-2)

首先,您的userIsAuth()函数名称错误,因为其中仅包含非常简单的身份验证。

其次,您需要先开始会话,然后再进行其他操作,例如:

if(session_id() === '') {
    session_start();
}

在每个页面的HTML之前添加该代码,并按如下方式更改userIsAuth()函数:

function userIsAuth()
{
    return (isset($_SESSION['user']) && !empty($_SESSION['user']));
}

您的userIsAuth()函数现在将像以前一样返回一个布尔值,但是PHP脚本的行要少得多。