PHP / Ajax : 如何在 $_SESSION 变量值上显示/隐藏 DIV


PHP / Ajax : How to show/hide DIV on $_SESSION variable value?

我在网上搜索了很多很多关于会话变量以及如何通过Ajax从Javacript获取它们的主题。然而,虽然我已经能够这样做,但这并不能完全解决我的问题。

目的

提供在线库存管理。

约束

  • 只有经过身份验证的用户才能管理在线库存
  • 库存管理控件对未经身份验证的用户隐藏
  • 必须独立地通知每个部分身份验证,以便相应地显示/隐藏其控件

代码示例

  • 进行身份验证.php
  • 项目.js
  • 索引.php
  • 亚视.php
  • 亚视库存列表.php
  • 部分处理程序.php

索引.php

<?php session_start(); ?>
<html>
...
    <div id="newAtvDialog" title="Input information on the new ATV">
        <form id="newAtvAjaxForm" action="addNewAtv.php" method="post">
        ...
        </form>
    </div>
    <div id="section">
        <$php echo file_get_contents("inventory-sections.html"); ?>
    </div>
...
</html>

进行身份验证.php

<?php
require_once "data/data_access.php";
$userName = "";
$password = "";
if (isset($_REQUEST["userName"])) $userName = $_REQUEST["userName"];
if (isset($_REQUEST["password"])) $password = $_REQUEST["password"];
$isAuthentic = isAuthenticUser($userName, $password);
$_SESSION["isAuthentic"] = $isAuthentic;
echo $isAuthentic;
// I try to use the below-written function where ever I need to show/hide elements.
function isCurrentUserAuthenticated() { 
    return isset($_SESSION["isAuthentic"]) && $_SESSION["isAuthentic"]; 
}
?>

项目.js

$(document).ready(function() {
    $("#newAtvDialog").dialog({
        autoOpen: false,
        closeOnEscape: true,
        modal: true,
        width: 1000
    });
    $("#newAtvAjaxForm").ajaxForm(function(data) {
        $("#newAtvDialog").dialog("close");
        $("#section").load("sectionhandler.php?section=atv&type=-1&make=0&year=0&category=0", function(event) { $("button").button(); }); 
    });
});

亚视.php

<div id="newAtvButton"> <!-- This DIV is to be hidden when not authenticated -->
    <button id="addNewAtvButton">Add New ATV</div>
</div>
<div id="criterion">
    ...
</div>
<div id="atv-inventory">
    <?php include ('atv-inventory-list.php'); ?>
</div>

亚视库存列表.php

<?php 
$type = -1;
$make = 0;
$year = 0;
$category = 0;
if (isset($_REQUEST["type"])) $type = $_REQUEST["type"];
...
$atvs = getAllAtvs($type, $make, $year, $category);
foreach ($atvs as $value=>$atv):
?>
<div class="inventory-item">
    <img src="<?php echo utf8_decode($atv->getPathToImage())">
    <div class="item-title">
    ...
    </div>
    <div id="commands">
    <!-- This is the way I have tried so far, and it doesn't seem to work properly. -->
        <button id="removeAtvButton" 
           class="<?php echo isCurrentUserAuthenticated() ? 'show' : 'hide'; ?>">
           Remove ATV
        </button> 
    </div>
</div>

部分处理程序.php

$section = "";
if (isset($_REQUEST["section"])) $section = $_REQUEST["section"];
$type = -1;
$make = 0;
$year = 0;
$category = 0;
// getting values from $_REQUEST[]
$activatedSection = "";
switch($section) {
    case "atv": $activatedSection = "atv.php";
    ...
}
$file = $url.raw_url_encore($activatedSection);
include $file;

补充意见

我想设置一个布尔会话变量,该变量将在用户不活动大约 20 分钟后过期,迫使他再次登录。

我知道我不使用存储在数据库中的密码。这是本网站内身份验证的第一步,我将很快上线,因为客户将很快要求交付。加密密码将是下一步。但首先,我需要显示/隐藏功能才能正常工作。

我也考虑过cookie,并且对Web开发很陌生,我不太确定什么是最好的方法。就我而言,最简单的就是最好的,只要它意味着最低的安全性。这毕竟不是美国宇航局的网站!;-)

感谢大家的投入! =(

这是一个想法,但你可以从中工作;

actionURL 是一个 php 文件,您可以在其中检查用户是否使用有效会话登录。

ajaxSession 函数返回 true 或 false 如果记录了用户。

然后,您可以每 X 秒/分钟调用一次此函数来控制会话是否仍在进行。

window.setInterval(function(){
  // call your function here
  if(ajaxSession(actionUrl)){
      //return true, user logged, append/show protected divs.
  }else{
      //return false, remove/hide protected divs and ask user to log.
  }    
}, 5000); //every 5 seconds.

ajaxSession function:

 function ajaxSession(actionUrl) {
        var sessionOK= false;
        $.ajax({
        async: false,
        url: actionUrl,
        success: function(msg) {
             // check the return call from the php file.
              if(msg== 'OK'){ 
                 sessionOK = true;
              }else{
                 sessionOk = false;
              }
        }});
        return sessionOK;
    }

编辑

我将为 actionUrl 添加一个示例代码,如果会话是否设置为 ajaxSession 函数,该代码将返回:

<?php
    session_start();
    // $_SESSION['reg'] is true when the user is logged in.
    if($_SESSION['reg'] == true){
        echo 'OK';
    }else{
        echo 'NO';
    }
?>

请记住在 ajaxSession 函数中检查 Ajax 调用的结果。如果为 Ok,则 sessionOk = true,如果不是,sessionOk = false。