【问题标题】:Php Cookie Ignored忽略 PHP Cookie
【发布时间】:2014-11-13 04:37:15
【问题描述】:

我有一个模块,如果它不存在,它应该使用 php 创建一个 cookie。如果它不确认 cookie 的存在,它会创建它并实现一个 js 文件,然后显示一个覆盖。否则函数应该结束。问题是,js 在每次刷新时都会加载,即使它不应该加载。请帮忙,非常感谢,谢谢。

Cookie PHP

<?php
/**
 * @file
 * Checks for cookie, non-existent: creates cookies + implements js, else
 * removes specfic content
 */

/*
 * Implements hook_preprocess_page().
 */
function cookie_check_preprocess_page($hook, &$variables) {
        // Check for cookie
        if (!isset($_COOKIE['firsttime'])) {
            // Set variables for setcookie
            $name = 'firsttime';
            $value = 'no';
            // six months
            $expire = time()+(15768000);
            // Directory
            $path = '/~wolfden/';  
            $domain = 'hitechwolf.com';
            // Create cookie
            setcookie($name, $value, $expire, $path, $domain, true, true);
            // Implement the js
            // Welcome Overlay/Button Functionality ONLY ON FIRST TIME VISIT               
            $path = drupal_get_path('module', 'cookie_check');
            drupal_add_js($path . '/js/welcome_button.js');
        }
        else {
            return '';      
        }
}

覆盖 JS

/**
 * @file
 *  Appends Welcome Overlay to page, upon button
 *  click closes overlay
 */

(function ($, Drupal){
    Drupal.behaviors.welcomeOverlay = {
      attach : function() {
          $('#block-panels-mini-welcome-overlay').appendTo('body');
                function clickButton() {
                $('.welcome-content').fadeTo('500', 0, 'linear', function(){
                    $('.welcome-overlay').slideToggle('700', 'linear', function(){
                  $('#block-panels-mini-welcome-overlay').remove();     
                    });     
                });
    }
    setTimeout(function () {
               $('.welcome-overlay').slideToggle('700', 'linear', function(){
                 $('.welcome-content').fadeTo('500', 1, 'linear');  
               });
    }, 1500); 
    $('.w-button').on('click', clickButton);     
  }
 }
})(jQuery, Drupal);

Site In Question

【问题讨论】:

    标签: php jquery cookies drupal


    【解决方案1】:

    由于一个原因,您的 cookie 未设置。你写的设置cookie的代码是

    setcookie($name, $value, $expire, $path, $domain, true, true);
    

    您设置的最后 2 个参数为 TRUE。倒数第二个 TRUE 是出于安全考虑,它表示 cookie 只能通过来自客户端的安全 HTTPS 连接传输。当设置为 TRUE 时,只有存在安全连接时才会设置 cookie。

    您的网站“http://hitechwolf.com/wolfden/”不是 https。所以你的cookie没有设置。

    设置为 FALSE 试试

    setcookie($name, $value, $expire, $path, $domain, false, true);
    

    【讨论】:

    • 试过了,还是一样的错误,cookie好像还没有设置好,感谢尝试:)
    • 设置 $path = "/" 和 $domain = ".hitechwolf.com"
    • 进度,根据 devtools 的说法,cookie 现在存在,但 js 仍然会在每次重新加载页面时触发......
    猜你喜欢
    • 1970-01-01
    • 2012-02-26
    • 1970-01-01
    • 2012-02-10
    • 2013-03-07
    • 1970-01-01
    • 1970-01-01
    • 2014-11-07
    • 1970-01-01
    相关资源
    最近更新 更多