Tracking Cookies to Prevent Multiple Voting on a TopSites

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
BIGB
Forum Newbie
Posts: 2
Joined: Tue Feb 17, 2004 9:53 am

Tracking Cookies to Prevent Multiple Voting on a TopSites

Post by BIGB »

Hi all,

I did a search and didnt find anything that was quite on this.

Basically I have a wonderful looking PHP based topsites program that allows webmasters to list their sites/banners and put the code on their site somewhere for visitors to click through to increase their ranking.

Now the downfall, there was no tracking, either by IP or cookies. Unfortunately the creater of the code hasnt updated it or isnt willing to help on this matter.

I would like to add a simple cookie that doesnt allow multiple voting. I am a complete nooB when It comes to PHP, I could fight my way out of a wet paper bag :lol: But I can follow directions to a tee, I have run and added many hacks to PHP boards, so I just need to know what to start with.

Any help is appreciated, I will check back tonight.

B.
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

See [php_man]setcookie[/php_man].

Note that it isn't quite possible to do what you want.

IP cannot be used to identify visitors (dynamic IPs, NATs, spoofing). AOL users can even have different IPs per session.

A cookie stored on the client's machine can be deleted.

Sessions - user can close the browser and start a new session.

A mixture of the above might nevertheless be useful.
Last edited by McGruff on Tue Aug 09, 2005 5:35 pm, edited 1 time in total.
BIGB
Forum Newbie
Posts: 2
Joined: Tue Feb 17, 2004 9:53 am

Post by BIGB »

Thanks McGruff,

So, I figure I will use cookies, and set them to expire in 24 hours.

here is the code on the setcookie:

Code: Select all

<?php
$value = 'something from somewhere';

setcookie("TestCookie", $value);
setcookie("TestCookie", $value, time()+43200);  /* expire in 24 hours */
setcookie("TestCookie", $value, time()+3600, "/~rasmus/", ".example.com", 1);
?>
Where would be the best place to put this? On the index page of the topsites? Also, what needs to go into the $value = 'something from somewhere' ?

Once I am able to set cookies, is there a snippet of code I can throw into a page to recognize that the person is voting twice, and redirect them to a warning page?

Sorry about all the questions, you guys are great!

B
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

in your code for a 24 hour set cookie (what u want):

Code: Select all

<?php
setcookie("TestCookie", $value, time()+43200);  
?>
$value is the variable..

To check if the user is there using that above code, i would use a simple isset command, ie:

Code: Select all

<?php
if(isset($value)) {
echo "the cookie is set n being noticed";
} else {
echo "cookie isnt present, go log-in";
}
?>
User avatar
tim
DevNet Resident
Posts: 1165
Joined: Thu Feb 12, 2004 7:19 pm
Location: ohio

Post by tim »

oh and a explanation thatt might help you understand the concept:

Code: Select all

<?php
if(isset($value)) {
echo "$value";
} else {
$value = "hi tim";
setCookie ("testcookie", $value);
echo "you need to refresh the page cause the cookie isn't set";
}
?>
This should display "hi tim" on the page. Get it? And I say refresh the page cause one you first visit it, it wont have the cookie yet but once you refresh the page, it will then see the cookie and its "value."

And if you want a example of code to see if the user is voting twice, how about you post your code your using for the voting section
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

In the user comments for the setcookie link I gave you (hint, hint ;) ):
Editor's Note: if you are having problems sharing a cookie between sub-directories, simply set the path attribute to "/". this will give that cookie reign over the whole web tree. this will allow you to set a value in one directory, and then read it in another.]
Only proviso with that is, if you have several apps/sites installed below the web root, you just set a cookie which covers them all.

Store something in the cookie value to check if they have already voted depending on what your voting rules are.

I'd suggest best option is using a combination of the three methods but nothing is foolproof.
Post Reply