View Full Version : Disable going back?
Moshambi
04-12-2009, 08:16 PM
Ok so I have a poll system I am making and what it does is submits a form, but all the code is on the same page it submits to so that way it will pop up the results in the same div tag. Now what I am curious is if there is an easy way to keep it from users hitting the back button to answer the poll again and again. Should I just make a session?
Any help is appreciated.
When your done processing the data through the database, make it redirect to $_SERVER['PHP_SELF']. And then after the redirect put:
exit;
Moshambi
04-12-2009, 08:30 PM
Thank you Nile but where would I put that in this code if you could:
// Connection stuff here
$username = "root";
$password = "";
$host = "localhost";
$database = "polls";
$connect = mysql_connect($host, $username, $password) or die ( "Could not connect: " . mysql_error() );
$database = mysql_select_db($database, $connect) or die ("Could not select database: " . mysql_error() );
if(!isset($_POST['submit']))
{
// Get all the fields from the poll
$sql = "SELECT * FROM poll";
$resultPoll = mysql_query($sql) or die (mysql_error());
// Find out how many rows there are. The last row will be the most current poll
$pollRows = mysql_num_rows($resultPoll);
/*$sql = "SELECT question FROM poll where id=$pollRows";
$resultAnswers = mysql_query($sql) or die (mysql_error());*/
//Using the $pollRows output the most current poll
echo "<form action='index.php' method='post'>";
echo mysql_result($resultPoll, $pollRows - 1, "question") . "<br />";
echo "<input type='radio' name='question' value='firstAnswer' />";
echo mysql_result($resultPoll, $pollRows - 1, "firstChoice") . "<br />";
echo "<input type='radio' name='question' value='secondAnswer' />";
echo mysql_result($resultPoll, $pollRows - 1, "secondChoice") . "<br />";
echo "<input type='radio' name='question' value='thirdAnswer' />";
echo mysql_result($resultPoll, $pollRows - 1, "thirdChoice") . "<br />";
echo "<input type='radio' name='question' value='fourthAnswer' />";
echo mysql_result($resultPoll, $pollRows - 1, "fourthChoice") . "<br />";
echo "<input type='submit' name='submit' />";
echo "</form>";
}
else
{
$question = $_POST['question'];
//$pollName = $_POST['pollname'];
$username = "root";
$password = "";
$host = "localhost";
$database = "polls";
$connect = mysql_connect($host, $username, $password) or die ( "Could not connect: " . mysql_error() );
$database = mysql_select_db($database, $connect) or die ("Could not select database: " . mysql_error() );
// Update the answer field before doing calculations
$sql = "UPDATE answers SET $question = $question + 1";
$result = mysql_query($sql) or die (mysql_error());
$sql = "SELECT * FROM answers";
$result = mysql_query($sql) or die (mysql_error());
$length = mysql_num_rows($result);
// Get all the numbers and add them together
$first = mysql_result($result, $length - 1, "firstAnswer");
$second = mysql_result($result, $length - 1, "secondAnswer");
$third = mysql_result($result, $length - 1, "thirdAnswer");
$fourth = mysql_result($result, $length - 1, "fourthAnswer");
$sum = $first + $second + $third + $fourth;
$sql = "SELECT * FROM poll";
$result = mysql_query($sql) or die (mysql_error());
// Get the questions to display them for the result
$firstChoice = mysql_result($result, $length - 1, "firstChoice");
$secondChoice = mysql_result($result, $length - 1, "secondChoice");
$thirdChoice = mysql_result($result, $length - 1, "thirdChoice");
$fourthChoice = mysql_result($result, $length - 1, "fourthChoice");
echo "<table>";
echo "<tr><th>Poll Results</th></tr>";
echo "<tr>";
echo "<td>". $firstChoice. "</td><td><span style='color: blue;'>" . number_format((($first/$sum) * 100), 2, '.', ' ') . "</span>%</td></tr><tr>";
echo "<td>" . $secondChoice . "</td><td><span style='color: blue;'>" . number_format((($second/$sum) * 100), 2, '.', ' ') . "</span>%</td></tr><tr>";
echo "<td>" . $thirdChoice . "</td><td><span style='color: blue;'>" . number_format((($third/$sum) * 100), 2, '.', ' ') . "</span>%</td></tr><tr>";
echo "<td>" . $fourthChoice . "</td><td><span style='color: blue;'>" . number_format((($fourth/$sum) * 100), 2, '.', ' ') . "</span>%</td></tr><tr>";
echo "<td colspan='2'>This poll has been taken <span style='color: blue;'>" . $sum . "</span> times.</td></tr></table>";
}
Try;
// Connection stuff here
$username = "root";
$password = "";
$host = "localhost";
$database = "polls";
$connect = mysql_connect($host, $username, $password) or die ( "Could not connect: " . mysql_error() );
$database = mysql_select_db($database, $connect) or die ("Could not select database: " . mysql_error() );
if(!isset($_POST['submit']))
{
// Get all the fields from the poll
$sql = "SELECT * FROM poll";
$resultPoll = mysql_query($sql) or die (mysql_error());
// Find out how many rows there are. The last row will be the most current poll
$pollRows = mysql_num_rows($resultPoll);
/*$sql = "SELECT question FROM poll where id=$pollRows";
$resultAnswers = mysql_query($sql) or die (mysql_error());*/
//Using the $pollRows output the most current poll
echo "<form action='index.php' method='post'>";
echo mysql_result($resultPoll, $pollRows - 1, "question") . "<br />";
echo "<input type='radio' name='question' value='firstAnswer' />";
echo mysql_result($resultPoll, $pollRows - 1, "firstChoice") . "<br />";
echo "<input type='radio' name='question' value='secondAnswer' />";
echo mysql_result($resultPoll, $pollRows - 1, "secondChoice") . "<br />";
echo "<input type='radio' name='question' value='thirdAnswer' />";
echo mysql_result($resultPoll, $pollRows - 1, "thirdChoice") . "<br />";
echo "<input type='radio' name='question' value='fourthAnswer' />";
echo mysql_result($resultPoll, $pollRows - 1, "fourthChoice") . "<br />";
echo "<input type='submit' name='submit' />";
echo "</form>";
}
else
{
$question = $_POST['question'];
//$pollName = $_POST['pollname'];
$username = "root";
$password = "";
$host = "localhost";
$database = "polls";
$connect = mysql_connect($host, $username, $password) or die ( "Could not connect: " . mysql_error() );
$database = mysql_select_db($database, $connect) or die ("Could not select database: " . mysql_error() );
// Update the answer field before doing calculations
$sql = "UPDATE answers SET $question = $question + 1";
$result = mysql_query($sql) or die (mysql_error());
header("Location: ".$_SERVER['PHP_SELF']."?sub");
exit;
}
if(isset($_GET['sub'])){
$username = "root";
$password = "";
$host = "localhost";
$database = "polls";
$connect = mysql_connect($host, $username, $password) or die ( "Could not connect: " . mysql_error() );
$sql = "SELECT * FROM answers";
$result = mysql_query($sql) or die (mysql_error());
$length = mysql_num_rows($result);
// Get all the numbers and add them together
$first = mysql_result($result, $length - 1, "firstAnswer");
$second = mysql_result($result, $length - 1, "secondAnswer");
$third = mysql_result($result, $length - 1, "thirdAnswer");
$fourth = mysql_result($result, $length - 1, "fourthAnswer");
$sum = $first + $second + $third + $fourth;
$sql = "SELECT * FROM poll";
$result = mysql_query($sql) or die (mysql_error());
// Get the questions to display them for the result
$firstChoice = mysql_result($result, $length - 1, "firstChoice");
$secondChoice = mysql_result($result, $length - 1, "secondChoice");
$thirdChoice = mysql_result($result, $length - 1, "thirdChoice");
$fourthChoice = mysql_result($result, $length - 1, "fourthChoice");
echo "<table>";
echo "<tr><th>Poll Results</th></tr>";
echo "<tr>";
echo "<td>". $firstChoice. "</td><td><span style='color: blue;'>" . number_format((($first/$sum) * 100), 2, '.', ' ') . "</span>%</td></tr><tr>";
echo "<td>" . $secondChoice . "</td><td><span style='color: blue;'>" . number_format((($second/$sum) * 100), 2, '.', ' ') . "</span>%</td></tr><tr>";
echo "<td>" . $thirdChoice . "</td><td><span style='color: blue;'>" . number_format((($third/$sum) * 100), 2, '.', ' ') . "</span>%</td></tr><tr>";
echo "<td>" . $fourthChoice . "</td><td><span style='color: blue;'>" . number_format((($fourth/$sum) * 100), 2, '.', ' ') . "</span>%</td></tr><tr>";
echo "<td colspan='2'>This poll has been taken <span style='color: blue;'>" . $sum . "</span> times.</td></tr></table>";
}
Moshambi
04-12-2009, 08:41 PM
Well that didn't really work the way I wanted. I also tried moving it to another place but it still didn't work the way I want...I'm going to try and set up a session variable to make it so the same user can't do it more than once in a session. That would probably be the best way right?
I don't think so. You need to use header(), and exit;
djr33
04-12-2009, 10:25 PM
You cannot disable the "back" feature on the browser. You will have to make it so that when they do go back, it's worthless to them.
There are a few ways of doing this.
The best isn't sessions, but user accounts. You want to make each person only vote once, so make them log in. That guarantees (unless they have 2 or more accounts) one vote per person. It takes a bit longer, but that's the only real way to make the poll accurate.
You can make it a little harder by doing sessions, but just coming back 15 minutes later may generate a new session, so that means they can still vote 4 times per hour, or more if they know what they're doing.
You could additionally include through javascript (ajax) so that the back button wouldn't go back because that is independent of ajax. That won't help with voting again, though.
There are a number of options, but in short you will not stop people from voting twice unless you have some restriction on the voting itself, on a much bigger scale than just the back button. In fact, it is unlikely people will think of hitting back, but instead just try the process again (refresh the page, click "vote" again, whatever).
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.