1

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!.

Ron
  • 3,975
  • 17
  • 80
  • 130
  • Where's the subdomain here ? o_o – Shikiryu Nov 24 '10 at 15:32
  • @Chouchenos Looks like a unicode domain name - חרי שנכנסתי ל.mywebsite.co.il – Orbling Nov 24 '10 at 15:34
  • @Orbling : I don't see a `.` between the חרי שנכנסתי ל and mywebsite.co.il but I may need glasses – Shikiryu Nov 24 '10 at 15:35
  • @Chouchenos Because it's a right-to-left alphabet mixed with an LtR, it really screws up selection, in Chrome at least. – Orbling Nov 24 '10 at 15:36
  • @Orbling that just means "After I entered"--he mistakenly started writing in Hebrew and edited it back to English; there's no unicode internationalized domain here. – Yahel Nov 24 '10 at 18:07
  • @yc Well it could have been such a domain, regrettably I do not speak Hebrew. ;-) – Orbling Nov 24 '10 at 18:09

1 Answers1

1

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.
Shikiryu
  • 10,180
  • 8
  • 49
  • 75
  • $vote = 2 so the vote is always the same and like i said before, it works for me if the vote always the same... – Ron Nov 24 '10 at 16:06
  • @Ron : You're not saving `$vote` in your cookie in the first place... you're saving "voted" in it... – Shikiryu Nov 24 '10 at 16:11
  • @Chouchenos did you mean to write "viewed ".$vote and "voted ".$vote or "viewed ".$id and "voted ".$vote? – Ron Nov 24 '10 at 16:43
  • @Ron : Since from your code you just register inside your 2 cookies "voted" or "viewed" w/o anything else, I wanted to show you that, as the `$_GET['vote']` isn't used in your code, you could put it inside your cookie. I may be wrong. **Anyway** both my cookies are saved only once with their first respective value and not reset as asked if they got the same `id`. If you want a more detailed answer, please be more specific that "set the first cookie again" by editing your question else we can't do anything more. – Shikiryu Nov 24 '10 at 17:00
  • @Chouchenos did you change the code in ur site to ur last edit too? – Ron Nov 24 '10 at 17:16
  • @Chouchenos well, it works but something's wierd :X when I add mysql_query("UPDATE `mydb`.`mydb` SET Views=Views+1 WHERE ID=$id"); in the else { }, when the $vote change it updates mydb – Ron Nov 24 '10 at 17:35
  • Using the domain without leading dot is invalid; but clients most will just add it if it’s missing. So there is no difference. – Gumbo Nov 24 '10 at 17:49
  • @Ron : there's no such thing in your question. Please edit it with the details you need. Anyway, you don't use `$vote` anywhere, you check `$_COOKIE["$cookiez"]` which is different (it's based on $id and not on $vote). – Shikiryu Nov 24 '10 at 22:20
  • @Gumbo, I misread the manual when checking for his answer http://php.net/manual/en/function.setcookie.php thanks for the update :) – Shikiryu Nov 24 '10 at 22:24