【问题标题】:Losing $_SESSION variables on AJAX POST... was working before在 AJAX POST 上丢失 $_SESSION 变量...之前工作过
【发布时间】:2017-07-24 04:38:24
【问题描述】:

在说别的之前,是的,我确实在每个相关页面的顶部都有session_start();。这不是问题。

我正在使用 Google Analytics API,并且有一个可以查询分析数据的页面。我正在使用多个下拉菜单来允许用户选择他们想要查询的帐户。

为了填充这些下拉菜单,我使用 AJAX 调用 API 调用来填充每个下拉菜单,具体取决于前一个下拉菜单的值是什么(想想 country->state->city)。

无论如何,在我不得不更新 API 库之前,一切都运行良好。现在,我在发出 AJAX POST 请求的脚本和处理 AJAX 的脚本之间丢失了 $_SESSION 变量。

这是我的代码:

index.php(三个下拉菜单,注意它们是如何实例化的)

    if ($client->getAccessToken()) { //if the client has a valid access token
    try {
        //lists the client's available analytic accounts
        $accounts = $analytics->management_accounts->listManagementAccounts();
        $accsAvailable = $accounts->getItems(); ?>

        <p>Available Accounts:</p> <?php
            if (!isset($_REQUEST['compare'])) { ?>
                <form action="" name="formSubmit" onsubmit="return validateForm()" method="post"> <!--START FORM--> <?php
                } else { ?>
                <form action="" name="formSubmit" onsubmit="return validateComparison()" method="post"> <!--START FORM--> <?php
                } ?>
                <select name="dropAccounts" class="dropAccounts" id="dropAccounts"> <!--drop-down for avaiable accounts--> <?php
                //if there is at least one account available
                if (count($accsAvailable) > 0) { //create a drop down of available accounts ?>
                    <option value="0">---Select an account---</option> <?php //default option
                    foreach ($accsAvailable as $account) {
                        //populate from API
                        echo '<option value=' . $account->getId(). '>' . $account->getName() . '</option>';
                    }
                } else { ?>
                    <option value="0">---No accounts available---</option> <?php //else no accounts exist
                } ?>
            </select> <!--END drop-down for avaiable accounts-->
        </div>
        <!-- /.col-md-4 -->

        <div class="col-md-4">
            <p>Available Webproperties:</p> <!--drop-down for available webproperties-->
            <select name="dropProperties" class="dropProperties" id="dropProperties">
                <option selected="selected" value="0">---Select a webproperty---</option> <!--default option-->
            </select>  <!--END drop-down for available webproperties-->
        </div>

        <div class="col-md-4">
            <p>Available Profiles: </p> <!--drop-down for available profiles-->
            <select name="dropProfiles" class="dropProfiles" id="dropProfiles">
                <option selected="selected" value="0">---Select a profile---</option> <!--default option-->
            </select> <!--END drop-down for available profiles-->
        </div>
    </div>
    <!-- /.row -->

当在第一个下拉菜单中进行选择时,使用此函数发出 AJAX 请求:

Javascript AJAX:

<script type="text/javascript">
    $(document).ready(function() {
    //populates the properties drop-down from API call
    function populateProperties(accountID) {
        $.ajax
        ({
            type: "POST",
            url: "/scripts/propertyID.php",
            data: {
                'accountID' : accountID //sends account ID for processing
            },
            cache: false,
            success: function(html) {
                $("#dropProperties").html(html); //refresh the properties drop-down
                populateProfiles($("#dropProperties > option:selected").val()); // Populate profiles after properties load
            }
        });
    }

    //populates the profiles drop-down from API call
    function populateProfiles(propertyID) {
        $.ajax
        ({
            type: "POST",
            url: "/scripts/profileID.php",
            data: {
                'propertyID' : propertyID //sends property ID for processing
            },
            cache: false,
            success: function(html) {
                $("#dropProfiles").html(html); //refresh the profiles drop-down
            }
        });
    }

    //onchange event - repopulates properties and profiles after changing account
    $("#dropAccounts").change(function() {
        var accountID = $("#dropAccounts > option:selected").val(); //gets the account ID from drop-down value
        populateProperties(accountID); //repopulates properties drop-down
    });

    //onchange event - repopulates profiles after changing properties
    $("#dropProperties").change(function(){
        var propertyID = $("#dropProperties > option:selected").val(); //gets the profile ID from drop-down value
        populateProfiles(propertyID); //repopulates the profiles drop-down
    });
});
</script>

这是没有接收任何 $_SESSION 变量的脚本:

propertyID.php

<?php

session_start();

//required Google Analytics libraries
set_include_path("../../lib/google-api-php-client-1.0.4-beta/src");
require_once 'Google/Client.php';
require_once 'Google/Service/Analytics.php';

if($_POST['accountID'])
{
    $account = $_POST['accountID'];
    $_SESSION['account_id'] = $accountID;
    $client = $_SESSION['client'];
    if ($client != null) {
        $analytics = new Google_Service_Analytics($client);

        if ($accountID != "0") {
            $webProperty = $analytics->management_webproperties->listManagementWebproperties($accountID);
            $webItem = $webProperty->getItems();

            foreach ($webItem as $item) {
                echo '<option value=' . $item->getId() . '>' . $item->getName() . '</option>';
            }
        }
    }
}   else {
    echo '<option value="0">---Select a webproperty---</option>';
}
?>

我在 propertyID.php 中从$_SESSION['client'] 接收null(是的,我确实将它保存到会话变量中)。事实上,当我刷新 index.php 时,允许我访问 API 的 cookie 也会被清除。某处正在清除会话,但我不确定如何处理。

任何建议将不胜感激!

【问题讨论】:

  • 会话 cookie 正在消失,或者您每次都获得不同的会话(例如 session_id() 的值发生变化)。
  • 为什么会发生这种情况?奇怪的是,考虑到我所做的唯一一件事就是更新库,这会发生。 编辑:我刚刚检查了两个脚本之间的网络日志,两个脚本上的PHPSESSID 相同。
  • 您是否验证了您的 ajax 调用以查看它是否传递了您的会话 id cookie?另一个原因可能是服务器上不同的域或不同的文件夹不共享相同的 cookie。
  • @user3621911 我该怎么做?在它工作之前,我不必这样做。 :)
  • 使用浏览器的开发者工具查看实际发出的请求。

标签: javascript php jquery ajax session


【解决方案1】:

找出我的问题...

无论出于何种原因,我升级到的库给我带来了问题并擦除了我的会话。零意义,但换个版本解决了这个问题。

感谢所有努力阅读我的问题的人。

【讨论】:

    【解决方案2】:

    你可以调用一个解决方法

    session_start($session-id);
    

    并随时开始会话。

    您可以通过读取 $_COOKIES 变量中的 cookie 来获取会话 ID。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-05-14
      • 2014-06-16
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      • 2010-12-07
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多