简体   繁体   English

子域中的Cookie无效

[英]Cookies in subdomain doesnt work

I got the following code: 我得到以下代码:

<?php
$id = $_GET['id'];
$vote = $_GET['vote'];
$month = 2592000 + time();
$cookie = "votez" . $id;
$cookiez = "viewz" . $id;

if(isset($_COOKIE[$cookiez]))        
{
    if(!isset($_COOKIE[$cookie]))        
    {
        setcookie($cookie, "voted", $month, '/', ".mywebsite.co.il");
    }
}
else
{
        setcookie($cookiez, "viewed", $month, '/', ".mywebsite.co.il");
}

?> ?>

lets assume that I go to > www.mywebsite.co.il/example.php?id=1&vote=1 on the first time > it will set the first cookie. 假设我第一次进入> www.mywebsite.co.il/example.php?id=1&vote=1>它将设置第一个cookie。 on the second time > it will set the second cookie. 在第二次>将设置第二个cookie。 on the third time > nothing will happen 第三次>什么也不会发生

this is how it should work. 这就是它应该如何工作的。

but if I go to > www.mywebsite.co.il?/example.php?id=1&vote=2 (after I was at www.mywebsite.co.il/example.php?id=1&vote=1) it will set the first cookie again. 但是如果我转到> www.mywebsite.co.il?/example.php?id=1&vote=2(在我进入www.mywebsite.co.il/example.php?id=1&vote=1之后),它将设置再次是第一个Cookie。

if I will go to > www.mywebsite.co.il?/example.php?id=1&vote=3 (after I was at www.mywebsite.co.il/example.php?id=1&vote=1) it will set the first cookie again. 如果我要转到> www.mywebsite.co.il?/example.php?id=1&vote=3(在我进入www.mywebsite.co.il/example.php?id=1&vote=1之后),它将设置再次是第一个Cookie。

and so on.. 等等..

what do I need to do so no matter what the vote equal, as long as its the same ID, the cookie will be the same? 无论投票数相等,我需要怎么做,只要其ID相同,cookie就会相同?

(this is not the full code and you dont need the full code in order to understand the problem or to solve it). (这不是完整的代码,您不需要完整的代码即可理解问题或解决问题)。

thanks!. 谢谢!。

Anyway, check this http://labs.shikiryu.com/test-cookie.php : 无论如何,请检查此http://labs.shikiryu.com/test-cookie.php

<?
$id = "1";
$vote = "2";
$month = 2592000 + time();
$cookie = "votez" . $id;
$cookiez = "viewz" . $id;

if(isset($_COOKIE["$cookiez"]))        
{
    if(!isset($_COOKIE["$cookie"]))        
    {
        var_dump(setcookie("$cookie", "voted", $month, '/', ".shikiryu.com"));
        echo "2nd cookie set :".$month;
    }
}
else
{
        var_dump(setcookie("$cookiez", "viewed", $month, "/", ".shikiryu.com"));
        echo "1st cookie set :".$month;
}

How do you check your cookie? 您如何检查Cookie? 'cause, for example, in firefox, you must look for your domain (and not your subdomain). 例如,因为在firefox中,您必须查找您的域(而不是子域)。 In chrome, ctrl+I, storage tab. 在Chrome浏览器中,按Ctrl + I,然后单击存储标签。

Can you try this code on your server and come back to tell us if it works, it may comes from your unicode domain name. 您能否在服务器上尝试此代码,然后回来告诉我们它是否有效,它可能来自您的unicode域名。


I've changed the code to (same url): 我已将代码更改为(相同的网址):

<?
$id = $_GET['id'];
$vote = $_GET['vote'];
$month = 2592000 + time();
$cookie = "votez" . $id;
$cookiez = "viewz" . $id;

if(isset($_COOKIE["$cookiez"]))        
{
    if(!isset($_COOKIE["$cookie"]))        
    {
        var_dump(setcookie("$cookie", "voted ".$vote, $month, '/', ".shikiryu.com"));
        echo "2nd cookie named ".$cookie.". is set to :voted ".$vote;
    }
}
else
{
        var_dump(setcookie("$cookiez", "viewed ".$vote, $month, "/", ".shikiryu.com"));
        echo "1st cookie named ".$cookiez." set to : viewed ".$vote;
}

if you try : 如果你试试 :

  1. http://labs.shikiryu.com/test-cookie.php?id=1&vote=1 you'll have bool(true) 1st cookie named viewz1 set to : viewed 1 http://labs.shikiryu.com/test-cookie.php?id=1&vote=1您将拥有bool(true) 1st cookie named viewz1 set to : viewed 1
  2. http://labs.shikiryu.com/test-cookie.php?id=1&vote=3 => bool(true) 2nd cookie named votez1. is set to :voted 3 http://labs.shikiryu.com/test-cookie.php?id=1&vote=3 => bool(true) 2nd cookie named votez1. is set to :voted 3 bool(true) 2nd cookie named votez1. is set to :voted 3
  3. http://labs.shikiryu.com/test-cookie.php?id=1&vote=2 => won't show anything since both cookies are set. 由于两个cookie均已设置,因此http://labs.shikiryu.com/test-cookie.php?id=1&vote=2 =>不会显示任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM