所有子域上的Cookie组合Javascript / PHP

时间:2012-12-21 14:16:02

标签: php javascript cookies

我正在我的网站上实施'cookie通知栏'。这是一个灰色透明的酒吧 通过使用cookies的通知固定在屏幕底部,a 链接到包含更多信息的页面,在最右侧链接“关闭”按钮。酒吧隐藏 使用display:none与javascript on-click事件。我的下一个问题是:是否可以在同一个函数中设置一个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,如果它在主域上设置,它是否适用于我的子域名?

提前致谢!

桑德

2 个答案:

答案 0 :(得分: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");

答案 1 :(得分: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\""; } ?> >  

代替您的Cookie的名称放置名称,并在域中放置您的域名。 希望它有所帮助...

如果您有更多信息,请访问Set cookie wih JS, read with PHP problem