[PHP|MySQL] Voting with IP Authentication
Here's another tasty morsel; I was working on a voting script for Sano and figured Ah what the hell I'll clean it up and release it. As usual it's GPL, with or without a link back. Here ya go,
<?php
// votepoll.php
// MySQL/PHP Voting script with IP Authentication
//
// Author: Joshua Worden, IWM Entertainment
// Released Nov. 2007 under GPL
//
// Usage: Designed to be called within a webpage using include_once().
// Functions And Definitions
$_site['sql-host'] = "";
$_site['sql-user'] = "";
$_site['sql-pass'] = "";
$_site['sql-data'] = "";
$_site['sql-table'] = "";
$_site['options_num'] = ""; // Number of options in your vote
$_site['options'] = array();
$_site['options'][0] = "Sample Question 1";
// ...
$_site['script-uri'] = $_SERVER['PHP_SELF']; // Don't change unless using this externally.
$_sql['select_all'] = "SELECT * FROM " . $_site['sql-table'];
$_sql['insert_new'] = "INSERT INTO " . $_site['sql-table'] . " (vote, ip) VALUES (`" . $vote . "`, `" . $_SERVER['REMOTE_ADDR'] . "`);";
$_sql['select_auth'] = "SELECT ip FROM " . $_site['sql-table'] . " WHERE ip=" . $_SERVER['REMOTE_ADDR'] . " LIMIT 1;";
function Display_Basic () { // Display voting page
echo "<form action=" . $_site['script-uri'] . " method=post>";
for($count=0, $count++, $count<$_site['options_num']) {
echo "<div style=clear:both;><option name=$count>" . $_site['options'][$count] . "</option></div>";
}
echo "<div style=clear:both;><input type=submit></div>";
echo "</form>";
}
function Display_Results() { // Display results page
// I didn't get around to doing this part.
}
function Display_AuthFail() { // Display results/vote error
echo "Sorry, a vote was already recorded for this IP Address.";
}
function Do_Vote($MyVote) { // Process vote, go to results.
$bool_query = @mysql_query($_sql['select_auth']);
if(!$bool_query) {
$bool_query = @mysql_query($_sql['insert_new']);
}
else { Display_AuthFail(); die; }
}
// Connect to MySQL
@mysql_connect($_site['sql-host'],$_site['sql-user'],$_site['sql-pass']);
@mysql_select_db($_site['sql-data']);
// Check for correct referring mode
if(!isset($_POST['DisplayMode'])) { Display_Basic(); die; }
// Get DisplayMode
$_page['DisplayMode'] = $_POST['DisplayMode'];
// Call correct function due to DisplayMode
if($_page['DisplayMode']=="Vote") { Do_Vote($_POST['vote']); }
elseif($_page['DisplayMode']=="Results") { Display_Results(); }
elseif($_page['DisplayMode']=="AuthFail") { Display_AuthFail(); }
else { Display_Basic(); }
?>
Enjoy.
