Ron
Ron

Reputation: 4095

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. on the second time > it will set the second 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.

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.

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?

(this is not the full code and you dont need the full code in order to understand the problem or to solve it).

thanks!.

Upvotes: 1

Views: 414

Answers (1)

Shikiryu
Shikiryu

Reputation: 10219

Anyway, check this 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? 'cause, for example, in firefox, you must look for your domain (and not your subdomain). In chrome, ctrl+I, storage tab.

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.


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
  2. http://labs.shikiryu.com/test-cookie.php?id=1&vote=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.

Upvotes: 1

Related Questions