Jump to content

[SOLVED] Help


unsider

Recommended Posts

Alright, I've written a poll that allows you to vote once, retreiving your IP and logging it into a .dat file. Well upon loading of the 'index' I am iterating an IF statement to determine whether to display the poll choices, or the poll results based on the IP you are viewing the page with.

 

All of my functions are operational, 'has_voted', and 'the_current_poll_results'

 

Problem: I would like to display my the_current_poll_results(); function.

Although it continues to display my (show_vote_control('1' ); function.

As well as $ipaddress, so everything is outputting.

I fear the problem may be in my syntax somewhere, but I can't for the life of me find it.

 

Everything seems like it should work, although I'm still having problems. If you need any more code, questions, etc.. let me know.

 

index.php

<?php
require_once('backendfunctions.php');
      $ipaddress = $_SERVER['REMOTE_ADDR'];
global $PREVENT_DUPLICATE_VOTES;
if($PREVENT_DUPLICATE_VOTES && has_voted($poll_id, $ipaddress)) {
the_current_poll_results();
}
else
{
echo "$ipaddress";
show_vote_control('1');
show_vote_control('2');
}
?> 

 

 

List of functions:

 

backendfunctions.php

<?php

$PREVENT_DUPLICATE_VOTES = TRUE;

function vote_history_add($poll_id, $ipaddress, $vote_value_id) {

	$history_fp = @fopen(vote_history_file_path($poll_id), "a");
	if($history_fp === FALSE) {
		die("Unable to open history file for writing");
	}

	@flock($history_fp, LOCK_EX); 
	fputs($history_fp, $ipaddress . "|" . $vote_value_id . "\n");
	fclose($history_fp);

}

function explode_history($line) {
return array_map("trim", explode("|", $line));
}

function vote_history_list($poll_id) {

// Load existing vote history
$summarylist = @file(vote_history_file_path($poll_id));
if($summarylist !== FALSE) {
	$summarylist = array_map("explode_history", $summarylist);
}

return $summarylist;

}

function has_voted($poll_id, $ipaddress) {

$vote_history_list = vote_history_list($poll_id);
if($vote_history_list !== FALSE) {
	return( find_vote_history(trim($ipaddress), $vote_history_list) !== FALSE );
} else {
	return FALSE;
}

}

function find_vote_history($ipaddress, $list) {

if(!empty($list)) {
	for($i = 0; $i < count($list); $i++) {
		if($list[$i][0] == $ipaddress) return $list[$i];
	}
}
return FALSE;

}
?>

 

Link to comment
Share on other sites

:o omg... clean code

 

I'm gonna take another look at your code (not saying i know what it is haha) But I noticed that in your backendfunctions.php in you're first if statement there were 3 '==='

 

if($history_fp === FALSE) {
		die("Unable to open history file for writing");
	}

 

Perhaps this is why? Just a shot in the dark i saw quickly

Link to comment
Share on other sites

=== is EXACTLY and is fine.

 

Cosizzle, try this:

 

<?php

$var = FALSE;

if ($var == 0)
  echo '$var == 0<br>';

if ($var === 0)
  echo '$var === 0<br>';

?>

 

=== FALSE is very handy on functions that may return 0, but have not actually failed :)

Link to comment
Share on other sites

:-\ hmm - I have never not once seen === used at least not that I can recall. Thanks for the tip!

 

Glad he helped you understand, saved me from writing up a little demo ;D. Glad you learned something from it though.

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.