/    Sign up×
Community /Pin to ProfileBookmark

Hey

I’m trying to write a fairly basic cookie function that allows a user to vote in a poll only once (provided they haven’t disabled cookies). Once they’ve voted, there is code on the results page that sets a cookie (which appears to be working). If they then went back to the vote page in theory they would be redirected straight to the results page rather than having the opportunity to vote again.

The code I’ve written is:

//vote page
<?php
if ($_COOKIE[“scpvotecheese”]==1) {//if cooie exists on user’s computer
header (“location: http://www.savethechildren.org.uk/stopchildpoverty/rating/commentsmph.php“); //redirect user to the comments page
exit; //and don’t implement the remaining code on this page
} else { //else open the voting form
?>
//html displaying the form
<?php
}
?>

//results page
<?php
setcookie(“scpvotecheese”, “1”,time()+60*60*24*3);
?>

I’d be really grateful if anyone’s got any ideas as to how to make this work!

Cheers
Sara

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@JonaMar 01.2006 — [font=trebuchet ms]Maybe it&#8217;s a string/number issue.[/font]

[code=php]
if (isset($_COOKIE['scpvotecheese']) && $_COOKIE['scpvotecheese'] == '1'){
header("Location: http://www...commentsmph.php");
exit;
}
[/code]
Copy linkTweet thisAlerts:
@sssaraauthorMar 01.2006 — Having seen how you coded it I changed the first line to:

if (isset($_COOKIE['scpvotecheese'])) and took out giving the cookie the value of one as I don't need that presumably? The cookie is set but no redirection happens when I go back to the page
Copy linkTweet thisAlerts:
@bokehMar 01.2006 — The cookie is set...[/QUOTE]How do you know that? Also you are capitalising Location: aren't you?
Copy linkTweet thisAlerts:
@JonaMar 01.2006 — 
[i]Originally posted by sssandra[/i]

[b]Having seen how you coded it I changed the first line to:

if (isset($_COOKIE[&#8216;scpvotecheese&#8217;])) and took out giving the cookie the value of one as I don&#8217;t need that presumably? The cookie is set but no redirection happens when I go back to the page[/b]
[/quote]


[font=trebuchet ms]That is not necessarily true. The isset function checks to make sure that $_COOKIE[‘scpvotecheese’] exists, but it does not affirm that the value is 1. If you have different possible values for the cookie (e.g., 1, 2, 3, or 4), then you may not obtain the level of specificity you require.[/font]
×

Success!

Help @sssara spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...