Page 2 of 2
Help with cookies!
Posted: Tue Oct 28, 2003 5:08 pm
by packito
Ok, now i'm trying to use cookies to solve this problem. This function is called just after the user send his intention to vote:
Code: Select all
function cookietest() {
//set a new cookie name based on the id of the answer (cRima)
$cookiename="Rimodromo".(string)$_GETї'cRima'];
//check if the cookie is set
if (!isset($_COOKIEї'$cookiename'])){
setcookie($cookiename, (string)$_GETї'cRima'], time()+3600);
return false;
}
else return true;
}
As you can see, my idea is plain simple: if the cookie does not exists, cookietest returns false and the voting table is updated. Otherwise, user gets a warning and the voting table remains unaltered.
The problem is that function cookietest is alway returning false, thus allowing every vote! I only see ONE cookie on my pc, with it's name and value changing according to the differents choices i make! I want to see several cookies, each with it's own name and value.
Can anyone help me with this one?
Thanks in advance!
Posted: Tue Oct 28, 2003 5:41 pm
by qads
your plan would't work at all, you are only allowed up to 20 cookies (per site), and each cookie can only be 4kb in size, so..after setting 20 cookies your back to square one.
why dont you record user ips after they have voted, and every time user wants to vote, you can check it with the database table, Ip address also change too but this wil atleast stop users from voteing right away.
Posted: Tue Oct 28, 2003 7:01 pm
by JAM
Have in mind of serialize() and base64_encode().
Using those, you can transform an array into a string, use that in a cookie. Later on, you retrieve that string, base64_decode/unserialize it and you have the array back as is.
The array of course would in this case store what and how you voted for the different polls, viewed the different images etc. etc...
Example:
Code: Select all
<?php
for ($i=0;$i<=30;$i++) {
$array['vote'.$i] = 'foo_'.$i;
}
print_r($array);
echo $coded = base64_encode(serialize($array));
print_r(unserialize(base64_decode($coded)));
/*
Array
(
[vote0] => foo_0
[vote1] => foo_1
// ...
[vote30] => foo_30
)
YTozMTp7czo1OiJ2b3RlMCI7czo1OiJmb29fM[... etc ...]CI7czo1OiJ2b3RlMSI7
Array
(
[vote0] => foo_0
[vote1] => foo_1
// ...
[vote30] => foo_30
)
*/
?>
Prbolem solved!!
Posted: Wed Oct 29, 2003 10:40 am
by packito
Thanks JAM, for your wonderfull tip!!
After reading about serialize() and company, i developd this function to check for cookies:
Code: Select all
function cookietest(){
$id=$_GET['id_autor'];//get the id of the vote
if (!isset($_COOKIE['rimodromo'])) { //if its a first time user, new cookie must be set
$array['voto_'.$id]=$id;
$coded = base64_encode(serialize($array)); //serialize the array containing the vote
setcookie("rimodromo",$coded,time()+3600);//set the cookie
return false;
}
else{
$old_array=unserialize(base64_decode($_COOKIE['rimodromo'])); // array within the cookie, containing the votes already made by this user
$search_string="voto_".$id;
if (array_key_exists($search_string, $old_array)) //search for the vote key inside the array
else {//if user never voted for this...
$old_array['voto_'.$id]=$id; //add his/her vote to the array...
$coded = base64_encode(serialize($old_array)); //(re)serialize the array...
setcookie("rimodromo",$coded,time()+3600);//and update the cookie!
return false;
}
}
}
Et voila! I now control the user's votes!
There's still the issue of the cookie's size, as qads mentioned, but that's another problem... I'm happy for now!
Thanks to all!!
PackiT0.
PS: i still have a lot to learn about php!...
