Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/performance/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 是否有其他选择和重定向来设置会话?_Php_Jquery_Ajax - Fatal编程技术网

Php 是否有其他选择和重定向来设置会话?

Php 是否有其他选择和重定向来设置会话?,php,jquery,ajax,Php,Jquery,Ajax,这篇文章与我之前发表的一篇文章有关,但现在的问题不同了: 当我在AJAX中选择select字段时,会话被设置,页面被刷新以显示会话中的值。在没有刷新的情况下,我怎么能把它放在这样的手柄上呢 $("#output").something 通过向ajax.php?action=checksessionShipping发送一个ajax请求,每秒检查一次,如果设置了会话,则该ajax文件将返回true;如果未设置会话,则返回false。因此,一旦检测到会话被设置为btw,会话将是另一个堆栈页面上的$

这篇文章与我之前发表的一篇文章有关,但现在的问题不同了:

当我在AJAX中选择select字段时,会话被设置,页面被刷新以显示会话中的值。在没有刷新的情况下,我怎么能把它放在这样的手柄上呢

$("#output").something

通过向ajax.php?action=checksessionShipping发送一个ajax请求,每秒检查一次,如果设置了会话,则该ajax文件将返回true;如果未设置会话,则返回false。因此,一旦检测到会话被设置为btw,会话将是另一个堆栈页面上的$_session['shipping']its。它将值返回到div。

请记住,这是一个快速而肮脏的代码。它不应该按原样用于生产,因为它不是非常安全或可扩展的。但是,这会让你开始:

您可以在HTML中执行以下操作:

<div id="mydiv">

</div><!-- END: #mydiv -->

<script type="text/javascript" charset="utf-8">

    var myInterval = setInterval(function() {

        $.ajax({
          url: 'ajax.php?action=checksessionShipping',
          type: 'POST',
          dataType: 'json',
          data: {},
          complete: function(xhr, textStatus) {
            //called when complete
          },
          success: function(data, textStatus, xhr) {

            if (data.session_set == true)
                $("#mydiv").html(data.shipping);
                clearInterval(myInterval);

          },
          error: function(xhr, textStatus, errorThrown) {
            //called when there is an error
          }
        });

    }, 1000);

</script>
在您的PHP中有如下内容:

<?php

session_start()
$arrReturn = array('session_set' => FALSE);

if (!empty($_SESSION['shipping'])) {

    $arrReturn['session_set'] = TRUE;
    $arrReturn['shipping'] = $_SESSION['shipping'];

}

echo json_encode($arrReturn);
?>

在生产过程中,上述产品有什么问题?除了总是在使用前清理会话文件之外,我只是说,在生产中使用它之前,您应该提供错误报告等。上面的代码并不一定是不安全的,但是如果您不小心,当您开始修改它以使其在您的情况下工作时,它可能会很快变得不安全。