Cookie For 5 star poll

Phydeaux

Prince
Joined
Aug 9, 2003
Messages
510
I'm a noob when it comes to php, java...ect I have a simple five star voting thing in php, but it doesn't have any cookies to keep people from voting more than one time. I've been looking for days and I can't find out how to make a cookie like this. Is there any one nice enough to show me how to create a cookie that will keep people from voting more than one time, and that will tell me how to use it...ect?

Thanks ;)

P.S. I can give you a link to the poll if you need it.
 
No ideas? Anything would be helpful.
 
There are many ways to do it, but if you're using php, then here is one

setcookie("ses_id",$cookie_value,time()+7200,"/","yourdomain.com");

Obviously, you will fill in the values for your program, rather than just using these. The time()+7200 gives the expiration for the cookie - here the current time + 7200 seconds; i.e. the cookie only lasts two hours.

Make sure you make this call before you start in with html.

The cookie will not necessarily prevent people from voting more than once. I always have my cookies turned off for most sites, and I clean the few I do accept regularly. Anyone can delete a cookie...
 
IbnSina said:
setcookie("ses_id",$cookie_value,time()+7200,"/","yourdomain.com");

Obviously, you will fill in the values for your program, rather than just using these. The time()+7200 gives the expiration for the cookie - here the current time + 7200 seconds; i.e. the cookie only lasts two hours.

It didn't work. Maybe If I sent you the script code you would know why.

There are two parts to my voting thing,

1) I named index,

2) I named training center.

The first one (index) is basically every thing you will see (I took out the stars and put it on another page). Here is the code;

<HTML>
<body style="color: rgb(0, 0, 0); background-color: rgb(0, 0, 0);"
link="#3333ff" alink="#00cccc" vlink="#99ffff">
<!-- The following can be pasted anywhere on the page. Make sure the action goes to THIS page itself -->

<FORM method="post" action="TrainingCenterPage.php">
<SELECT name="your_rating">
<OPTION value="one">1</OPTION>
<OPTION value="two">2</OPTION>
<OPTION value="three">3</OPTION>
<OPTION value="four">4</OPTION>
<OPTION value="five">5</OPTION>
</SELECT>
&nbsp;
<INPUT type="submit" value="rate!">
</FORM>
</BODY>
</HTML>

Yes I know it's html, I saved it as a php file though.

The second one is where all the information is stored, from the vote;

<?php $one = "0"; $two = "0"; $three = "0"; $four = "0"; $five = "1"; ?>

Very simple.

I thought, I would have to put it in the second file, but every time I put it in there and vote it deletes it (should have seen it coming).

So where would I put this code? Do I need another one for html? What's going wrong here? Thanks for any help :) .
 
I'm not sure I understand what you want to do, but am I correct that you want to store how many people gave how many stars? If so that problem is completely different from your first question.

The problem is, the variables only exist for a given time (usually as long as the specific user is on that page), it is initialized for every users separately as well, so if two users vote, each one only sees his own choice. It's not really possible to store the information directly into the php-file. Usually a database (like mysql) is used for these purposes, or you can store it into a flat-file (though that might give you trouble with locked files).
 
You need JavaScript! I can help! Add the following code into the head of your page(between <HEAD> AND </HEAD>)
Code:
<SCRIPT Language="JavaScript">
	function setCookie() {
		// Get the current time and put it forward 6 months (change it if 
		// u want)
		TheTime=new Date()
		TheTime.setMonth(TheTime.getMonth()+6)
		expiredate=TheTime.toGMTString()
		alert(expiredate)
		
		// Sets a cookie with the value 'voted' for this document
		document.cookie="contents=voted; expires="+expiredate
	}

	function checkCookie() {
		// Checks the cookie and returns true if voted, false of not
		if (document.cookie.split("=")[1]=="voted") {
			return true
		} else {
			return false
		}
	}
</SCRIPT>

Now, when they click a button, etc, add the cookie by calling setCookie(). To check whether a cookie is already present, call checkCookie() which will return true or false.

e.g. <input type="button" name="vote" value="Vote!" onclick="setCookie()" >
This will set a cookie
You'll have to work out how to use the value of the cookie to stop more votes. validate procedure in a button...? Maybe in your FORM tag put
Code:
onsubmit="return
checkCookie()"

Hope this helps
 
After posting I saw your next problem...I am not sure what to do, unless you can utilise my above code somehow?
 
Back
Top Bottom