我在网上搜索了很多主题,这些主题讨论了会话变量以及如何通过Ajax从Javacript中获取它们。但是,尽管我已经能够做到这一点,但这并不能完全解决我的问题。
目的
在线提供在线库存管理。
约束条件
代码样本
index.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>
authenticate.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"]; } ?>
project.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(); }); }); });
atv.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>
atv-inventory-list.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>
sectionhandler.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开发来说还很陌生,所以我不确定哪种方法最好。就我而言,最简单的是最好的,只要它意味着最低的安全性即可。毕竟这不是NASA网站!;-)
感谢大家的投入!=)
这是一个主意,但您可以继续/从中进行;
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 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调用的结果。如果可以,则sessionOk = true,否则,sessionOk = false。