Jump to content

Urgent (for tomorrow) cookies won't save!!


christam1

Recommended Posts

Hi there, I've created this PHP voting script based on a template, but using XML with it as well as SWFs...and I'm wondering why anyone can vote more than once, it's really irritating me!

 

If somebody could possibly have a look at this and tell me what might be wrong with the setcookie part of the script that would be great!

 

The script is supposed to let any one user on a computer vote only once, not as many times as they like which is what it's currently doing!! You can see what it is currently doing here http://www.st-augustines.worcs.sch.uk/departments/music/

 

<title>-Pop Icons 2009-</title>

<?

//
// SET VARIABLES
//

// name of XML file which contains your image data
$xml_file = "poll.xml";

// path to header file
$header_file = "poll_header.inc";

// path to footer file
$footer_file = "poll_footer.inc";

// set to 0 for testing
$one_vote_per_user = 1;

// import request variables
import_request_variables("gp", "request_");

// make "request_pid" an integer
settype($request_pid, "integer");


//
// DEFINE FUNCTIONS
//

// isClosed(): check whether poll is closed
// if poll is closed, return closing date
// if poll is not closed, return false
function isClosed($pid) {
// get "polls" object into this scope
global $polls;

// get closing date and split into three parts (year, month, and day)
$closing_date = explode("-", $polls->poll[$pid]->closingdate);

// if today's date is after closing date, then return closing date
if (mktime() > mktime(0, 0, 0, $closing_date[1], $closing_date[2], $closing_date[0])) {
$closing_date = date("l, F j, Y", mktime(0, 0, 0, $closing_date[1], $closing_date[2], $closing_date[0]));
return $closing_date;
}

// if today's date is on or before closing date, return false
else {
return false;
}
}


// printVotingForm(): print voting form
function printVotingForm($pid) {
// get these variables in this scope
global $polls, $header_file, $footer_file;

// include header file
include($header_file);

// print poll title and form
echo "<h3 style=\"text-align: center\">" . $polls->poll[$pid]['title'] . "</h3>\n";
echo "<form action=\"" . $_SERVER['PHP_SELF'] . "\" method=\"post\">\n";
echo "<fieldset>\n";
echo "<input type=\"hidden\" name=\"pid\" value=\"$pid\" />\n";
echo "<ul style=\"list-style-type: none\">\n";
echo "<table class=\"dark\" width=\"10%\" height=\"10\" cellspacing=\"12\" cellpadding=\"15\">\n";

// count number of answers
$no_answers = count($polls->poll[$pid]->answers->answer);
$flash = $polls->poll[$pid]->answers->answer[$i]->flash;

// print each answer as a checkbox
if ($polls->poll[$pid]->multipleanswers == "yes") {
for ($i = 0; $i < 5; $i++) {
echo "<li><input type=\"checkbox\" name=\"vote[]\" value=\"" . $polls->poll[$pid]->answers->answer[$i]->name . "\" />" . $polls->poll[$pid]->answers->answer[$i]->name .
(isset($polls->poll[$pid]->answers->answer[$i]->image)?
'<img src="' . $polls->poll[$pid]->answers->answer[$i]->image . '">'
:'').
"</li>\n";
}
}

// print each answer as a radio button
else {
for ($i = 0; $i < 3; $i++) {
echo "<td>";
$name = $polls->poll[$pid]->answers->answer[$i]->name;
$song = $polls->poll[$pid]->answers->answer[$i]->song;
$image = $polls->poll[$pid]->answers->answer[$i]->image;
$flash = $polls->poll[$pid]->answers->answer[$i]->flash;

$embedImg = "<br /><img src='{$image}' />";

$embedFlash = "<br /><object width='100' height='30'>
<param name='movie' value='{$flash}'>
<embed src='{$flash}'></embed>
<param name='SCALE' value='noborder' />
</object>";

echo "<input type='radio' name='vote' value='{$name}' /><br />" . "<strong>$name;</strong>";
echo "<br />" . $song;
echo !empty($image) ? $embedImg : '';
echo !empty($flash) ? $embedFlash : '';

"\n";
}
}
echo "<tr>";
echo "<tr>";
echo "<tr>";
echo "<tr>";
echo "<tr>";
echo "<tr>";


echo "</table>";
echo "</ul>\n";
echo "<p style=\"text-align: center\"><input type=\"submit\" value=\"Vote\" /> or <a href=\"" . $_SERVER['PHP_SELF'] . "?pid=$pid&view_results=1\">View Results</a></p>\n";
echo "</fieldset>\n";
echo "</form>\n";

// include footer file
include($footer_file);
}


// addVote(): add user's vote to XML file
function addVote($pid, $vote) {
// get variables into this scope
global $polls, $xml_file;

// update vote tally
for ($i = 0; $i < count($polls->poll[$pid]->answers->answer); $i++) {
if ($polls->poll[$pid]->answers->answer[$i]->name == $vote) {
$polls->poll[$pid]->answers->answer[$i]->tally = $polls->poll[$pid]->answers->answer[$i]->tally + 1;
break;
}
}

// update XML file with new tally
$fp_xml_file = fopen($xml_file, "w");
fwrite($fp_xml_file, $polls->asXML());
fclose($fp_xml_file);
}


// printResults(): display results with optional message
function printResults($pid, $message = "") {
// get variables into this scope
global $polls, $header_file, $footer_file;

// include header file
include($header_file);

// print header
echo "<h3 style=\"text-align: center\">" . $polls->poll[$pid]['title'] . "</h3>\n";

// print optional message
if ($message != "") {
echo "<p style=\"text-align: center\">$message</p>\n";
}

// count number of answers
$no_answers = count($polls->poll[$pid]->answers->answer);

// calculate total number of votes
for ($i = 0; $i < $no_answers; $i++) {
// add this vote subtotal to "grand_total" variable
$grand_total += $polls->poll[$pid]->answers->answer[$i]->tally;
}

// begin printing results table
echo "<table class=\"dark\" width=\"100%\">\n";

// print a table row for each answer
for ($i = 0; $i < $no_answers; $i++) {
// calculate this answer's percentage of total number of votes
if ($grand_total == 0) {
$percentage = 0;
}

else {
$percentage = ($polls->poll[$pid]->answers->answer[$i]->tally / $grand_total) * 100;

// round percentage to 1 decimal place
$percentage = round($percentage, 1);
}

echo "<tr>\n";
echo "\t<th class=\"dark\">" . $polls->poll[$pid]->answers->answer[$i]->name . "</th>\n";

// if percentage is 0, do not print a bar
if ($percentage == 0) {
echo "\t<td></td>\n";
echo "\t<td>0%</td>\n";
}

// otherwise, print a bar
else {
echo "\t<td><img src=\"poll.jpg\" alt=\"$percentage%\" height=\"15\" width=\"$percentage\" /></td>\n";
echo "\t<td>$percentage%</td>\n";
}

// print vote subtotal for this answer
if ($polls->poll[$pid]->answers->answer[$i]->tally == 0) {
echo "\t<td>0 votes</td>\n";
}

elseif ($polls->poll[$pid]->answers->answer[$i]->tally == 1) {
echo "\t<td>1 vote</td>\n";
}

else {
echo "\t<td>" . $polls->poll[$pid]->answers->answer[$i]->tally . " votes</td>\n";
}

echo "</tr>\n";
}

// finish printing results table
echo "</table>\n";

// print total number of votes
echo "<p style=\"text-align: center\">Total Votes Cast: <b>$grand_total</b></p>\n";

// include footer file
include($footer_file);
}


//
// GET XML DATA
//

// create SimpleXML object from XML file
$polls = simplexml_load_file($xml_file);


//
// PROGRAM CODE
//

// print results
if ($view_results) {
printResults($request_pid);
}

// if poll is closed, display poll with closing date
elseif ($closing_date = isClosed($request_pid)) {
printResults($request_pid, "The poll closed on $closing_date.");
}

// if poll is open, check whether user voted
else {
// if user has not voted, display voting form
if (!isset($request_vote) && !isset($_COOKIE['xmlPoll_' . $request_pid])) {
printVotingForm($request_pid);
}

// if user has voted, attempt to add his vote
else {
// if user has already voted, display poll with error message
if (($one_vote_per_user == 1) && isset($_COOKIE['xmlPoll_' . $request_pid])) {
printResults($request_pid, "You voted on " . $_COOKIE['xmlPoll_' . $request_pid] . ".");
}

// otherwise, add vote
else {
// if user cast multiple votes, add each vote
if (is_array($request_vote)) {
for ($i = 0; $i < count($request_vote); $i++) {
addVote($request_pid, $request_vote[$i]);
}
}

// otherwise, add single vote
else {
addVote($request_pid, $request_vote);
}

// add cookie to user's computer to prevent repeat voting
setcookie("xmlPoll_$request_pid", date("l, F j, Y"), time()+60*60*24*90, "/", ".fromthedesk.com");

// then display poll
printResults($request_pid);
}
}
}

?>

Thanks for your help

Chris

Link to comment
Share on other sites

I've got this at the bottom right around where the results table gets called after someone has voted.

 

// otherwise, add single vote
else {
addVote($request_pid, $request_vote);
}

 

I also have at the top

$one_vote_per_user = 1;

 

and around the middle

 

		// if user has already voted, display poll with error message
	if (($one_vote_per_user == 1) && isset($_COOKIE['xmlPoll_' . $request_pid])) {
		printResults($request_pid, "You voted on " . $_COOKIE['xmlPoll_' . $request_pid] . ".");
	}

 

I'm actually so confused, this stuff is what the creator of the original script put in! :(

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.