首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何正确读取IE-11上的$_SESSION?

如何正确读取IE-11上的$_SESSION?
EN

Stack Overflow用户
提问于 2016-03-31 14:26:41
回答 2查看 226关注 0票数 4

我正在做一项工作,试图提交一份关于IE的表格:

1.-当我打开page1.php并单击"Submit“,我得到一个对话框jQuery UI弹出,然后单击”是的,我接受“,它需要我到page2.php,如果我点击”后退“按钮,它会带我回到page1.php。

2.-我还有一个名为“inProgress”的php会话,当用户从page1.php转到page2.php时,该会话将获得1的赋值。这基本上意味着,只要用户只在“是我接受”按钮上单击一次,用户就不应该再显示弹出窗口(即使用户来回切换)。

问题:

( a)当用户单击"Submit“按钮(在page1.php上)时,弹出显示并自动单击”是的我接受“,但是即使返回,也不会再次弹出弹出,这是件好事。不过,我更喜欢自己点击“是的,我接受”按钮。

( b)主要的问题是,即使当我使用IE-11时,我不断地得到弹出以显示,即使我来回来回(我只希望弹出只显示一次)。

在IE-11上单击“是我接受”之后,如何才能使弹出只显示一次,或者换句话说,如何确保在IE-11上正确地读取会话?我考虑过使用带javascript的cookie,但不确定如何实现that...Someone,请帮助我。非常感谢你的进步!!下面是我的page1.php和page2.php的代码

page1.php

代码语言:javascript
复制
<?php
session_start();
if ($_POST) {
$_SESSION['inProgress'] = "1";
header("Location: page2.php");
exit;
}
?>
<html>
<head>
<title>Page1</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
</head>
<body>
<form id="homeForm" name="homeForm" method = "POST">
<input type = "submit" id="btnOpenDialog" name = "submit">
<input type="text" id="inProgress" value="<?php echo $_SESSION['inProgress'] ?>"/>
</form>
<h1>This is Page 1</h1>
<div id="dialog-confirm"></div>
<script type="text/javascript">
$(document).ready(function(){
$("#btnOpenDialog").click(function(){
var inProgress = $('#inProgress').val();
if (inProgress !== "1") {
$("#dialog-confirm").dialog({
resizable: false,
modal: true,
title: "Title",
height: 250,
width: 350,
closeOnEscape: false,
buttons: {
  "Yes, I accept": function () {
    $('#homeForm').submit();
    },
    "No, thanks": function () {
      $(this).dialog('close');
    }
  }
  });
  }
 });
});
</script>
</body>
</html>

page2.php

代码语言:javascript
复制
 <html ng-app="store">
 <head>
 <title>Page1</title>
 </head>
 <body>
 <h1>This is Page 2</h1>
 <div id="dialog-confirm"></div>
 <script type="text/javascript">

 function myfunction() {
 window.location.href="page1.php";
 }

 </script>
 <input type="button" id="btnOpenDialog"  onclick = "myfunction()"   value="Back" />

</body>
</html>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2016-04-06 07:03:00

这不是浏览器问题(我们讨论的是会话,即只读服务器端,在客户端,您只有令牌来标识您的会话是什么)。

当您返回时,猜测服务器代码没有被执行(浏览器缓存了页面,只会加载像图像或javascript这样的资产)。

您可以使用localStorage改进这种检查客户端的行为。对于旧的浏览器(如果您需要旧的浏览器支持),有一些库可以写到localStorage,并返回到cookies (如果需要旧的浏览器支持),比如SimpleStorage.js (在github中)。

使用localStorage (我将使用没有后备或包装库的localStorage ),只要单击“是的,我接受”按钮,您就会进入page2存储一个标志

代码语言:javascript
复制
$('#btnOpenDialog').on('click', function(){
   localStorage.setItem('dialogAccepted', 'true'); //you can string just strings, if you ever need to store comples data use JSON.stringify(variable) so it's parsed to json
})

然后在page1中,您执行您已经在做的服务器端的检查(会话内容),然后添加这个客户端。

代码语言:javascript
复制
if(localStorage.getItem('dialogAccepted') === 'true'){
  //code to disable the dialog pop up
} 

要解决“自动”单击:它不是自动单击,只是对话框没有阻止表单的隐藏。你可以这样解决:

代码语言:javascript
复制
$(document).ready(function () {
    var preventedDefaultMet = false;
    $("#btnOpenDialog").on('click', function (event) {
        var inProgress = $('#inProgress').val();
        if (inProgress !== "1") {
            if (!preventedDefaultMet) {//so when the dialog was accepted we can trigger the click event and do not enter an infinite loop of dialogs
                event.preventDefault();//to prevent the browser from making the POST call
            }
            $("#dialog-confirm").dialog({
                resizable: false,
                modal: true,
                title: "Title",
                height: 250,
                width: 350,
                closeOnEscape: false,
                buttons: {
                    "Yes, I accept": function () {
                        preventedDefaultMet = true; // set flag
                        $('#homeForm').trigger('submit');//accepted! just trigger the click! yay!
                    },
                    "No, thanks": function () {
                        $(this).dialog('close');//trigger the click
                    }
                }
            });
        }
    });
});

把我给你的两个答案混在一起,你就应该有你想要的行为。顺便说一句,您不知道是否出于某种原因执行$_SESSION['inProgress'] = "1";,但是您可以编写一些javascript并将该标志存储在浏览器内存中,如下所示:

代码语言:javascript
复制
<form id="homeForm" name="homeForm" method = "POST">
        <input type = "submit" id="btnOpenDialog" name = "submit">
        <input type="text" id="inProgress" value="<?php echo $_SESSION['inProgress'] ?>"/>
    </form>
<script>
   // when the browser reads this it will store the value PHP printed into the javascript variable
   var someJavaScriptVar = <? echo $_POST ? true : false ;?>;
</script>

这样,您就不必这样做了,$_SESSION['inProgress'] = "1";和输入将具有用户输入的值。您必须将if (inProgress !== "1") {更改为if (someJavaScriptVar) {。试一试。

票数 2
EN

Stack Overflow用户

发布于 2016-04-02 15:02:11

在第1页上,您的函数在加载时自动运行,而不是等待用户操作。您可以做的是将<input type = "submit" id="btnOpenDialog" name = "submit">更改为一个普通按钮,然后该按钮触发您的函数。

看起来可能是这样的:

代码语言:javascript
复制
<?php
session_start();
if ($_POST) {
$_SESSION['inProgress'] = "1";
header("Location: page2.php");
exit;
}
?>
<html>
<head>
<title>Page1</title>
<link rel="stylesheet" href="http://code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css">
<script src="http://code.jquery.com/jquery-1.10.2.js"></script>
<script src="http://code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
</head>
<body>
<form id="homeForm" name="homeForm" method = "POST">
<input type = "button" id="btnOpenDialog" name = "submit" onclick="yourfunctionABC()">
<input type="text" id="inProgress" value="<?php echo $_SESSION['inProgress'] ?>"/>
</form>
<h1>This is Page 1</h1>
<div id="dialog-confirm"></div>
<script type="text/javascript">

function yourfunctionABC(){
 xxx}

您是否也检查会话是否已经设置?可能看起来像:

代码语言:javascript
复制
if(session_id() == '' || !isset($_SESSION)) {
    session_set_cookie_params(3600, '/', '.example.com');  
    // session isn't started
    session_start();//create unique session for user
}

你真的在第二页上重置会话变量吗?如果没有,会话变量将始终保持设置状态,这就是为什么不会显示其他弹出内容。

希望这能有所帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36336240

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档