check this code for security issues

Discussions of secure PHP coding. Security in software is important, so don't be afraid to ask. And when answering: be anal. Nitpick. No security vulnerability is too small.

Moderator: General Moderators

Post Reply
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

check this code for security issues

Post by shiznatix »

can u guys check for any flaws or obvious exploits in this code. its a voting system that u choose a option and vote for it (obviously) also if its real sloppy i know i was drunk the whole time i made it so bear with the non existing organization.

Code: Select all

<?
ob_start();

//show voting options
function showOptions()&#123;
$file = "poll.txt";

print "<form action='index.php' method='post'>";

$open = fopen($file, "r+");
	while (!feof($open))&#123;
	$read = fgetss($open, 99999);
	$read = explode(",", $read);
	$count = count($read);
		for ($i=0; $i<$count; $i++)&#123;
			if($i == "0")&#123;
			print "<input type='radio' name='option' value='" . $i . "'> " . $read&#1111;$i] . "<br>";
			&#125;else
		print "<input type='radio' name='option' value='" . $i . "'>" . $read&#1111;$i] . "<br>";
		&#125;
	&#125;
fclose($open);
print "<input type='submit' value='submit' name='submit'>";
print "</form>";
print '<br>';
&#125;
//end


//add votes to data.txt and add ip to ip.txt
if (isset($_POST&#1111;'option']) && isset($_POST&#1111;'submit']))&#123;
addVotes();
&#125;


//function addVotes
function addVotes()&#123;

checkIP();

$option = $_POST&#1111;'option'];

	function getOrder($n)&#123; 
	return intval(trim($n)); 
	&#125;
	
$array = array_map('getOrder', explode(',', file_get_contents("data.txt")));
$array&#1111;$option]++;

$fp = fopen('data.txt', 'r+');
fwrite($fp, implode(',',$array));
fclose($fp);

$fz = fopen('ip.txt', 'a');
$ip = $_SERVER&#1111;'REMOTE_ADDR'] . ',';
fwrite($fz, $ip);
fclose($fz);

header('location: index.php?id=1');
&#125;
//end function


if($id == "showResults")&#123;

$array1 = explode(',', file_get_contents("poll.txt"));
$array2 = explode(',', file_get_contents("data.txt"));

$count = count($array1);

	for ($i=0; $i<$count; $i++)&#123;
	$sum = $sum + $array2&#1111;$i];
	&#125;

	for ($b=0; $b<$count; $b++)&#123;
	print $array1&#1111;$b];
	echo "<font size='2'>&nbsp;&nbsp;&nbsp;has&nbsp;&nbsp;&nbsp;";
	print $array2&#1111;$b]; echo ($array2&#1111;$b] == 1 ? ' vote' : ' votes') . "</font><br>";
	print "<img src='bar.jpg' height='20' width='" . ($array2&#1111;$b]/$sum)*150 . "'><br>";
	
	&#125;
&#125;


//check to see if the ip has already voted
function checkIP()&#123;

$arrIP = explode(",", file_get_contents("ip.txt"));
$count = count($arrIP);
	for ($i=0; $i<$count; $i++)&#123;
		if ($arrIP&#1111;$i] == $_SERVER&#1111;'REMOTE_ADDR'])&#123;
		header('location: http://www.wesmokerocks.com/shizPoll/index.php?id=showResults');
		exit();
		&#125;
	&#125;
//if it past the ip test
showOptions();

&#125;

if (!isset($id))&#123;
checkIP();
&#125;

?>
Post Reply