0

我正在我的网站上实现一个“cookie 通知栏”。它是一个灰色透明条,固定在屏幕底部,带有使用 cookie 的通知、包含更多信息的页面的链接以及最右侧的“关闭”按钮。该栏使用带有 javascript on-click 事件的 display:none 隐藏。我的下一个问题:是否可以在单击关闭栏的同一函数中设置 cookie?因为它似乎不起作用,我的代码:

//就在我的身体标签之前,我有:

<script language="javascript"> 
function closeCookieBar() {
document.getElementById('cookiepolicy').style.display = 'none';
SetCookie("cookiepolicy", 1, 8)
}

</script>

// 在页面底部我有:

<div id="cookiepolicy" <?php if(isset($_cookie['cookiepolicy'])){ echo "style=\"display:none\""; } ?> >
<div id="cookiepolicy-wrapper">
<div id="cookiepolicy-txt">
<span id="cookiepolicy-notice">Deze website maakt gebruik van cookies. Waarom? Klik <a href="http://....nl/cookies.php" target="_blank">hier</a> voor meer informatie.</span>
<span id="cookiepolicy-accept-cookies" onclick="closeCookieBar();">Sluit</span>
</div>
</div>
</div>

我正在预见的下一个问题:我有大约 415 个子域,我也希望保存这个 cookie,如果它设置在主域上,它是否适用于我的子域?

提前致谢!

桑德

4

2 回答 2

0

你可以在 js 中创建你的 cookie 并像这样在 PHP 中使用它

     function closeCookieBar() {
       document.getElementById('cookiepolicy').style.display = 'none';
          var date = new Date();
          date.setTime(date.getTime()+(days*24*60*60*1000));
          var expires = "; expires="+date.toGMTString();
          document.cookie = name+"="+value+expires+"; path=/; domain=.example.com";
     }

  <div id="cookiepolicy" <?php if(isset($_cookie['cookiepolicy'])){ echo "style=\"display:none\""; } ?> >  

代替 name 放置您的 cookie 的名称,并在 domain 中放置您的域。希望能帮助到你...

有关更多信息,您可以访问使用 JS 设置 cookie,阅读 PHP 问题

于 2012-12-22T05:51:05.443 回答
0

我也许可以帮助解决其中的一部分。由于您使用的是 PHP,因此此 PHP 脚本可以设置一个可用于所有子域和子目录的 cookie。如果您使用的是 xxx.co.uk 之类的域,您可能需要在第 30-40 行进行一些调整。

<?php // RAY_cookie_splash_page.php
error_reporting(E_ALL);


// SHOW A SPLASH PAGE ON FIRST ENTRY, THEN COOKIE THE BROWSER TO SKIP SPLASH PAGE
// TO SEE COOKIES IN FIREFOX, FOLLOW TOOLS => OPTIONS => PRIVACY => SHOW COOKIES
// MAN PAGE: http://php.net/manual/en/function.setcookie.php



// DEFINITIONS AS NEEDED HERE
define('COOKIE_LIFE', 60*60*24); // A 24-HOUR DAY IN SECONDS ( = 86,400 )



// CONSTRUCT AND SET THE COOKIE
// USE THIS TO MAKE COOKIE EXPIRE AT END OF BROWSER LIFE
$cookie_expires = 0;

// USE THIS TO MAKE A PERSISTENT COOKIE - DEFINE COOKIE_LIFE IN SECONDS - date('Z') IS UTC OFFSET IN SECONDS
$cookie_expires = time() + date('Z') + COOKIE_LIFE;

// CHOOSE THE COOKIE NAME AND VALUE
$cookie_name  = 'Fred';
$cookie_value = 'Wilma';

// MAKE THE COOKIE AVAILABLE TO ALL DIRECTORY PATHS IN THE WWW ROOT
$cookie_path    = '/';

// MAKE THE COOKIE AVAILABLE TO ALL SUBDOMAINS - DOMAIN NAME STARTS WITH DOT AND OMITS WWW (OR OTHER SUBDOMAINS).
$x = explode('.', strtolower($_SERVER["HTTP_HOST"]));
$y = count($x);
if ($y == 1) // MAYBE 'localhost'?
{
   $cookie_domain = $x[0];
} else // SOMETHING LIKE 'www2.atf70.whitehouse.gov'?
{
// USE THE LAST TWO POSITIONS TO MAKE THE HOST DOMAIN
   $cookie_domain = '.' . $x[$y-2] . '.' . $x[$y-1];
}

// MAKE THE COOKIE AVAILABLE TO HTTP, NOT JUST HTTPS
$cookie_secure = FALSE;

// HIDE COOKIE FROM JAVASCRIPT TO IMPROVE SECURITY (PHP 5.2+)
$cookie_http  = TRUE;

// SET THE COOKIE (BUT DO NOT BOTHER OUTPUTTING THE DEBUGGING MESSAGES)
if (setcookie($cookie_name, $cookie_value, $cookie_expires, $cookie_path, $cookie_domain, $cookie_secure, $cookie_http))
{
// echo "<br/>SUCCESS!  THE COOKIE HAS BEEN SET AND WILL BE AVAILABLE TO THE NEXT PAGE LOAD \n";
} else {
// echo "<br/>FAILURE!  THE COOKIE WAS NOT SET AS EXPECTED \n";
}



// IF THE COOKIE IS NOT AVAILABLE IN THIS SCRIPT, SHOW THE SPLASH PAGE
if (empty($_COOKIE["Fred"]))
{
   echo "<h1>SPLASH PAGE</h1>\n";
   echo "<p><a href=\"{$_SERVER["REQUEST_URI"]}\">CONTINUE TO SITE</a></p>\n";
   echo date('c');
   die("\nSPLASH COMPLETE");
}

// SPLASH PAGE HAS ALREADY BEEN SHOWN
echo "<h1>REGULAR START PAGE - AFTER SPLASH HAS BEEN SHOWN</h1>\n";
echo "<p><a href=\"{$_SERVER["REQUEST_URI"]}\">CONTINUE TO SITE</a></p>\n";
echo date('c');
die("\nREGULAR PAGE COMPLETE");
于 2012-12-21T14:23:18.803 回答