0

I've been tinkereing around with PHP and I've come unstuck.

I started out with an index page used to login, this linked to a login page for validation against allowed users, which in turn brought up a Voting Form and finally a page to display the results. In all 5 or 6 pages.
I then started using if/else statements to include all the pages in one. It work up to a point, but when I include the Displayed results page everything breaks down. I though maybe it was because I have 2 separate forms posting back onto the one page.
Can this be done? Is the 2nd post wiping the 1st? I dont really want to post the entire script as it's close to 500 lines, but I will if it helps.
Thanks in advance for any help :)

2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by Rumblefish1976
0

okay, here goes...... If I take Lines 49 - 227ish and put them in a separate file, then the code works.

<?php

if ($_POST["login"]=="goForLogin")
	{
	 
	 //  formly  LOGIN.PHP
	 
	 session_start();
	 
	 $passUser = file('add/users.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
	 $passPass = file('add/password.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
	 $passUser = preg_replace('/\s+/', '', $passUser);
	 $passPass = preg_replace('/\s+/', '', $passPass);
	 
	 $users = array_combine($passUser, $passPass);
	 
     function checkUser($user) 
		{
		 global $users;
		 foreach ($users as $theUser=>$thePassword) 
			{
			 if ($user==$theUser) return true;
			}
		 return false;
		}
	 
	 function checkPassword($user,$password)
		{
		 global $users;
		 foreach ($users as $thisUser=>$thisPassword)
			{
			 if ($user==$thisUser && $password==$thisPassword) return true;
			}
		 return false;
		}
	 
	 $formUser=$_POST["username"];
	 $formPass=$_POST["password"];
	 
	 if (checkUser($formUser)) 
		{
		 if (checkPassword($formUser, $formPass))
			{
			 if ($_SESSION["$formUser"]!=="voted")
				{

				 $_SESSION["currentUser"]=$formUser;
				 $_SESSION["currentPassword"]=$formUser;
if($_POST["tallyVotes"]=="getSomeVotes")
	{

	 session_start();
	 
	 $currentUser=$_SESSION["currentUser"];
	 
	 $passUser = file('add/users.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
	 $passPass = file('add/password.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
	 $passUser = preg_replace('/\s+/', '', $passUser);
	 $passPass = preg_replace('/\s+/', '', $passPass);
	 $users = array_combine($passUser, $passPass);
	 
	 
	 function showPoints($person) 
	 {
	 if (isset($_SESSION["$person"]))
		echo $_SESSION["$person"];
	 else echo "0";
	 }
	 
		 $first=$_POST["firstPlace"];
		 $second=$_POST["secondPlace"];
		 $third=$_POST["thirdPlace"];
	 
	 $_SESSION["$first"]+=3;
	 $_SESSION["$second"]+=2;
	 $_SESSION["$third"]+=1;
	 
	 $_SESSION["$currentUser"]="voted";
	 
	 $numberVoted=0;
	 
	 foreach ($users as $user=>$password) 
	 {
	 if( $_SESSION["$user"]=="voted")
		{
		 $numberVoted++;
		}
	 }
	 
	 ?>
	 
	 <html>
		 
	 <head>
	 <title>My Vote</title>
	 <link rel="stylesheet" type="text/css" href="add/voteStyle.css">
	 </head>
	 
	 <body>
	 
	 <div id="main">
		
	 <h4>Number of Voters : <?php echo $numberVoted." of ".sizeof($users) ?> </h4>
	 <?php 
	 if ($numberVoted==sizeof($users))
		{ ?>
		 <h1>Final Results</h1>
		 <?php
		}
	 else if ($_SESSION["badLogins"]>2)
		{ ?>
		 <h1>Login Error</h1>
		 <?php 
		} 
	 else
		{ ?>
		 <h1>Results so far</h1>
		 <?php 
		} ?>
		
	 <div id="tableContainer">
		 <div class="candidateTable">
			 <div class="candidateTitle">Candidate</div>
			 <div class="candidateName">Adam</div>
			 <div class="candidateName">Caitlin</div>
			 <div class="candidateName">Melanie</div>
			 <div class="candidateName">Norman</div>
			 <div class="candidateName">Sharon</div>
		 </div>
			 
		 <div class="scoreTable">
			 <div class="scoreTitle">Points</div>
			 <div style="text-indent:10px;background:#ff00ff;margin:4px 0px;width:<?php showPoints("Adam")?>0%">  <?php showPoints("Adam");?></div>
			 <div style="text-indent:10px;background:#ff0000;margin:4px 0px; width:<?php showPoints("Caitlin")?>0%">  <?php showPoints("Caitlin");?></div>
			 <div style="text-indent:10px;background:#66ff66;margin:4px 0px; width:<?php showPoints("Melanie")?>0%">  <?php showPoints("Melanie");?></div>
			 <div style="text-indent:10px;background:#3333ff;margin:4px 0px; width:<?php showPoints("Norman")?>0%">  <?php showPoints("Norman");?></div>
			 <div style="text-indent:10px;background:#ff9900;margin:4px 0px; width:<?php showPoints("Sharon")?>0%">  <?php showPoints("Sharon");?></div>
		 </div>
	 </div>
	 
	 <div class="cvLink">
		 <?php 
		 
		 $finalVotes = array('Adam'=>$_SESSION["Adam"], 'Caitlin'=>$_SESSION["Caitlin"], 'Melanie'=>$_SESSION["Melanie"], 'Norman'=>$_SESSION["Norman"], 'Sharon'=>$_SESSION["Sharon"]);
		 $tiedVote=array();
		 
			//when all the users have logged on and voted, run this code
		 if ($numberVoted==sizeof($users))
			{ 
				// sort the votes from highest to lowest
			 arsort($finalVotes,SORT_NUMERIC);
				// slice off the first value of the array i.e. the highest score
			 $highScore=array_slice($finalVotes, 0, 1);
				// separate the $key and the $value
			 foreach($highScore as $name=>$vote)
				{
					//check the original array for any values matching the higest score
				 $winners = array_keys($finalVotes, $vote);
					//separate the $key and the $value
				 foreach($winners as $a=>$winner)
					{
						//add the name of each candidate with the highest score to the array $tiedVote
					 array_push($tiedVote, $winner);
					}
				} 
				
				// If there is a clear winner ........
			 if (count($tiedVote)==1)
				{
				  echo "<h3>$tiedVote[0] was the winner with a score of ".$vote."</h3>";
				}
				
				
				// If there is a tie ......
			 if (count($tiedVote)==2)
				{
				 echo "<h3>There was a tie between $tiedVote[0] and $tiedVote[1] with a score of ".$vote."</h3>";
				}
				
			 if(count($tiedVote)>2)
				{
				 echo "<h3>There was a tie between ";
				 for($x=0;$x<count($tiedVote)-2;$x++)
					{
					 echo "$tiedVote[$x], ";
					}
				 for ($x=count($tiedVote)-2;$x>count($tiedVote)-3;$x--)
					{
					 echo "$tiedVote[$x] and ";
					}
				 for ($x=count($tiedVote)-1;$x>count($tiedVote)-2;$x--)
					{
					 echo "$tiedVote[$x].</h3>";
					}
				}
				 
			  ?>
			 <br/>
			 <a style="float:left" href="killSession.php">Click here to Erase Votes</a>
			 <br/><br/><br/>
			 <?php
			}
			
		 else if ($_SESSION["badLogins"]>2)
			{ ?>
			 <a style="float:left" href="killSession.php">Click here to Start Again</a>
			 <br/><br/><br/>
			 <?php
			}
		 else
			{ ?>
			 <a style="float:left"  href="index_test.php">Click here to return</a>
			 <br/><br/><br/>
			 <?php 
			} ?>
	 </div>
	 
	 </div>
	 
	 </body>
	 
	 </html>
				 
	 <?php
	}
	 else 
		{
				 session_start();
				 $currentUser=$_SESSION["currentUser"];
				 ?>
				 
				 <html>
				 
				 <head>
					 <title>My Vote</title>
					 <link rel="stylesheet" type="text/css" href="add/voteStyle.css">
					 <script type="text/javascript" src="add/voteScript.js"></script>   
				 </head>
					
				 <body onLoad="self.focus();document.voteForm.firstPlace.focus()">
				 
				 <div id="main">
					 
					 <h1>Welcome <?php echo $_SESSION["currentUser"]?>, please make your selection</h1>
						
					 <div id="loginBox">   
						 
						 <form name="voteForm" method="post" action="index_test.php" onsubmit="return checkVotes()">
							 
							 <div class="text">First Place (3 points)<br />
								 <select name="firstPlace" >
								 <option value="Adam">Adam</option>
								 <option value="Caitlin">Caitlin</option>
								 <option value="Melanie">Melanie</option>
								 <option value="Norman">Norman</option>
								 <option value="Sharon">Sharon</option>
								 </select>
							 </div>
							 
							 <div class="text">Second Place (2 points)<br />
								 <select name="secondPlace">
								 <option value="Adam">Adam</option>
								 <option value="Caitlin">Caitlin</option>
								 <option value="Melanie">Melanie</option>
								 <option value="Norman">Norman</option>
								 <option value="Sharon">Sharon</option>
								 </select>
						   	 </div>
								
							 <div class="text">Third Place (1 points)<br />
								 <select name="thirdPlace">
								 <option value="Adam">Adam</option>
								 <option value="Caitlin">Caitlin</option>
								 <option value="Melanie">Melanie</option>
								 <option value="Norman">Norman</option>
								 <option value="Sharon">Sharon</option>
								 </select>
							 </div>
							 
							 <div class="text">
								 <input type="hidden" name="tallyVotes" value="getSomeVotes" />
								 <input type="hidden" name="goForLogin" value="login" />
								 <input type="submit" value="Register my choice">
								 
						 	 </div>
								 
						 </form>
						 <br/>
						 
					 </div>
					 
				 </div>
				 
				 </body>
				 
				 </html>
				 <?php
				 
		}      
				 
				}
			 else
				{
				 header("Location:index_test.php?failCode=3");
				}
			}
		 else 
			{
			 $_SESSION["badLogins"]++;
			 header("Location:index_test.php?failCode=2");
			}
		} 
	 else 
		{
	 	 $_SESSION["badLogins"]++;
		 header("Location:index_test.php?failCode=1");
		}
	}
	
 else 
	{
	 
	 // orignal  index_test.php
	 
	 session_start();
		
	 $_SESSION["currentUser"]="";
	 
 	 $passUser = file('add/users.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
	 $passPass = file('add/password.txt', FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
	 $passUser = preg_replace('/\s+/', '', $passUser);
	 $passPass = preg_replace('/\s+/', '', $passPass);
	 $users = array_combine($passUser, $passPass);
	 ksort($users);
	 
	 $numberVoted=0;
	 
	 foreach ($users as $user=>$password)
		{
		 if($_SESSION["$user"]=="voted")
			{
			 $numberVoted++;
			}
		}
	 
	 if ($numberVoted==sizeof($users))
		{
		 header("Location:countVotes.php");
		}
	 
	 if (!isset($_SESSION["badLogins"])) 
		{ 
		 $_SESSION["badLogins"]=0;  
		}
		
	 if ($_SESSION["badLogins"]>2) 
		{
		 
		 // formly BADLOGIN.PHP
		 
		 ?>
		 <html>
		 
		 <head>
			 <title>My Vote</title>
			 <link rel="stylesheet" type="text/css" href="add/voteStyle.css">
		 </head>
		 
		 <body>
			 
			 <div id="main">
				 <?php
				 if ($_SESSION["badLogins"]>2)
					{ ?>
					 <h1>Login Error</h1>
					 <?php 
					 unset($_SESSION["badLogins"]);
					 ?> 
					 <div class="cvLink">
						 <a style="float:left" href="index_test.php">Return to Login.</a>
						 <img style="margin-left:200px" src="add/alert.png" />
					 </div>
					 <?php
					}
				 ?>
			 </div>
			 
		 </body>
		 
		 </html>
		 <?php
		} 
		
	 else
		{ 
		 
		 //also original  index_test.php
		 
		 ?>
		 
		 <html>
		 
		 <head>
			 <title>Vote Counter</title>
			 <link rel="stylesheet" type="text/css" href="add/voteStyle.css">   
		 </head>
		 
		 <body onLoad="self.focus();document.form.username.focus()">
			 
			 <div id="mainLogin">
				 
				 <h1><a href="ks.php">P</a>lease log in</h1>
					
				 <div id="loginBox">
					 <form name="form" method="post" action="index_test.php">
						 <div class="text">Username<br />
							 <input type="text" name="username" />
						 </div>
						 
						 <div class="text">Password<br />
							 <input type="password" name="password" />
						 </div>
						 
						 <div class="text">
							 <input type="submit" value="Log in" />
							 <input type="hidden" name="login" value="goForLogin" />
						 </div>
					 </form>
				 </div>
				 
				 <?php
				 
				 if ($_GET["failCode"]==1)
					{
					 $message="<p class=\"loginMessage\">".
					 "Bad username entered.<br/> Attempt Number ". $_SESSION["badLogins"]."</p>";
					}
				 
				 if ($_GET["failCode"]==2) 
					{
					 $message="<p class=\"loginMessage\">".
					 "Wrong password entered. <br/>Attempt Number ". $_SESSION["badLogins"] ."</p>";
					}
				 
				 if ($_GET["failCode"]==3) 
					{
					 $message="<p class=\"loginMessage\"> This User has already voted.<br/>Login with different User</p>";
					}
				 
				 if ($_GET["failCode"]=="") 
					{
					 $message="<p class=\"welcomeMessage\">3 Failed attempts to login<br/>will result is a lockout</p>";
					}
				 
				 echo "$message";
				 ?>
				 
				 <div class="users">
					 <?php
					 echo "<p><span class=\"bold\">Users available:<br/> </span>";
					 foreach ($users as $user=>$password) 
						{
						 if($_SESSION["$user"]!="voted")
							{		
							 echo "$user. ";
							}
						}   
					 echo "</p>";
						
					 ?>
				 </div>
				 <br/>
				 
				 <div class="users">
					 <a href="register.php">Register New User </a>
				 </div>
				 
			 </div>
			 
		 </body>
		 
		 </html>
		 <?php
		 
		} 
	 
	}
	
?>

Edited by Rumblefish1976: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.