PHP / AJAX:如何显示在$ _SESSION变量值/隐藏DIV? [英] PHP / Ajax : How to show/hide DIV on $_SESSION variable value?

查看:109
本文介绍了PHP / AJAX:如何显示在$ _SESSION变量值/隐藏DIV?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我寻觅了很多很多的话题在网上讨论关于会话变量,以及如何通过阿贾克斯从Javacript得到他们。不过,虽然我已经能够做到这一点,这并不能完全解决我的问题。

  

目的

要提供网上库存管理网络。

  

约束

  • 只有通过认证的用户可以管理在线库存
  • 在库存管理控制隐藏未认证用户
  • 在每个部分必须独立地告知身份验证,以显示/隐藏自己控制相应
  

code样品

  • authenticate.php
  • project.js
  • 的index.php
  • atv.php
  • ATV-库存list.php的
  • sectionhandler.php

的index.php

 < PHP的session_start(); ?>
< HTML>
...
    < D​​IV ID =newAtvDialog称号=&GT输入的新亚视的信息;
        <形式ID =newAtvAjaxForm行动=addNewAtv.php方法=邮报>
        ...
        < /形式GT;
    < / DIV>
    < D​​IV ID =部分>
        < $的PHP回声的file_get_contents(库存sections.html); ?>
    < / DIV>
...
< / HTML>
 

authenticate.php

 < PHP
require_once数据/ data_access.php;

$ USERNAME =;
$密码=;

如果(使用isset($ _ REQUEST [username的]))$ USERNAME = $ _REQUEST [username的]。
如果(使用isset($ _ REQUEST [密码]))$密码= $ _REQUEST [密码];

$ isAuthentic = isAuthenticUser($用户名,密码$)
$ _SESSION [isAuthentic] = $ isAuthentic;
回声$ isAuthentic;

//我尝试使用下面编写的函数在以往我需要显示/隐藏要素。
传播isCurrentUserAuthenticated(){
    返回使用isset($ _ SESSION [isAuthentic])及和放大器; $ _SESSION [isAuthentic];
}
?>
 

project.js

  $(文件)。就绪(函数(){
    $(#newAtvDialog)。对话框({
        的AutoOpen:假的,
        closeOnEscape:真正的,
        模式:真正的,
        宽度:1000
    });

    $(#newAtvAjaxForm)。当作ajaxForm(功能(数据){
        $(#newAtvDialog)对话框(亲密)。
        $("#section").load("sectionhandler.php?section=atv&type=-1&make=0&year=0&category=0",功能(事件){$(按钮)按钮()。 });
    });
});
 

atv.php

 < D​​IV ID =newAtvButton> <! - 这DIV是被隐藏时,未通过身份验证 - >
    <按钮ID =addNewAtvButton>添加新的ATV< / DIV>
< / DIV>
< D​​IV ID =准则与GT;
    ...
< / DIV>
< D​​IV ID =亚视库存>
    <?PHP的包括:(ATV-库存list.php的'); ?>
< / DIV>
 

ATV-库存list.php的

 < PHP
$型= -1;
$使= 0;
$一年= 0;
$类= 0;

如果(使用isset($ _ REQUEST [型]))$型= $ _REQUEST [型];
...

$全地形车= getAllAtvs($类型,$时许,$年,$类);
的foreach($沙滩车为$值=> $ ATV):
?>
< D​​IV CLASS =存货项目>
    < IMG SRC =<?PHP的回声utf8_de code($ ATV-> getPathToImage())>
    < D​​IV CLASS =项目标题>
    ...
    < / DIV>
    < D​​IV ID =命令>
    <! - 这是我试过到目前为止的方式,它似乎并没有正常工作。 - >
        <按钮ID =removeAtvButton
           类=?< PHP的回声isCurrentUserAuthenticated()'秀'?:?'隐藏';>中>
           删除ATV
        < /按钮>
    < / DIV>
< / DIV>
 

sectionhandler.php

  $节=;
如果(使用isset($ _ REQUEST [一节]))$第= $ _REQUEST [一节]。

$型= -1;
$使= 0;
$一年= 0;
$类= 0;

//从$ _REQUEST获取值[]

$ activatedSection =;

开关($部分){
    案亚视:$ activatedSection =atv.php;
    ...
}

$文件= $ url.raw_url_encore($ activatedSection);
包括$文件;
 

  

补充的想法

我想设置它会从用户约20分钟不活动后过期的布尔会话变量,迫使他再次登录的。

我知道我不会使用存储在数据库中的密码。这是这种身份验证的网站,我就把网上pretty的很快,因为客户端要问交付的任何时间内很快的第一步。加密的密码将是下一个步骤。但首先,我需要的显示/隐藏功能才能正常工作。

我也想过饼干,是很新的Web开发,我是不太知道什么是最好的办法。就我而言,最简单的是最好的,只要它意味着最低的安全性。这不是美国航空航天局的网站后,所有! ; - )

谢谢大家对您的输入! =)

解决方案

这是一个想法,但你可以在/工作从它;

actionURL 的是一个PHP文件,你可以检查用户是否登录了有效的会话。

ajaxSession 的,如果用户登录函数返回true或false。

然后就可以调用这个函数每隔X秒/分钟来控制,如果会话仍在继续。

  window.setInterval(函数(){
  //这里调用你的函数
  如果(ajaxSession(actionUrl)){
      //返回true,用户登录,追加/显示受保护的div。

  }其他{
      //返回false,删除/隐藏受保护的div,并要求用户登录。

  }
},5000); //每5秒。
 

ajaxSession功能:

 函数ajaxSession(actionUrl){
        VAR sessionOK = FALSE;

        $阿贾克斯({
        异步:假的,
        网址:actionUrl,
        成功:函数(MSG){
             //检查从PHP文件返回调用。
              如果(MSG =='OK'){
                 sessionOK =真;
              }其他{
                 sessionOk = FALSE;
              }
        }});

        返回sessionOK;
    }
 

修改

我将添加一个例子code代表的 actionUrl 的,将返回如果会话使用isset还是不给的 ajaxSession 的功能:

 < PHP
    在session_start();

    // $ _SESSION ['章']为真时登录的用户。
    如果($ _ SESSION ['章'] ==真){
        回声'OK';
    }其他{
        回声'NO';
    }
?>
 

记住要检查在ajaxSession功能Ajax调用的结果。如果这是确定的,那么sessionOk = true,如果不是,sessionOk =假的。

I have searched many, many topics on the Net discussing about session variables and how to get them from Javacript through Ajax. However, though I have been able to do so, this doesn't completely solve my problem.

Objective

To provide online inventory management online.

Constraints

  • Only authenticated users can manage the online inventory
  • Inventory management controls are hidden from an unauthenticated user
  • Each sections must be independently informed of the authentication in order to show/hide their controls accordingly

Code Samples

  • authenticate.php
  • project.js
  • index.php
  • atv.php
  • atv-inventory-list.php
  • sectionhandler.php

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;

Supplementary thoughts

I thought of setting a boolean session variable which would expire after about 20 minutes of inactivity from the user, forcing him to log in again.

I know I shan't use passwords stored in the database. This is the first step of the authentication within this site which I shall put online pretty soon, as the client is going to ask for delivery any time soon. Encrypted passwords will be the next step. But first, I need the show/hide feature to work properly.

I have also thought about cookies, and being quite new to web development, I ain't quite sure what would be the best approach. As far as I'm concerned, the simplest the best, as long as it implies a minimum of security. This is not the NASA site after all! ;-)

Thanks everyone for your inputs! =)

解决方案

It's an idea, but you can work on/from it;

actionURL is a php file where you can check if the user is logged in with a valid session.

ajaxSession function returns true or false if the user is logged.

Then you can call this function every X seconds/minutes to control if the session still going.

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;
    }

EDIT

I will add an example code for the actionUrl, that will return if the session is isset or not to the ajaxSession function:

<?php
    session_start();

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

Remember to check in the ajaxSession function the result of the Ajax call. If it's Ok, then sessionOk = true, if not, sessionOk = false.

这篇关于PHP / AJAX:如何显示在$ _SESSION变量值/隐藏DIV?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆