【问题标题】:My Post and Session's php script for wordpress not work online but only in local我的 Post 和 Session 的 wordpress php 脚本不能在线工作,只能在本地工作
【发布时间】:2013-05-04 22:41:12
【问题描述】:

我正在开发一个允许用户对一些自定义帖子执行搜索的 wordpress 插件页面。搜索表单基于脚本 ajax,当您单击搜索按钮时,脚本 jquery 会拦截调用并为 wordpress 创建 ajax 调用。此调用在相关的 wordpress 插件类中启动一个方法,该方法处理通过 $ _POST 从搜索表单传递的数据。 然后显示结果并单击其中一个,您将发送到单个页面。 我正在尝试执行的脚本应该允许用户使用浏览器的后退按钮从单个页面返回搜索结果。问题是这个脚本在我的电脑上本地运行良好,但是一旦在线加载,这个插件就会停止工作。 (很抱歉介绍太长,但它很好地定义了问题)。

脚本的工作原理如下:

  1. 填写搜索表单并点击搜索按钮
  2. $_POST 填充了表单数据,我用 $_SESSION = $_POST 填充了 $_SESSION
  3. 打开结果并转到单个页面,然后在浏览器中回击
  4. 使用控件 $_POST = $_SESSION 强制刷新页面设置 (骑行要复杂得多,但这是它所依据的原则)。

    ajaxResult php 函数 该函数在搜索按钮点击时调用

    // Check if user came from single page or from search page
    if(!$_POST && $_SESSION['back_to_hardsoft_results']) {
        if( $_SESSION['back_to_page'] ) {
            $_POST = $_SESSION['back_to_hardsoft_results'];
            $_SESSION['back_to_page'] = null;
        }
    } else if( $_POST && $_SESSION['back_to_hardsoft_results']) {
        $_SESSION['back_to_hardsoft_results'] = null;
    }
    
    ...some controls with the $_POST data...
    
    $search = $this->getQueryObject($this->type, $this->taxonomies, $this->historicalFilter());
    
    include_once TEMPLATE_PATH . CUSTOM_POST_SLUG . '-' . RESULTS_PAGE . '-page.php';
    
    } else if ( ...$_GET...) { ...other things... }
    

当用户访问单个页面时,会创建一个 $_SESSION['back_to_page']。

getQueryObject php 函数由 ajaxResult 调用

$args = array ( ...some args... );          
$q = new WP_Query ( $args);

// Save the query for back to results from single_mediateca_page
$_SESSION['back_to_hardsoft_results'] = $_POST;

return $q; }

我已经在 js 文件中添加了以下字符串来强制刷新页面。

history.navigationMode = 'fast';

我认为问题也可能与浏览器的缓存有关,但我不知道是否真的是由于它的问题。 非常感谢您的帮助!

【问题讨论】:

  • 在 WordPress 中使用 $_SESSION 存在问题 - 请参阅 this questionthis one。不确定它是否与您的问题有关,但可能值得一试。

标签: php wordpress jquery session post


【解决方案1】:

您可能想要使用一个插件,其中改编自 Codeigniter 会话,请查看此https://wordpress.stackexchange.com/a/72744

您也不应该使用类似 $_POST['results'] 的东西将 $_SESSION['back_to_hardsoft_results'] 放在特定索引中。

【讨论】:

    猜你喜欢
    • 2015-04-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-04-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多