Results 1 to 7 of 7

Thread: Disable going back?

  1. #1
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default Disable going back?

    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.
    What is obvious to you might not be to another.


    My Site

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    When your done processing the data through the database, make it redirect to $_SERVER['PHP_SELF']. And then after the redirect put:
    Code:
    exit;
    Jeremy | jfein.net

  3. #3
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default

    Thank you Nile but where would I put that in this code if you could:

    Code:
    // 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>";
    }
    What is obvious to you might not be to another.


    My Site

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    Try;
    Code:
    // 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>";
    }
    Jeremy | jfein.net

  5. #5
    Join Date
    Nov 2007
    Location
    USA
    Posts
    170
    Thanks
    8
    Thanked 22 Times in 22 Posts

    Default

    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?
    What is obvious to you might not be to another.


    My Site

  6. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    I don't think so. You need to use header(), and exit;
    Jeremy | jfein.net

  7. #7
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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).
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •