473,466 Members | 1,395 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Php Cookie based voting script

2 New Member
I'm just starting to learn PHP.

I have a website where we are collecting votes for something. I want to use cookies to deter people from re-voting (yes, this is not efficient if they're smart enough to turn their cookies off). I seem to have successfully created a cookie, and then code to read that cookie and redirect. However, it places the cookie before looking for the cookie, so it immediately redirects, rather than redirecting on the next visit.

Can I either:

a) set the cookie to be installed in a few moments (or after the page loads - with the reading script being run immediately as the page loads)

b) set the cookie to be placed on page exit

c) set the cookie to be placed on the submit button for the form for voting?

Thanks for any help.

C.

Here's the code I'm using, in this order, at the very top of the page after the first <?... setting:
Expand|Select|Wrap|Line Numbers
  1. if (!isset($_COOKIE['voted']) || $_COOKIE['voted'] != "voted") {
  2.   header ("Location: ../thanks-for-voting.php");
  3. }
  4.  
  5. setcookie("voted", "voted", time()+86400*10);
  6.  
Oct 10 '07 #1
4 4540
Atli
5,058 Recognized Expert Expert
Hi. Welcome to TSDN!

This is a matter of how you structure your code, I would think.
The way you posted it, it will always redirect you, because it checks to see if the cookie does NOT exists, and seeing as it doesn't, it redirects you. It will never reach the setcookie() function.

Consider this:
Expand|Select|Wrap|Line Numbers
  1. if(!isset($_COOKIE['voted']) or $_COOKIE['voted'] != 'voted']) {
  2.   # Do your voting thing
  3.  
  4.   setcookie("voted", "voted", time()+86400*10);
  5. }
  6. else {
  7.   header("Location: /thank-you-for-voting.php");
  8. }
  9.  
Would that work for you?
Oct 10 '07 #2
BritishAgent
2 New Member
Thank you so much Atli. This was very helpful.

Can you tell me which part was the part that makes it NOT exist?

And, also, what do the ! marks represent?

Thanks again.
Oct 10 '07 #3
Atli
5,058 Recognized Expert Expert
Thank you so much Atli. This was very helpful.

Can you tell me which part was the part that makes it NOT exist?

And, also, what do the ! marks represent?

Thanks again.
The isset() function check if a variable or an element exists, if it has any value. It returns a boolean value, TRUE if it exists or FALSE if it doesn't.

The ! mark "flips" a boolean value, kind of like flipping over a coin. It can be used to check if a boolean value is false.

By using it with a function like isset(), you are essentially flipping it's value. If it returns FALSE, the ! mark will change it into TRUE.
Expand|Select|Wrap|Line Numbers
  1. # This is obviously not true
  2. if(true == false)...
  3.  
  4. # But this is
  5. if(true != false)...
  6.  
  7. # So this
  8. if(!isset($variable))...
  9.  
  10. # Can also be written like this
  11. if(isset($variable) == false)...
  12.  
Oct 10 '07 #4
ak1dnar
1,584 Recognized Expert Top Contributor
[Help! This should be easy - PHP and Cookies] changed the thread title little bit.
Oct 10 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Aki Niimura | last post by:
Hello everyone, I have been trying to write a Python script to access a Web site. I'm currently having a problem completing my script because the contents I would like to access in the Web...
4
by: Shannon Jacobs | last post by:
I'm doing some trivial surveys, and I want to know if the same user answers twice. Can't really know that, but at least I thought I could check for the same browser/computer combination by using a...
1
by: Display Name | last post by:
Used one of these canned scripts to set up a JS quiz but not before having used another canned PHP script for "Tell your friend about this Web page!" sort of thing. Now i've gotta integrate them;...
7
by: What-a-Tool | last post by:
How does the expire date work setting it server side with asp. I know with javascript setting it client side it will be set to the clients local time, and therefore expire when the clients local...
5
by: george | last post by:
Hi All I've used couple of GPL/free voting scripts (some of these are very good), but need to create/find one that allows users to add a new answer. For example: This is a voting question -...
0
by: JT | last post by:
I posted a question earlier about communicating between a javascript function and vb.net. In the end I decided to try using a cookie. I use the following javascript function (from vb.net) to set...
4
toxicpaint
by: toxicpaint | last post by:
Hi there, I've built a form for peolpe to submit a vote and I thought the best way to restrict people voting twice would be to create a cookie called "voted" and set the value to 1 when they vote....
29
by: Jerim79 | last post by:
I did try to find the answer to this before posting, so this isn't a knee jerk reaction. What I am trying to accomplish is to have a script that opens a cookie, reads a value, and then use a...
3
by: rjoseph | last post by:
Hi Guys I hope this is an easy one for you. Basically I have a page on my website that creates a javascript based cookie on the users pc. The contents of the cookie when created looks as...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.