繁体   English   中英

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

[英]How to read a $_SESSION properly on IE-11?

我正在尝试在IE上提交表单的任务:

1.-当我在page1.php上并点击“提交”时,我得到一个对话框jQuery UI弹出窗口然后我点击“是的我接受”它将我带到page2.php,如果我点击“返回” “按钮它将我带回page1.php

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

问题:

a)不知何故,当用户点击“提交”按钮(在page1.php上)时,弹出窗口显示并自动点击“是的我接受”,但即使我回去了,我也不会再次弹出是一件好事。 但是,我更愿意单击“是的我接受”按钮。

b)主要的问题是,即使我在使用IE-11时来回走动,我仍然会弹出显示(我只希望弹出窗口只显示一次)。

如何在IE-11上单击“是的我接受”后弹出显示ON ON ONCE,或者换句话说如何确保在IE-11上正确读取会话? 我考虑过使用带有javascript的cookie但不确定如何实现...有人帮助我。 先谢谢你! 这是我的page1.php和page2.php的代码

page1.php中

<?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

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

认为它不是浏览器问题(我们谈论的是会话,只读服务器端,在客户端上你只需要令牌来识别你的会话是什么)。

想,当你没有被执行回服务器代码(该浏览器有网页缓存,只会加载资产如图片或JavaScript)。

您可以使用localStorage改进执行此类检查客户端的行为。 有一些库可以写入localStorage并且可以 localStorage 浏览器的cookies (如果你需要旧的浏览器支持),比如SimpleStorage.js (在github中)。

使用localStorage会是这样的(我会使用localStorage ,没有回退或换行库),只要点击Yes I accept按钮,你就转到page2存储一个标志

$('#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 ,你检查你已经在服务器端(会话东西),你添加这个客户端

if(localStorage.getItem('dialogAccepted') === 'true'){
  //code to disable the dialog pop up
} 

要解决“自动”点击:它不是自动点击,只是对话框没有阻止表单子命令 你可以这样解决:

$(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并将这个标志存储在浏览器内存中,如下所示:

<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") { for if (someJavaScriptVar) { 尝试一下。

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

它可能看起来像这样:

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

您还在检查会话是否已设置? 看起来像:

if(session_id() == '' || !isset($_SESSION)) {
    session_set_cookie_params(3600, '/', '.example.com');  
    // session isn't started
    session_start();//create unique session for user
}

你真的在第二页重置了会话变量吗? 如果不是,会话变量始终保持设置,这就是为什么不会显示其他弹出窗口的原因。

希望有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM