【问题标题】:How to read a $_SESSION properly on IE-11?如何在 IE-11 上正确读取 $_SESSION?
【发布时间】:2023-03-16 16:05:01
【问题描述】:

我正在处理一项尝试在 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 上单击“是的,我接受”后,如何让弹出窗口仅显示一次,或者换句话说,如何确保在 IE-11 上正确读取会话?我考虑过将 cookie 与 javascript 一起使用,但不知道如何实现...请有人帮助我。非常感谢您的先进!这是我的 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 php jquery jquery-ui internet-explorer


    【解决方案1】:

    认为这不是浏览器问题(我们谈论的是会话,即只读服务器端,在客户端上,您只需使用令牌来识别您的会话)。

    猜测当你返回时服务器代码没有被执行(浏览器缓存了页面并且只会加载 assets 像图像或 javascript)。

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

    使用 localStorage 会是这样(我将使用 localStorage,不使用回退或包装库),只要单击 是我接受 按钮并转到 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") { 更改为if (someJavaScriptVar) {。试一试。

    【讨论】:

    • 您的回答完全是我所需要的。非常感谢!!
    【解决方案2】:

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

    它可能看起来像这样:

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

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

    希望对您有所帮助。

    【讨论】:

    • @Micahel 我希望会话变量始终保持设置,这是我的主要目标。但是,当我使用 IE 时,我无法设置会话变量 :(
    • @xpng 您是否在 IE 中启用了 cookie?
    • 是的,我确实启用了它们
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-19
    • 1970-01-01
    相关资源
    最近更新 更多